Run a JavaScript function from a php if [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have to run JavaScript using PHP variables the code have PHP if statement and if true javascript will execute but the script using a second PHP variable.
<?php
if ($time_close_option == 1){
echo '<script type="text/javascript">';
// close the div in $timeoutdiv secs
echo 'window.setTimeout("closePromoSpace();",' <?php echo $timeoutdiv;?>);';
echo 'function closePromoSpace()
{
document.getElementById("promospace").style.display=" none";
};';
echo'</script>';
}
?>

It looks like you're using $timeoutdiv as a string, not a number. Try
<?php if ($time_close_option == 1) { ?>
<script type="text/javascript">
// close the div in $timeoutdiv secs
window.setTimeout(closePromoSpace(), <?php echo $timeoutdiv;?>);
function closePromoSpace()
{
document.getElementById("promospace").style.display=" none";
};
</script>
<?php } ?>
Notice that we can clean the code up a lot by not using the echo statements, but by switching between php and html contexts.

<?php if ($time_close_option == 1)
{
echo '<script type="text/javascript">';
echo 'function closePromoSpace()
{
document.getElementById("promospace").style.display="none";
}';
echo "window.setTimeout(closePromoSpace, $timeoutdiv);";
echo '</script>';
}
?>

<script type="text/javascript">
<?php
if ($time_close_option == 1)
{?>
window.setTimeout(closePromoSpace, '<?php echo $timeoutdiv; ?>');
function closePromoSpace()
{
document.getElementById("promospace").style.display = "none";
}
<?php
} ?>
</script>
Try this one

Related

Redirect page after press OK using sweetalert [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I've tried to redirect but it's not working. Can anyone help me, please?
if(isset($_POST['submit'])){
if(send_report($_POST['id_truck'], $_GET['id'])){
echo '<script type="text/javascript">';
echo 'setTimeout(function () { swal("Success!","data successfully added","success");';
echo '}, 200); window.location.href = 'report.php' </script>';
} else {
echo '<script type="text/javascript">';
echo 'setTimeout(function () { swal("failed!","Something wrong","warning");';
echo '}, 200), ;</script>';
}
}
You are using single quotes in a string wrapped in single quotes, escape them:
echo '}, 200); window.location.href = \'report.php\' </script>';
Edit:
Now as your page is redirecting, your sweetalert will work but you may not be able to see it.
swal() returns Promise which will be resolved when you click on OK button of sweetalert and thus we will have to wait for it to redirect the page thereafter:
echo '<script type="text/javascript">';
echo 'setTimeout(function () {';
echo 'swal("Success!","data successfully added","success").then( function(val) {';
echo 'if (val == true) window.location.href = \'report.php\';';
echo '});';
echo '}, 200); </script>';

Syntax error while passing the values [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am trying to pass the value one form to another. I am getting ") missing" but I am not getting where I am going wrong. Is this the correct way to call all the parameters?
Uncaught SyntaxError: missing ) after argument list
$("#div1").load("http://ppp.gkdsjfgk.com/wp-content/themes/thestory/compare-form-site.php?loanamt=" +
<?php echo $_POST['loanAmt']; ?>."&occupation=" +
<?php echo $_POST['occupation']; ?>."&rateType=" +
<?php echo $_POST['rateType']; ?>."&age=" +
<?php echo $_POST['age']; ?>."&city=" +
<?php echo $_POST['city']; ?> );
You're using the wrong concatenator just after each of your PHP brackets ?>. The dot (the concatenator for PHP) should be replaced with + (the JavaScript concatenator) like this ?> +.
$("#div1").load(
"http://ppp.gkdsjfgk.com/wp-content/themes/thestory/compare-form-site.php?loanamt=" +
<?php echo $_POST['loanAmt']; ?> + "&occupation=" +
<?php echo $_POST['occupation']; ?> + "&rateType=" +
<?php echo $_POST['rateType']; ?> + "&age=" +
<?php echo $_POST['age']; ?> + "&city=" +
<?php echo $_POST['city']; ?>
);
you are mixing PHP and JavaScript concatenation. (.) is used in PHP and (+) is used in JavaScript
concatenation in PHP is like this:
$val = 'Your'.' '.'val';
concatenation in JavaScript is like this:
var val = 'Your'+' '+'val';
so your code should be like this:
$("#div1").load("http://ppp.gkdsjfgk.com/wp-content/themes/thestory/compare-form-site.php?loanamt="+<?php echo $_POST['loanAmt'];?>+"&occupation="+<?php echo $_POST['occupation']; ?>+"&rateType="+<?php echo $_POST['rateType']; ?>+"&age="+<?php echo $_POST['age']; ?>+"&city="+<?php echo $_POST['city']; ?> );
try this one, first make a variable so that it will be easier to modify if there's still wrong, just comment here what's the error after you try this code.
<script>
// set variable
var loadAmt = "<?php echo isset($_POST['loanAmt']) ? $_POST['loanAmt'] : ''; ?>";
var occupation = "<?php echo isset($_POST['occupation']) ? $_POST['occupation'] : ''; ?>";
var rateType = "<?php echo isset($_POST['rateType']) ? $_POST['rateType'] : ''; ?>";
var age = "<?php echo isset($_POST['age']) ? $_POST['age'] : ''; ?>";
var city = "<?php echo isset($_POST['city']) ? $_POST['city'] : ''; ?>";
// your load variable
var loadVar = "http://ppp.gkdsjfgk.com/wp-content/themes/thestory/compare-form-site.php?loanamt="+loadAmt+"&occupation="+occupation+"&rateType="+rateType+"&age="+age+"&city="+city;
// load to specified div
$('#div1').load(loadVar);
</script>

json_encode string php to javascript but when use it in javascript got null [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
script
<script type="text/javascript" >
var myvar = <?= json_encode($str); ?>;
alert(myvar);
</script>
php
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<?
$str = "ABCD";
echo "ENCODING: " . mb_detect_encoding($str) . "\n";
?>
im not good at english sorry.
$str = "ABCD"
i want to send string from php to javascript
var myvar = <?= json_encode($str); ?>;
i use json_encode but when i alert no massage display in alert-box(box happen but no massage there maybe null value)
alert(myvar);
i dont know what happen please help me
PS.alert for test string i want to use that string in javascript code
There are some issues with your code try to fix those as below :
Put the second <script> tag after PHP blocks.
Replace <? ?> blocks with <?php ?> or <?= ?>
Like so :
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<?=
$str = "ABCD";
echo "ENCODING: " . mb_detect_encoding($str) . "\n";
?>
<script type="text/javascript" >
var myvar = <?= json_encode($str); ?>;
alert(myvar);
</script>
Here are 2 flaws in your code
syntex to use php is <?php $str = "ABCD"; ?> not <?$str = "ABCD"?>
echo "ENCODING: " . mb_detect_encoding($str) . "\n"; is giving error , so it will not run
PHP code :-
<?php
$str = "ABCD";
$message = json_encode($str);
?>
Javacript code:-
<script type="text/javascript" >
var json = <?= json_decode($message); ?>;
var obj = JSON.parse(json);
alert(obj);
</script>

Javascript not working on a php while cycle [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to execute this javascript code on each database table record.
I have try to define all the div id on the script, but since i am not good a javascript i can not find where is the problem.
I think that javascript should be specified with a uniq name... heres the code:
echo '<script>';
echo '$(document).ready(function(){';
echo 'var menu = $("#shfaqa'.$row["id"].'")';
echo '$("#butoni'.$row["id"].'").click(function(event){';
echo 'event.preventDefault();';
echo 'event.stopPropagation();';
echo 'if (menu.is(":visible"))';
echo '{';
echo 'menu.slideUp(400);';
echo 'jwplayer( "my-video'.$row["id"].'" ).stop();';
echo '}';
echo 'else';
echo '{';
echo 'menu.slideDown(400);';
echo '}';
echo '});';
echo '$(document).not("#shfaqa'.$row["id"].', #butoni'.$row["id"].'").click(function(event) {';
echo 'event.preventDefault();';
echo 'if (menu.is(":visible"))';
echo '{';
echo 'menu.slideUp(400);';
echo 'jwplayer( "my-video'.$row["id"].'" ).stop();';
echo '}';
echo '});';
echo '})';
echo '</script>';
I think you're missing a ; at line 3. This should be better:
echo 'var menu = $("#shfaqa'.$row["id"].'");';
And you're missing a semicolon at the very end as well:
echo '});';
echo '});';
echo '</script>';
But this whole concept has some issues as Implant said
And you could write this whole thing by just using one single echo. Much clearer, isn't it?
echo "<script>
$(document).ready(function(){
var menu = $(\"#shfaqa{$row["id"]}\");
$(\"#butoni{$row["id"]}\").click(function(event){
event.preventDefault();
event.stopPropagation();
if (menu.is(\":visible\"))
{
menu.slideUp(400);
jwplayer( \"my-video{$row["id"]}\" ).stop();
}
else
{
menu.slideDown(400);
}
});
$(document).not(\"#shfaqa{$row["id"]}, #butoni{$row["id"]}\").click(function(event) {
event.preventDefault();
if (menu.is(\":visible\"))
{
menu.slideUp(400);
jwplayer( \"my-video{$row["id"]}\" ).stop();
}
});
});
</script>";
You could set the full javascript in a regular HTML format and echo only the PHP values you need like :
<?php
// any PHP condition here
if($something) {
?>
<script>
var something = <?php echo $row["id"]; ?>;
// more javascript here
</script>
<?php
}; // close php if
?>

show and hide elements using mysql enum and javascript in php?

I am trying to simply show and hide an element depending on the value of an ENUM in mysql database.
I am using the javascript and PHP but it seems like the javascript within PHP does not or cannot select element given as the element is always on display!
here is my php code:
if ($sizeSelect != '1') {
echo '<script type="text/javascript">';
echo 'document.getElementById("sizeSelect").style.display = "block"';
echo '</script>';
}else{
echo '<script type="text/javascript">';
echo 'document.getElementById("sizeSelect").style.display = "none"';
echo '</script>';
}
and this is my HTML element:
<select id="sizeSelect" name="sizeSelect">
<option>Small</option>
<option>Large</option>
</select>
is there anything that I'm missing?
any help would be appreciated.
Thanks
P.S. I have made sure that I am connected to mysql database and get the ENUM value properly so the mysql connection is not an issue here.
okay, I just did a test within my html file and placed the following code at the top of the page and still didn't work but when I put the same code at the bottom of the page, it did work and changed the element's display to none:
<script language='javascript' type='text/javascript'>
document.getElementById('sizeSelect').style.display = 'none';
</script>
SO, i did try this code in my PHP file with document ready function but still doesn't work from php file!
if ($sizeSelect = 1) {
echo "<script language='javascript' type='text/javascript'>";
echo
"$(document).ready(function(){ document.getElementById('sizeSelect').style.display = 'block';});";
echo "</script>";
}else{
echo "<script language='javascript' type='text/javascript'>";
echo "document.getElementById('sizeSelect').style.display = 'none';";
echo "</script>";
}
any help would be great.
Try to put your php code that generating the javascript after your SELECT tag or at the end of file, because your javascript doesn't find the DOM element "#sizeSelect", i made test and it worked for me. like this:
<select id="sizeSelect" name="sizeSelect">
<option>Small</option>
<option>Large</option>
</select>
<?php
$sizeSelect = // retrieve sizeSelect value from your database;
if ($sizeSelect != '1') {
echo '<script type="text/javascript">';
echo 'document.getElementById("sizeSelect").style.display = "block"';
echo '</script>';
}else{
echo '<script type="text/javascript">';
echo 'document.getElementById("sizeSelect").style.display = "none"';
echo '</script>';
}
?>
After you specified that you're using Smarty, i come back with a solution for that:
Concatenate your js content produced in php file on a string variable instead of using echo, and assign its value with Smarty assign() function
$jsContent= '';
if ($sizeSelect != '1') {
$jsContent.= '<script type="text/javascript">';
$jsContent.= 'document.getElementById("sizeSelect").style.display = "block"';
$jsContent.= '</script>';
}
else{
$jsContent.= '<script type="text/javascript">';
$jsContent.= 'document.getElementById("sizeSelect").style.display = "none"';
$jsContent.= '</script>';
}
$smarty->assign('js', $jsContent);
Display the variable content as i said before, just after the select tag being targeted
<select id="sizeSelect" name="sizeSelect">
<option>Small</option>
<option>Large</option>
</select>
{$js}
I think you should have a div in which select will work, use Div for hide and show, see below
Code for HTML:
<div id ="sizeSelect">
<select id="select_id" name="select_id">
<option>Small</option>
<option>Large</option>
</select>
</div>
and for php
if ($sizeSelect) {
echo '<script type="text/javascript">';
echo 'document.getElementById("sizeSelect").style.display = "none"';
echo '</script>';
}else{
echo '<script type="text/javascript">';
echo 'document.getElementById("sizeSelect").style.display = "block"';
echo '</script>';
}
This worked for me:
if ($sizeSelect != 1) {
echo '<script language="javascript" type="text/javascript">';
echo
'$( document ).ready(function() { $( "#sizeSelect" ).hide();});';
echo "</script>";
}else{
echo "<script language='javascript' type='text/javascript'>";
echo
"$( document ).ready(function() { $( '#sizeSelect' ).show();});";
echo "</script>";
this is jQuery by the way.

Categories

Resources