How can I echo this javascript if the php error messages is called? I have an error message setting that when a user misses his username or password it triggers an error message. The php error message is called by a php code. Here is the code:
<?php echo showmessage($msg) ?>
I have an alert message in javascript that when called it will make a javascript css pop up alert box. IF the javascript code is present it will show the alert box right away after reload. Here is code:
<script type="text/javascript">
$(document).ready(function () {
jqxAlert.alert('Alert Message');
})
</script>
How can I incorporate so that when the php echo message comes up it will trigger the javscript alert message. I was trying an if in php, so something like this code:
if ( showmessage($msg) ) {
<script type="text/javascript">
$(document).ready(function () {
jqxAlert.alert('Alert Message');
})
</script>
}
How can I echo my javascript message on the php call?
This could be written like this:
endif
endif-stackoverflow
<?php if (showmessage($msg)): ?>
<script>
$(document).ready(function (){
jqxAlert.alert('Alert Message');
});
</script>
<?php endif; ?>
Or like this:
<?php if (showmessage($msg)) { ?>
<script>
$(document).ready(function (){
jqxAlert.alert('Alert Message');
});
</script>
<?php } ?>
Or like this:
<?php
if (showmessage($msg)) {
echo
'
<script>
$(document).ready(function (){
jqxAlert.alert(\'Alert Message\');
});
</script>
';
}
?>
short tags
short hand comparison (ternary)
ternary 2
Or even this (probably not the best idea though):
<?= (showmessage($msg)) ? '<script>$(document).ready(function (){jqxAlert.alert(\'Alert Message\');});</script>' : ""; ?>
Alternatively, if you mean you would like to put the error message in that alertbox
<?php
if (showmessage($msg)) {
echo
'
<script>
$(document).ready(function (){
jqxAlert.alert('.showmessage($msg).');
});
</script>
';
}
?>
and again in the first style:
<?php if (showmessage($msg)): ?>
<script>
$(document).ready(function (){
jqxAlert.alert(<?= showmessage($msg) ?>);
});
</script>
<?php endif; ?>
something like this.. close your php tag before starting to write javascript..
<?php if ( showmessage($msg) ) { ?>
<script type="text/javascript">
alert('Alert Message');
</script>
<?php } ?>
Can you try this
<?PHP if ( showmessage($msg) ) { ?>
<script type="text/javascript">
$(document).ready(function () {
jqxAlert.alert('Alert Message');
});
</script>
<?PHP } ?>
Related
<?php if ($data == "") { ?>
<script type="text/javascript">
alert("review your answer1");
window.location.href = "index1.php";
</script>
<?php } else { ?>
<script type="text/javascript">
alert("review your answer2");
window.location.href = "index2.php";
</script>
<?php } ?>
Alert box is not showing in above code as well as page is also not redirecting.
What is wrong in it?
Do the other way around:
<script>
var data = <?php echo $data; ?>
if ( data === "" ) {
alert("review your answer1");
window.location.href = "index1.php";
} else {
alert("review your answer2");
window.location.href = "index2.php";
}
</script>
Improving your code, it may require some adaptation:
<script>
alert("review your answer<?echo $data;?>");
window.location.href = "index<?echo $data;?>.php";
</script>
I am using this Plugin to add a marker to a image but they are in php echo formats but i want to get them as java script alert method or as a div to print using JavaScript ..
Code :
<script type="text/javascript">
$(document).ready(function() {
$('#map').dropPin('showPin',{
fixedHeight:495,
fixedWidth:700,
backgroundImage: '/images/{some-image.jpg}',
pin: '/image/{custom-pin-graphic.png}'
pinX: <?php echo {pinXcoord} ?>,
pinY: <?php echo {pinYcoord} ?>
});
});
</script>
i try this
<script type="text/javascript">
$(document).ready(function() {
$('#map').dropPin('showPin',{
fixedHeight:495,
fixedWidth:700,
backgroundImage: '/images/{some-image.jpg}',
pin: '/image/{custom-pin-graphic.png}'
pinX: alert(pinXcoord),
pinY: alert(pinYcoord)
});
});
</script>
but i can"t get the value
You can try this:
<script type="text/javascript">
$(document).ready(function() {
var x = <?php echo {pinXcoord} ?>;
var y = <?php echo {pinXcoord} ?>;
$('#map').dropPin('showPin',{
fixedHeight:495,
fixedWidth:700,
backgroundImage: '/images/{some-image.jpg}',
pin: '/image/{custom-pin-graphic.png}'
pinX: x,
pinY: y
});
alert(x+" "+y);
});
</script>
You can try this, assuming that you have the php variables($pinXcoord, $pinYcoord) defined in the same page.
<script type="text/javascript">
$(document).ready(function() {
$('#map').dropPin('showPin',{
fixedHeight:495,
fixedWidth:700,
backgroundImage: '/images/{some-image.jpg}',
pin: '/image/{custom-pin-graphic.png}'
pinX: '<?php echo $pinXcoord; ?>',
pinY: '<?php echo $pinYcoord ?>'
});
});
</script>
My variable contains a color name. My example:
<?php
$myvar = blueColour;
?>
I need to add this value to body of a html page using jQuery:
<script type="text/javascript">
jQuery(body).addClass("<?php echo $myvar; ?>");
</script>
Can I use php inside jQuery this way? Thank you.
EDIT: its an wordpress site. I use this in a if to add that class on a specific page template.
<script type="text/javascript">
jQuery("body").css("color", "<?php echo $myvar; ?>");
</script>
yes, as long as your html file is interpreted by php (having .php/.phtml extension)
I did it. Worked.
Correct is:
jQuery('body').addClass("<?php echo $myvar; ?>");
with
jQuery('body')
not
jQuery(body)
if ( is_page_template('nameOfYouTemplate.php') ) {
echo '<body class='. $myvar .'>';
} else {
echo '<body>';
}
What I have is php that updates the database field after a client signs a form. This works, but I want it to redirect to a new page after it is confirmed that the user signed it by clicking OK. It they click CANCEL it will leave them on the same page.
<?php
$username = 'joeblow';
require_once ("/mypath/include/connnect_db.php");
?>
<p>Please only submit this after above form is completely signed.</p>
<form id="item_complete" method="post">
<input name="submit" type="submit" form="item_complete" value="Submit After Signing">
</form>
<?php
if(isset($_POST['submit'])) { //if the submit button is clicked
$complete = 'c';
$query = "UPDATE mytbale SET mycolumn='c' WHERE username='$username'";
mysqli_query($con,$query) or die("Cannot Update");
echo "<script> confirmFunction(); </script>";
}
require_once ("/mypath/include/disconnect_db.php");
?>
<script type="text/x-javascript">
function confirmFunction(){
var r=confirm("Confirm that you have signed the form!");
if (r==true){
window.open("http://smartpathrealty.com/smart-path-member-page/");
}
else {
}
}
</script>
My problem is the javascript function does not execute after the php updtates the database.
I appreciate any advice or comments you have about this.
The problem is that you are using separate <script> tags and calling the function before it is defined. These two together do not work well. Also, I'm pretty sure that <script type="text/x-javascript"> does not work anyway since it's outdated and that you want <script type="text/javascript">
You can do the following:
Move function up and fix x-javascript:
<?php
$username = 'joeblow';
require_once ("/mypath/include/connnect_db.php");
?>
<p>Please only submit this after above form is completely signed.</p>
<form id="item_complete" method="post">
<input name="submit" type="submit" form="item_complete" value="Submit After Signing">
</form>
<script type="text/javascript">
function confirmFunction(){
var r=confirm("Confirm that you have signed the form!");
if (r==true){
window.open("http://smartpathrealty.com/smart-path-member-page/");
}
else {
}
}
</script>
<?php
if(isset($_POST['submit'])) { //if the submit button is clicked
$complete = 'c';
$query = "UPDATE mytbale SET mycolumn='c' WHERE username='$username'";
mysqli_query($con,$query) or die("Cannot Update");
echo "<script> confirmFunction(); </script>";
}
require_once ("/mypath/include/disconnect_db.php");
?>
Fiddle: Fiddle
You could post the form via ajax and in your php you can return a response to the javascript. That was you could use a callback to fire the new window.
Heres a jquery example:
$.post('/myscript.php', {foo: 'bar'}).done(function (response) {
window.open(......);
});
I believe you are using same page for landing and updating. What you need is to update your echo to add an onload.
echo "<script> window.onload=confirmFunction(); </script>";
Hope this what you are looking for.
<script type="text/javascript">
function confirmFunction(){
var r=confirm("Confirm that you have signed the form!");
if (r==true){
window.open("http://smartpathrealty.com/smart-path-member-page/");
}
else {
}
}
</script>
<?php
if(isset($_POST['submit'])) { //if the submit button is clicked
$complete = 'c';
$query = "UPDATE mytbale SET mycolumn='c' WHERE username='$username'";
mysqli_query($con,$query) or die("Cannot Update");
echo "<script> confirmFunction(); </script>";
}
require_once ("/mypath/include/disconnect_db.php");
?>
<script type="text/javascript">
window.onload = function(){
<?php
if(submitted){
echo "confirmFunction();";
}
?>
}
</script>
Try:
if (r==true) {
window.open("http://smartpathrealty.com/smart-path-member-page/");
}
else {
window.location = "PAGE THAT YOU WANT";
}
<?php if(isset($_SESSION['logged_in'])) {
$somevar= "something";
?>
<script type="text/javascript">
var somevar2 = <?php echo $value; ?>;
</script>
<?php } ?>
I put the code inside <head>, the console shows somevar2 is undefined, I was expecting the script don't run because it put in a scope where it execute only when user is logged in. What is the cause of this problem?
<?php if(isset($_SESSION['logged_in'])) {
$somevar= "something";
?>
<script type="text/javascript">
var somevar2 = <?php echo $somevar; ?>;
</script>
<?php } ?>
That should do the job. My guess is that you just forgot to change the variable echoed.