echoing a jquery alert popup in php - php

I want to display a pop-up alert box upon some condition in PHP being satisfied. Something like:
echo "<script type="text/javascript"> alert('bleh'); </script>";
except using a custom jquery alert box. Is this possible??
I've tried something like:
echo "<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function() {
$( "#dialog-message" ).dialog({
modal: true,
buttons: {
Ok: function() {
$( this ).dialog( "close" );
}
}
});
});
</script>";
but it gives me a weird effect. Not pop up.
Thanks for your interest.

echo <<<EOD
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$("#dialog-message").dialog({
modal: true,
buttons: {
Ok: function() {
$( this ).dialog( "close" );
}
}
});
</script>
EOD;

Try something like this. Where $bleh is the PHP variable with the message to show.
<script type="text/javascript">
$(document).ready(function(){
var dlg = $('<div>').text(<?php echo $bleh ?>);
$(body).append(dlg);
dlg.dialog(
modal: true
);
});
<script>

PHP is expecting " (double quotes) in any lines like rel="stylesheet" to be php code. You have to either use ' (single quotes) or escape them with a function around the echo like add_slashes. To get around this, I generally use single quotes around echo contents, so all the double quotes are not bothered.
echo 'all the "quoted" stuff here';

Yes its possible..
echo "<script type=\"text/javascript\">alert('bleh');</script>";
but i am not sure if its a good way to write whole function in echo, Instead Define Your Function in HTML some where in your Page and echo the function name here like i did above..

as noted before you need to escape your character or use it like this so no need to escape characters in here
<?php if (condition == true) : ?>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function() {
$( "#dialog-message" ).dialog({
modal: true,
buttons: {
Ok: function() {
$( this ).dialog( "close" );
}
}
});
});
</script>
<?php endif; ?>

Sure thing. you probably want something around this:
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
//check for PHP condition. Script will only be output on condition pass.
<?php if(condition == true){ ?>
<script>
$(function() {
$("#dialog-message").dialog({
modal: true,
buttons: {
Ok: function() {
$(this).dialog("close");
}
}
});
});
</script>
<?php } ?>
basically, you don't actually need to 'echo' the script, as long as you're outputting it from a .php file and not an external .js file. the script will show up as long as the condition passes.

Related

jQuery load with php $_GET didn't work

i want to send Get when the link clicked to receive the result in php code
and this what happened
this in php file working perfect
<html>
<a class="cc" href="#">click me</a>
<script src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$(".cc").click(function () {
$(".body").load('page.php ')
})});
</script>
<section class="body"></section>
</html>
but when i put GET to the href didn't work just Flashing content
<html>
<a class="cc" href="?id=1">click me</a>
<script src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$(".cc").click(function () {
$(".body").load('page.php ')
})});
</script>
<section class="body"></section>
</html>
In the second case, you really need to stop the <a> from following the link.
$(document).ready(function() {
$(".cc").click(function(e) {
e.preventDefault();
$(".body").load('page.php');
});
});

Creating an alert box with a button connected to a link

Salutations! I would like to create an alert box which echo a button connected to a link. I would like to do the whole thing using php. I would like to put that piece of code in my codeigniter controller function.
Here is a piece of code that can echo a button connected to a link:
echo 'Add to Google Contacts';
Please help me to do my task. Thanks a lot.
Alert is a special kind of method which is only used for Showing some messages,with a button is attached.Only we can write text in it.If you want an alert box with your own components, design a new one yourself.But it will never become an alert box.By using jquery you can do this ..paste it.
Here is the Jquery code for dialog box,paste it.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style></style>
</head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js"></script>
<script src='template/js/jquery.textarea-expander.js'></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript">
// <---- VENTAÑAS DE PARAMETERES---->
$(document).ready(function() {
var regex,v,l,c,b,i,contapara=3;
$( "#wnd_Addparam" ).dialog({
autoOpen: false,
height: 'auto',
width: 350,
modal: true,
resizable:false,
buttons: {
"Link": function() {
location.href="http://www.google.com";
return false; },
Cancel: function() {
$( this ).dialog( "close" );
}
},
close: {}
});
$( "#wnd_Addparam" ).dialog( "open" );
});
</script>
<body>
<div id="wnd_Addparam" title="Information" ></div>
</body>
</html>
Hope this will help you dr..
You can use it with JavaScript to like,
echo "<a href='onclick='javascript:function(id)''></a>";
and the javascript you can use it by,
function(id)
{
window.location.href("file_name.php")
}
echo "<a href='#'><button onclick='".'alert("your alert message")'."'>Try it</button>";

jQuery code for automatic Increment the item depends on the date

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery-ui.min.js"></script>
<link href="jquery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(function(){
var theDialog = $(".mydialog").dialog({
autoOpen: false,
resizable: false,
modal: true,
width: 'auto'
});
$('#one').click(function(){
theDialog.html('some random text').dialog('open');
});
$('#two').click(function(){
theDialog.html('<ul><li>Apple</li><li>Orange</li><li>Banana</li><li>Strawberry</li><li>long text string to see if width changes</li></ul>').dialog('open');
});
$('#three').click(function(){
//this is only call where off-centre is noticeable so use setTimeout
theDialog.html('<img src="./random.gif" width="500px" height="500px" />');
setTimeout(function(){ theDialog.dialog('open') }, 100);;
});
});
</script>
hi friends i have question, i have an iteam, no of 20 i want to add 2 item daily with the total item automatic, i need the jquery code , anyone can help me plz?

Passing PHP value to jQuery progressbar

How to pass the PHP value $sources to the value of progress bar? I am trying to add into session in the below code but fail. Any easy way to get the $sources? Please help.
......
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
var score = '<%= session.getAttribute("sources") %>';
$( "#progressbar" ).progressbar({
value: score
});
});
</script>
</head>
<body>
<?php
session_start();
$sources=10;
......
echo '<td>Your answer was correct </td></tr>';
$sources+=1;
......
$_SESSION['sources']=$sources;
?>
<div id="progressbar"></div>
</body>
</html>
session_start() should be at the top of the page. after starting
then $_SESSION['sources'] should be set before calling it. otherwise you should check for it's value before printing to web page.
find below codes and rearrange your script.
<?php
session_start();
?>
......
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
var score = <? echo (isset($_SESSION['sources'])) ? $_SESSION['sources'] : ""; ?>;
$( "#progressbar" ).progressbar({
value: score
});
});
</script>
</head>
<body>
<?php
$sources=10;
......
echo '<td>Your answer was correct </td></tr>';
$sources+=1;
......
$_SESSION['sources']=$sources;
?>
<div id="progressbar"></div>
</body>
</html>
var score = '<%= session.getAttribute("sources") %>';
The <%= %> is java syntax for scriplets ,
additionally it's wrapped in quotes so it is treated as a string literal from javascript.
Below is the syntactically right way (I don't know if you'll get the right value. That
depends on your program structure).
var sources = <?php echo $_SESSION['sources']; ?>;

Jquery modal form is not working for me in PHP

Recently I'm starting to use jquery, when trying to popup a modal form using jquery its won't working for me, the css file and javascript files are keeping locally. Please see the code snippet below
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var $dialog = $('<div></div>')
.html('This dialog will show every time!')
.dialog({
autoOpen: false,
title: 'Basic Dialog'
});
$('#opener').click(function() {
alert('test');
$dialog.dialog('open');
// prevent the default action, e.g., following a link
return false;
});
});
</script>
</head>
<body>
<?php
echo '<link rel="stylesheet" type="text/css" href="jquery-ui.css">';
?>
<button id="opener">Open the dialog</button>
</body>
Please correct me if I did any mistake. Thanks all.
You need to include:
modal: true,
in your dialog definition like this:
var $dialog = $('<div></div>')
.html('This dialog will show every time!')
.dialog({
autoOpen: false,
modal: true,
title: 'Basic Dialog'
});

Categories