Jump to content

Update Not Working.


Moorcam
 Share

Recommended Posts

Hi all,

I am hopeless with Javascript etc but want to do updates, add, delete etc via a Bootstrap Modal. Everything works fine except for Update.

No errors etc, just nothing happens.

Here is the JS for the Update:

	$(document).on('click','.update',function(e) {
		var id=$(this).attr("data-id");
		var name=$(this).attr("data-name");
		var email=$(this).attr("data-email");
		var phone=$(this).attr("data-phone");
		var address=$(this).attr("data-address");
		$('#id_u').val(id);
		$('#name_u').val(name);
		$('#email_u').val(email);
		$('#phone_u').val(phone);
		$('#address_u').val(address);
	});
	
	$(document).on('click','#update',function(e) {
		var data = $("#update_form").serialize();
		$.ajax({
			data: data,
			type: "post",
			url: "includes/functions.php",
			success: function(dataResult){
					var dataResult = JSON.parse(dataResult);
					if(dataResult.statusCode==200){
						$('#editDriverModal').modal('hide');
						alert('Data updated successfully !'); 
                        location.reload();						
					}
					else if(dataResult.statusCode==201){
					   alert(dataResult);
					}
			}
		});
	});

Here is the php for the Update:

if(count($_POST)>0){
	if($_POST['type']==2){
		$id=$_POST['id'];
		$name=mysqli_real_escape_string($con, $_POST['name']);
		$email=$_POST['email'];
		$phone=$_POST['phone'];
		$address=$_POST['address'];
		$query = "UPDATE `drivers` SET `name`='$name',`email`='$email',`phone`='$phone',`address`='$address' WHERE id=$id";
		if (mysqli_query($con, $query)) {
			echo json_encode(array("statusCode"=>200));
		} 
		else {
			echo "Error: " . $query . "<br>" . mysqli_error($con);
		}
		mysqli_close($con);
	}
}

And finally the Bootstrap form:

	<!-- Edit Modal HTML -->
	<div id="editDriverModal" class="modal fade">
		<div class="modal-dialog">
			<div class="modal-content">
				<form id="update_form">
					<div class="modal-header">						
						<h4 class="modal-title">Edit User</h4>
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
					</div>
					<div class="modal-body">
						<input type="hidden" id="id_u" name="id" class="form-control" required>					
						<div class="form-group">
							<label>Name</label>
							<input type="text" id="name_u" name="name" class="form-control" required>
						</div>
						<div class="form-group">
							<label>Email</label>
							<input type="email" id="email_u" name="email" class="form-control" required>
						</div>
						<div class="form-group">
							<label>PHONE</label>
							<input type="phone" id="phone_u" name="phone" class="form-control" required>
						</div>
						<div class="form-group">
							<label>Address</label>
							<input type="city" id="address_u" name="address" class="form-control" required>
						</div>					
					</div>
					<div class="modal-footer">
					<input type="hidden" value="2" name="type">
						<input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
						<button type="button" class="btn btn-info" id="update">Update</button>
					</div>
				</form>
			</div>
		</div>
	</div>

Data displays fine inside the Modal so it is communicating with the Database. But just when I click on Update nothing happens.

Any help or guidance would really be appreciated.

Thanks in advance...

Link to comment
Share on other sites

5 hours ago, requinix said:

:psychic:


echo "Error: " . $query . "<br>" . mysqli_error($con);

That is not valid JSON. If this gets outputted then the AJAX code will break because JSON.parse() will fail.

Love the Bubble bust lol

No that part is PHP bro. I know you know that.

I did a little more research and for some reason when it is being submitted it is not checking the ID in the WHERE clause.

Link to comment
Share on other sites

Well your code, bro, very clearly includes the id in the WHERE, so...

My point was that if there was an error then you would not get an alert() about it. The code would die with an error in your browser's developer console, which you may or may not be checking.

Link to comment
Share on other sites

8 minutes ago, requinix said:

Well your code, bro, very clearly includes the id in the WHERE, so...

My point was that if there was an error then you would not get an alert() about it. The code would die with an error in your browser's developer console, which you may or may not be checking.

Sorry I was joking with the Bro part.

Ok, it seems that there is an issue with the JS this line:

var dataResult = JSON.parse(dataResult);

The Console keeps pointing to that line.

Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) 

Link to comment
Share on other sites

That's exactly what would happen if the code I pointed out about returning an error was happening.

There's an easy solution to this: when you error, (1) use http_response_code to change the response status to 500, and (2) use an error callback for the AJAX that alert()s the responseText.
Honestly, you should be using http_response_code() for the 200/201 thing as well. Pretty sure that was the original intention too.

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.