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
?>
Related
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>
I need to pass a javascript array, via POST, to a PHP file.
I've tried to semplify my original business trying to explain my troubles ...
This is my first PHP file, in which I declare a javascript array and I set each element to 1 value (not a great business I know, but it doesn't matter, it's only to explain ....)
<?php
echo '<form action="testPhp-2.php" method="POST" target="testPhp-2">';
echo '<script type="text/javascript">';
echo 'var arr_selections = [];';
echo 'for(i=0; i < 10; i++)';
echo ' {';
echo ' arr_selections[i] = 1';
echo ' }';
echo 'arr_selections_json = JSON.stringify(arr_selections);';
echo 'alert (arr_selections[2]);';
echo 'document.write("<br/>");';
echo ' ';
// echo 'document.write("<input type="hidden" />");';
echo 'document.write("<input type=\"hidden\" name=\"arr_selections_json\" value=\"arr_selections_json\" />");';
echo ' ';
echo '</script>';
echo ' <input type="submit" value="Controlla">';
echo ' </form>';
?>
.... and here you are the code of testPhp-2 file ...
<?php
if(isset($_POST['arr_selections_json']))
{
echo "OK, array exist !! ";
echo '</br>';
echo '</br>';
$arr_selections = json_decode($_POST['arr_selections_json'], true);
echo $arr_selections[0];
}
else {
echo "NO; array does not exist !! ";
echo '</br>';
echo '</br>';
}
?>
Now, if you try to execute the code you'll see the OK, array exist !! message but no array value is printed about the echo $arr_selections[0]; line of code in testPhp-2.php file.
Any suggestion will be appreciated! Thank you in advance!
Cesare
Problem is that you're setting the value of the input to the litteral string "arr_selections_json" instead of to the contents of that variable.
Change
echo 'document.write("... value=\"arr_selections_json\" />");';
To
echo 'document.write("... value=\""+arr_selections_json+"\" />");';
I have this working script yet when I change it to retrieve(supposedly) the value inside $_SESSION["username"], it doesn't retrieve anything. The whole page is saved as .php as I am using some codes that uses PHP.
Code:
echo "<script type=text/javascript>";
echo "var hidden = false;";
echo "function actiondb1() {";
echo "if(!hidden) {";
echo "document.getElementById(\'clickdb1\').style.visibility = \'hidden\';";
echo "document.getElementById(\'db1\').style.visibility = \'visible\';";
echo "document.getElementById(\'db1\').disabled = false;";
echo "document.getElementById(\'db1\').value =".$_SESSION["username"];.";";
echo "}";
echo "}";
echo "</script>";
How can I make the script to properly retrieve the data inside $_SESSION["username"];?
Observe that, for instance, if the value of $_SESSION["username"] is John, your echo will be generating this result:
document.getElementById('db1').value = John;
But John is supposed to be a string and should be wrapped in quotation marks, otherwise JavaScript will understand it as a variable name (which value will be probably undefined).
As Havenard mentioned, this line is missing Javascript single quotes to properly denote a string variable:
echo "document.getElementById(\'db1\').value ='".$_SESSION["username"];."';";
However, you really shouldn't print JS out with PHP if you can help it. Though iatboy's answer answer won't ultimately fix your bug, it is a much cleaner way of doing things.
?>
<script type=text/javascript>;
var hidden = false;
function actiondb1() {
if(!hidden) {
document.getElementById('clickdb1').style.visibility = 'hidden';
document.getElementById('db1').style.visibility = 'visible';
document.getElementById('db1').disabled = false;
document.getElementById('db1').value ='<?php echo $_SESSION["username"];?>';
}
}
</script>;
<?php
Did you start session in this page?If you didn't,use the follow code to start session.
session_start();
Then change your code to
echo "<script type=text/javascript>";
echo "var hidden = false;\n";
echo "function actiondb1() {\n";
echo "alert('".$_SESSION['username']."')\n"; //test code
echo "if(!hidden) {\n";
echo "document.getElementById('clickdb1').style.visibility = 'hidden';\n";
echo "document.getElementById('db1').style.visibility = 'visible';\n";
echo "document.getElementById('db1').disabled = false;\n";
echo "document.getElementById('db1').value ='".$_SESSION["username"]."';\n";
echo "}\n";
echo "}\n";
echo "</script>";
it will be ok.
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.
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