I am using following code to pass PHP variables to javascript. but it is not working.
function gto(str) {
document.getElementById('goto').action = str;
document.getElementById('ID').value = <?php echo "$userid" ?>;
document.getElementById('name').value = <?php echo "$user_name" ?>;
document.getElementById('gname').value = <?php echo "$usergname" ?>;
document.getElementById('fmname').value = <?php echo "$userfname" ?>;
document.getElementById('img').value = <?php echo "$userimg" ?>;
document.getElementById('email').value = <?php echo "$useremail" ?>;
document.getElementById('goto').submit();
}
Following is the PHP code
<?php
if($_POST["name"] == null)
{
$user_name = 'Annomyous';
}
else{
$user_name = $_POST["name"];
$userid= $_POST["id"];
$usergname= $_POST["gname"];
$userfname= $_POST["fname"];
$userimg= $_POST["img"];
$useremail= $_POST["email"];
}
echo "<p style='color : white'>$user_name";
echo "$userid" ;
echo "$gname";
echo "$fname";
echo "$img";
echo "$email";
echo "$user_name";
echo "$user_name</p>";
$user_name =htmlspecialchars($user_name);
$user_name =str_replace("<script>","", $user_name);
?>
the output is a follows:
ReAlItY TuTs104598758504708047866ReAlItY TuTsReAlItY TuTs//this is php echo output.
JAVASCRIPT OUTPUT:-
function gto(str) {
document .getElementById('goto').action = str;
document.getElementById('ID').value = ;
document.getElementById('name').value = Annomyous;
document.getElementById('gname').value = ;
document.getElementById('fmname').value = ;
document.getElementById('img').value = ;
document.getElementById('email').value = ;
document.getElementById('goto').submit();
}
Function gto is called here:
<button class="w3-btn header-btn" onclick="gto('Contact.php');">Contact Us</button>
I can see in PHP output I am getting all variable output. but nin juavascript im getting only Annonymous why???? I need to pass post variables to contact us so i am using the form tag and javascript but this is not working Please help me! Thanks in Advance
-
you can pass php variable value javascript as var variablename = <?= $phpvariablename ?>Devsi Odedra– Devsi Odedra2018年04月28日 04:02:14 +00:00Commented Apr 28, 2018 at 4:02
-
You have way too many empty strings in the output - it looks like you need to fix the initial communication between the form and your endpoint first.CertainPerformance– CertainPerformance2018年04月28日 04:03:11 +00:00Commented Apr 28, 2018 at 4:03
-
@CertainPerformance w3schools.com/code/tryit.asp?filename=FQS9PAD2HFWFKhushit Shah– Khushit Shah2018年04月28日 04:06:35 +00:00Commented Apr 28, 2018 at 4:06
-
Thanks for the upvote! I can now comment!Kento Nishi– Kento Nishi2018年06月06日 20:05:04 +00:00Commented Jun 6, 2018 at 20:05
-
1I already did! It had 1 negative vote so I cancelled it out.Kento Nishi– Kento Nishi2018年06月25日 22:30:16 +00:00Commented Jun 25, 2018 at 22:30
2 Answers 2
values from php to js can be passed in many ways, here's one of them
try to pass the php values when calling the js function, like this:-
<button onclick="gto('Contact.php','<?php echo $userid;?>','<?php echo $username;?>');">Contact Us</button>
and then get values on js function like this:-
function gto(str,userid,username) {
document .getElementById('goto').action = str;
document.getElementById('ID').value = userid;
document.getElementById('name').value =username;
}
that's it, now use the values in js as your wish
1 Comment
Uh, found another issue. The js is in smart quotes, which won't work... "`" is invalid. use "'". Also, w3schools can't handle php in their editor, so it's no use. Example for POST requests: https://www.w3schools.com/code/tryit.asp?filename=FQSA8MJYGJ47
Hope this helps.