Hi. Im trying to update a page with a form using AJAX but it isn't working. Nothing happens.
I have a table and one of the columns has an anchor tag like this<td><a href='#' value='$name' class='request' onclick='request(".$name.")'>$status</a></td>
so when the user clicks on Request a form opens up via ajax. In the console this appears : Uncaught SyntaxError: missing ) after argument list page.php:64
ive probably gone blind looking at the code for too long coz i can't find anything missing. thanks in advance
This is the AJAX:
<script type="text/javascript">
function request(str)
{
if (window.XMLHttpRequest)
{
// Create the object for browsers
xmlhttp=new XMLHttpRequest();
}
else
{
// Create the object for browser versions prior to IE 7
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
// if server is ready with the response
if (xmlhttp.readyState==4)
{
// if everything is Ok on browser
if(xmlhttp.status==200)
{
//Update the div with the response
document.getElementById("requestForm").innerHTML=xmlhttp.responseText;
$("#cancel-requestform").on("click", function(){
location.reload(true);
});
}
}
}
//send the selected option id to the php page
xmlhttp.open("GET","userRequest.php?request="+str,true);
xmlhttp.send();
$(".requestForm").show();
}
</script>
This is the table:
<table>
<thead>
<th>Name</th>
<th>Username</th>
<th>Number</th>
<th>Action</th>
</thead>
<tbody>
<?php
if(isset($_SESSION['sess_admin_id']))
{
require "connection.php";
$user = $dbh->prepare("SELECT * FROM users ORDER BY name ASC");
$user->execute();
if($user->rowCount() > 0)
{
$user->setFetchMode(PDO::FETCH_ASSOC);
while($rows = $user->fetch())
{
$name = $rows['name'];
$username = $rows['username'];
$number = $rows['number'];
$status = $rows['status'];
echo "<tr><td>$name</td><td>$username</td><td>$number</td><td><a href='#' value='$name' class='request' onclick='request(".$name.")'>$status</a></td></tr>";
}
}
}
?>
</tbody>
</table>
userRequest:
<?php
$request = $_GET['request'];
require "connection.php";
$getrequest = $dbh->prepare("SELECT * FROM users WHERE name = ? LIMIT 1");
$getrequest->bindParam(1, $request, PDO::PARAM_INT);
$getrequest->execute();
if($getrequest->rowCount() > 0)
{
$getrequest->setFetchMode(PDO::FETCH_ASSOC);
while($row = $getrequest->fetch())
{
$name = $row['name'];
$username = $row['username'];
$email = $row['email'];
$phone = $row['number'];
$newname = htmlspecialchars($name,ENT_QUOTES);
$newusername = htmlspecialchars($username,ENT_QUOTES);
$newemail = htmlspecialchars($email,ENT_QUOTES);
echo
"<form method='post' action='userRequest.php'>
<fieldset style='border:none'>
<input type='hidden' value='$name' name='name'>
<div style='height:0;width:100px'><label>Name </label></div>
<div style='margin-left:100px'>
: <label>$newname</label>
</div><br>
<div style='height:0;;width:100px'><label>Username </label></div>
<div style='margin-left:100px'>
: <label>$newusername</label>
</div><br>
<div style='height:0;;width:100px'><label>Email </label></div>
<div style='margin-left:100px'>
: <label>$newmail</label>
</div><br>
<div style='height:0;;width:100px'><label>Phone Number</label></div>
<div style='margin-left:100px'>
: <label>$phone</label>
</div><br>
<input type='submit' name='acceptRequest' class='submit-request' value='Accept Request'>
<input type='button' id='decline-request' value='Decline Request'>
<input type='button' id='cancel' value='Cancel'>
</fieldset>
</form>";
}
}
?>