Syntax error while passing the values [closed] - javascript

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>

Related

How can i add two variables between single quotes

I have
function delImage(posteId,imagebinId) {
$.get("<?php echo base_url('/Home/deletePostimage/') ?>");
return false;
}
i want to be like /Home/deletePostimage/posteId/imagebinId
i tried
$.get("<?php echo base_url('/Home/deletePostimage/')+posteId+"/"+imagebinId ?>");
but it divide the two numbers
like posteId=5 imagebinId =2
the results will be
/Home/deletePostimage/2,5
the params from
<a class="postimgsd" onClick="if(confirm('Are you sure you want to delete this image ?')){delImage(<?php echo $value['id'],$get_images[0]['binId']; ?>); rmItem(<?php echo $i; ?>);}else{} ; " >
With ES6, you can do this with string interpolation:
$.get(`<?php echo base_url('/Home/deletePostimage/${postId}/${imagebinId}') ?>`);
Without ES6, there is another answer that answers it, or use this:
var url = '/Home/deletePostimage/' + postId + '/' + imagebinId;
$.get("<?php echo base_url('" + url + "') ?>");
Furthermore, the code that calls this function should actually be:
<a class="postimgsd" onClick="checkDeletion()" />
<script type='text/javascript'>
function checkDeletion() {
if(confirm('Are you sure you want to delete this image ?')) {
var postId = <?php echo $value['id']; ?>;
var imagebinId = <?php echo $get_images[0]['binId']; ?>;
delImage(postId, imagebinId);
rmItem(<?php echo $i; ?>);
}
}
</script>
You need to keep in mind that PHP is processed on back-end before send the html to your user browser. That said, everything between php tags (<?php ... ?>) is rendered/processed before javascript even exists.
So if you put any JavaScript code inside your PHP code it won't work.
To make it work and be clean, do something like this:
function delImage(posteId,imagebinId) {
let url = "<?php echo base_url('/Home/deletePostimage/') ?>";
$.get(url + posteId + "," + imagebinId);
return false;
}

Referencing a defined html variable in JS

My index.php page includes a config.php file, which returns an array that I have defined some variables in by using "define('var1' , 10)".
I am trying to validate my forms input, but I can't figure out how I can reference var1 from within the JS function. What is the easiest way to do this?
Just echo it to a javascript variable:
<script type="text/javascript">
var var1JS = "<?php echo $var1; ?>";
</script>
Not quite sure I am full understanding without seeing the code but, you could echo the variable from the PHP array in the JS function (as above answer).
Or Echo the entire JS query:
$y = count($PHPdata_array);
echo "function exampleFunction() {";
echo "var ArrayName = [";
for ($i = 1; $i <= $y; $i+=2) {
echo "{" . $PHPdata_array[$i] . "," . $PHPdata_array[$i-1] . "},";
}
echo "];";

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
?>

How to retrieve $_SESSION value in a JavaScript/HTML

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.

Categories

Resources