Passing javascript variable to php function as parameter?
I want to pass JavaScript variables to PHP using onchange event.
When I put the parameter directly like callT(3), it call the PHP function
callT($x) and return the value from PHP db and also print the value using
javascript. It is good.
If I pass the parameter through variable like the below source code, I
can't get the any result. Is there something wrong?
Here is the code:
<script type="text/javascript">
function calldata(){
var c=document.getElementById("bno").value;
var s="".concat("<?PHP callT(",c,"); ?>");
document.getElementById("dn").value= s;
}
</script>
<?PHP
FUNCTION callT($x)
{
$con=mysqli_connect("localhost","root","","books");
// Check connection
if (mysqli_connect_errno($con))
{
$msg = "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
$data = mysqli_query($con,"select * from books where BookId=".$x);
$row = mysqli_fetch_array($data);
echo $row[2];
}
mysqli_close($con);
}
?>
<html>
<body>
<form>
<select name="bno" id="bno" style="width:150px;"
onchange="calldata()">
<option>-</option>
<?php
$con=mysqli_connect("localhost","root","","books");
$data = mysqli_query($con, "SELECT bookid FROm
books");
while($row = mysqli_fetch_array($data))
{
echo " <option>" . $row[0] . " </option>";
}
?>
</select>
<br><br>
<input type="text" id="dn">
</form>
</body>
</html>
Please, help me. Thanks advance.
No comments:
Post a Comment