Hi I have a jquery popup script I am trying to call in a php function.
The script calling the function is:
<link rel='stylesheet' href='css/sweetalert.css'>
<script src='js/sweetalert-dev.js'></script>
<script src='https://code.jquery.com/jquery-2.1.3.min.js'></script>
<?php
include('functions/notification.php');
$notification_message = "There is a problem loading Smart Telecom at the moment.";
notify($notification_message);
?>
and the function call in notification.php:
<?php
function notify($message)
{
$pop.= "<script>sweetAlert('". $message ."', 'Our Sincere apologies.', 'error');</script>";
echo $pop;
}
?>
As it is, the pop up will display with an error about undefined variable $pop. If I try to disable error notifications, the popup doesn't display. if I do it without the concatenation, the popup also doesn't display.
Where am I going wrong?
UPDATE - SOLVED:
Got it to work. echoed "nbsp;", just before echoing $pop
Ok so follow these steps to ensure the code is working, then check the pop issue:
Delete all data in the function to look like below:
<?php
function notify($message) {
echo $message;
} //output: data passed to $message variable should be displayed?>
if echo $message works then
<link rel='stylesheet' href='css/sweetalert.css'>
<!-- You must always place the jQuery first and then call functions or it will not work -->
<script src='https://code.jquery.com/jquery-2.1.3.min.js'></script>
<script src='js/sweetalert-dev.js'></script>
<?php
function notify($message) {
$pop = "<script>sweetAlert('$message', 'Our Sincere apologies.', 'error');</script>";//refer link: http://stackoverflow.com/questions/10512452/php-using-a-variable-inside-a-double-quotes
echo $pop;
}?>
Related
Here is my CSS:
#optionHotel{
display:none;
}
Here is my JavaScript:
function cb1(type){
switch(type){
case "hotel":
alert("hotel");
$("#optionHotel").css("display","block");
break;
}
}
Here is my Html:
<div id="optionHotel"> Some Element In here</div>
Start Script in 'Head Tag':
<?echo '
<script>window.onload = cb1("'.$orderType.'");</script>
'?>
<!--CSS-->
<link href="../../css/navigate.css" rel="stylesheet"/>
<link href="../../css/reservation.css" rel="stylesheet"/>
Passing data from php to js is ok because I have checked In the switchcase
with alert() it's ok but I don't know why .css display to block doesn't work
please advice, Thank in advance
Your code:
<script>window.onload = cb1("'.$orderType.'");</script>
will call the cb1() function immediately and try to assign its result as the window.onload handler. You see the alert because the function does run, but because it runs immediately from inside the head of the document the document body has not yet been parsed so the script can't find your element.
You need to assign an actual function as the handler, where that function will be run onload and at that point will call cb1():
<script>window.onload = function() { cb1("'.$orderType.'"); };</script>
Or, since you are using jQuery, and assuming you don't want to wait for images to load before calling your function, use a document ready handler:
<?echo '
<script>
$(document).ready(function() {
cb1("'.$orderType.'");
});
</script>
'?>
...or move the script to the end of the body and call your function directly:
<?echo '
<script>cb1("'.$orderType.'");</script>
'?>
i give another codes for example
this is my some3.php code:(First file)
:
<head>
<script src="jquery-1.7.2.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('p').click(function(){
var who = $('input#who').val();
var why = $('input#why').val();
$('#geting').load('file2.php',{who:who,why:why},function(applyData){
if ( applyData == 'YEY . Ye have hi' ){
alert('OKKK data is ok ');
} else{
alert('Nooo We dont have requested output');
}
});
});
});
</script>
</head>
<body>
<p> click </p>
<input type="text" id="who">
<br>
<input type="text" id="why">
<div id="geting" align="center">
</div>
</body>
i this my file2.php:
<?php
echo "1";
echo "2";
if($_REQUEST['who'] == "hi"){
$myVariable = "YEY . Ye have hi";
echo $myVariable;
} else{
$myVariable = "The out put is not Hi";
echo $myVariable;
}
?>
its not work why? becuse we have echo "1" and echo "2"
i want jquery just check $myVariable data not whole php callback ! i think i must use json but i dont know how
Well, assuming that you want to read the value with JQuery off the page you are posting to, you could do this, since you are echo'ing the value out in that page by doing the following: echo $myVariable;
Now this is how I generally read a value off another page with JQuery which is by using JQuery's get() method.
$.get("thepagetoretrievefrom.php", function(retrievedvalue) {
alert("Here's the data you requested: " + retrievedvalue);
if (retrievedvalue == 1) {
//print out something here
alert("The retrieved value was 1.");
}
});
And that should retrieve the value from the PHP page. "thepagetoretrievefrom.php" is the page where you want to retrieve the information from. function(retrievedvalue) just indicates that whatever output you're requesting from the page via JQuery will be put into retrievedvalue. Then, using JQuery, you may decide whether you want to do a new call to another page depending on what the "retrievedvalue" was.
This, however is not the best method to achieve this, since this will print whatever may be in that page, but if you are requesting one specific value from that page, then it shouldn't be an issue.
<html>
<script type="text/javascript">
function show_alert()
{
alert("I am an alert box!");
}
</script>
<body>
<?php
$x = 8;
if($x ==10){
echo "Hi";
}
else{
echo 'show_alert()';
}
?>
</body>
</html>
How do I get the echo to output the value of show_alert() ?
Change
echo 'show_alert()';
to
echo '<script>show_alert()</script>';
so that the browser knows to treat show_alert() as a function call and not regular HTML text.
You need to wrap it in a script tag:
if($x ==10){
echo "Hi";
}
else{
echo '<script type="text/javascript">show_alert();</script>';
}
Note, this will not wait until the page has finished loading to call show_alert(). The alert will be displayed as soon as the browser reaches this point in the page rendering, which may be otherwise incomplete behind the alert box. If you want it to wait until the whole page is loaded, place the condition to be called in <body onload>
<body <?php if ($x != 10) {echo 'onload="show_alert();"';} ?>>
<?php
if ($x == 10)
{
echo "Hi!";
}
?>
</body>
If you mean call showAlert() when the browser renders/evaluates that line:
echo '<script type="text/javascript">show_alert();</script>';
If you mean get the value of showAlert() in PHP, you can't - PHP is a server-side language.
This:
echo 'show_alert()';
will simply print "showAlert()" on the page, unless you have already opened a <script> tag.
I think you may be confused about the difference between client side and server side code.
HOWEVER, if you are using the two correctly, and you want to make it appear:
echo '<script type="text/javascript">show_alert();</script>';
It depends largely upon when you want the show_alert() javascript function to be called. Guessing by the PHP code that you're using, I am going to assume that you want the javascript function to be called as soon as the page loads, in which case you might want to use PHP before the body loads, and add an "onload" attribute event handler to your body tag:
if($x ==10){
echo '<body>';
}
else{
echo '<body onload="show_alert();">';
}
Hey, I just made a simple function to load JavaScript files from a template view, like this:
public function includeJavascript($file){
if(file_exists('path/to/js/' . $file)){
$this->set('script', '<script type="text/javascript" src="/path/to/js/' . $file . '"></script>');
}
}
The string is passed via $script and gets printed in the main layout. This means that when I render the page I actually get: <script type="text/javascript" src="/path/to/js/myjavascript.js'"></script> in the HTML, but the JavaScript code wont run. Why is this?
Sincerely, Why
Temporarily add this line to the top of myjavascript.js as a sanity check..
alert('found myjavascript.js OK!');
Try this:
public function includeJavascript($file){
if(file_exists('path/to/js/' . $file)){
$this->set('script', '<script type="text/javascript" src="/path/to/js/' . $file . '"></script>');
}
}
Addded an apostrophe prior to path/to/js/ in the file_exists function.
If you look at the code you posted in the HTML:
<script type="text/javascript" src="/path/to/js/myjavascript.js'"></script>
Note the extra apostrophe after myjavascript.js? That's why it won't work.
I'm drawing a complete blank why this isn't working. I can do it with one variable passing through, but not two. When I use actually numbers like getnt(1,2) it works. It's just not working with two PHP variables.
<script type="text/javascript">
function getnt(nid,pnid) {
window.location = "nt.php?nid=" + nid + "&pnid=" + pnid;
}
</script>
<body>
<?php
echo "<a href='#' onclick='getnt($nid,$pnid)'>VIEW</a>";
?>
</body>
I can make the code work with echo "<a href='nt.php?nid=$nid&pnid=$pnid'>VIEW</a>";, but that's no good if I want to add in alerts and javascript commands.
If the ID and pnID are strings, enclose them with brackets like this.
<body>
<?php
echo "VIEW";
?>
</body>
If still not working, You can debug your code
View the source code in browser,
make sure it generates correctly.
Put some alert messages in the
javascript function. Install Firebug
if you have Firefox or see
Javaascript console if you get any javascript errors.
You could always try:
<script type="text/javascript">
function getnt(nid,pnid) {
window.location = "nt.php?nid=" + nid + "&pnid=" + pnid;
}
</script>
<body>
VIEW
</body>
Your question is probably best answered by looking at the rendered HTML source.
In any case, here's how I'd do it using graceful degradation
<script type="text/javascript">
function getnt(element) {
var href = element.href;
var nid = element.getAttribute("data-nid");
var pnid = element.getAttribute("data-pnid");
return true;
}
</script>
<p><a href="nt.php?nid=<?php echo $nid ?>&pnid=<?php echo $pnid ?>"
data-nid="<?php echo $nid ?>"
data-pnid="<?php echo $pnid ?>"
onclick="return getnt(this)">VIEW</a></p>