Im having trouble passing 2 parameters as variables in a javascript function. myFunction() works if I hard code values as the parameters and it also correctly works if I change the function to only accept 1 variable as its parameter, though it doesn't work with multiple variables as parameter. myFunction() is in index.php and is called from insert.php
index.php
<!DOCTYPE html>
<html>
<head>
<style></style>
</head>
<body>
<form name ="nameForm">
<label>Enter last name: </label><input type="text" name="nameLast" method="post" value="" size="15">
<label>: </label><input type="button" onclick="getTable()" value="submit" size="15"><br>
</form>
<p id="demo"></p>
<p id="demo1"></p>
<script>
function getTable() {
var lastName = document.nameForm.nameLast.value;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("demo").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","insert.php?ln="+lastName,true);
xmlhttp.send();
}
function myFunction(x,y) {
//only works with hard coded values, not variables
document.getElementById("demo1").innerHTML = x.toString() + y.toString();
//works with 1 variable, myFunction(x){
//document.getElementById("demo1").innerHTML = x.toString();}
}
</script>
</body>
</html>
insert.php
<!DOCTYPE html>
<html>
<head>
<style>
table { width: 30%; border-collapse: collapse; }
table, td, th { border: 1px solid black; padding: 5px; }
th { text-align: left; }
</style>
</head>
<body>
<?php
$q = $_GET['ln'];
$con = new mysqli("127.0.0.1", "root", "js", "baseball");
$sql="select Master.nameFirst , Master.nameLast from Master where Master.nameLast = '$q' ";
$result = $con->query($sql);
echo "<table>
<tr>
<th>first name</th>
<th>last name</th>
<th>debut</th>
</tr>";
while($row = $result->fetch_array())
{
echo "<tr>";
//works with 2 hard coded values
echo "<td><a href=javascript:myFunction1(5,3)>". $row[0] ."</a></td>";
//doesn't work with 2 variables as parameters, THIS IS WHAT I WANT!!!!!!!
//echo "<td><a href=javascript:myFunction('$row[0]','$row[1]')>". $row[0] ."</a></td>";
//works with 1 variable, myFunction is changed to 1 parameter and removed + y.toSting()
//echo "<td><a href=javascript:myFunction1('$row[0]')>". $row[0] ."</a></td>";
//works with the other variable
//echo "<td><a href=javascript:myFunction1('$row[1]')>". $row[0] ."</a></td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
Aucun commentaire:
Enregistrer un commentaire