I have a page in PHP, which is showing user account info from the database, now for editing account info, I have built a bootstrap modal. Now I want the parent page to refresh after we submit the form in modal, so that the page shows updated value from database, how do I do that??
Here is the page code where the modal form is:
<p data-placement="top" data-toggle="tooltip" title="Edit" data-original-title="Edit">
<button class="btn btn-primary btn-xs" data-title="Edit" data-toggle="modal" data-target="#edit">
<span class="glyphicon glyphicon-pencil"> Edit</span>
</button>
</p>
<div class="modal fade" id="edit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Edit Your Detail</h4>
</div>
<div class="modal-body">
<form method="post" action="process.php" class="updateForm">
<div class="form-group">
<input type="hidden" name="userID" value="<?php echo $_SESSION['user']; ?>" />
<label for="customer_name">Customer Name :</label>
<input class="form-control" type="text" name="customer_name" id="customer_name" value="<?php echo $userRow['fullName']; ?>" />
</div>
<div class="modal-footer">
<!-- <input type="submit" name="submit" class="btn btn-block btn-warning" value="Save changes" /> -->
<input type="submit" name="submit" class="btn btn-success" id="submit" />
</div>
</form>
</div>
</div>
</div>
</div>
Here is my custom.js code: Here if I am changing url: "process.php" to url: "index.php" and putting process.php code in index.php file, then the query is working, otherwise the query also is not working.
$("#updateForm").submit(function(event){
// cancels the form submission
event.preventDefault();
submitUpdateForm();
});
function submitUpdateForm(){
$.ajax({
type: "POST",
url: "process.php",
data: $('#updateForm').serialize(),
success : function(text){
if (text == "pass"){
updateFormSuccess();
}
else {
updateFormFailure();
}
},
error: function(){
alert("failure");
}
});
}
function updateFormSuccess(){
alert("Success");
$("#thanks").removeClass( "hidden" );
}
function updateFormFailure(){
$("#thanks").html("Fail");
}
Here is process.php code:
include('config.php'); //include the database connection
if ('POST' === $_SERVER['REQUEST_METHOD']) {
$userID = $_POST['userID'];
$customer_name = $_POST['customer_name'];
$results = mysqli_query($con, "UPDATE users SET fullName='$customer_name' WHERE userId=".$userID);
if ($results) {
echo "pass";
}else{
echo "invalid";
}
}
After troubleshooting I have found that the ajax call is not working at all. Can anybody please guide me what I'm doing wrong?