How to intercept ajax call server side

So I had a look at this. I’ve never tried jQuery, but it’s one of those things that looks like it might be useful. So basing some stuff on your code, and the code that others have suggested in the other thread you opened, here’s what I have got for the html code, a very basic form just to see how things work:

<html>
<head>
<script type="text/javascript" src="jquery-1.12.3.js"></script>

</head>
<body>

<h1>test</h1>

<div id="form-div">

<form method="post" action="">
Email: <input type="text" name="emailaddr">
Password: <input type="password" name="pwd">
<input type="submit">
</form>
</div>
<script>

  $('#form-div form').submit(function(e){
    e.preventDefault();
    var formData = $(this).serialize();
    $.post("jq1.php", formData).done(function(data){console.log(data);
    });
});

</script>
</body>
</html>

And I’ve got this very small bit of PHP, again just to see what happens

<?php
var_dump($_POST);
?>

So a few things I’ve learned from this:

  1. You don’t need to unserialize the data when it arrives at the PHP code
  2. The PHP code can output what it likes, and your browser won’t see it. It will appear in something like Chromelogger though.
  3. I used the separate line to grab the data as someone wrote in the other thread. Partly because that makes it easier to read in my opinion. Editing it back to your $(“form-div form”) version works too.

So, your issue with the test script might be down to point two, that the PHP code just doesn’t output stuff to the browser when you’ve called it in this way. Or it might be the URL.

Grateful that you have stuck around till now. I eventually did some restructuring to the PHP code I posted earlier and it now runs and logs errors in the error log file. But variables from this point on refused to run meaning that SQL is wrong so I guess if anyone can help me correct it, we’ll be home and dry.

Missing opening single-quote before $last_name in the values section. Though you should look at prepared statements to make it nicer and would deal with quotes for you.

The Ajax code correctly displays the “email already registered” error when I input a preregistered email. But the bad news is, it also displays any output from the script if the email hasn’t been registered. Here’s my Ajax

$("#email").change(function(){
$.post('sign_up.php', $("#form-div form").serialize
()).done(function(c) {
console.log(c);
if ($(c).attr("id", "errorText") && typeof
($('#errorText')) != 'undefined') {
$('#emailReport').html(c);
} else $('#emailReport').html("");
});
});

Hmmmmm … Lemmi give prepared statements a quick shot. In all this mêlée it didn’t occur to me.

Eagle eyes! :eyes:
I tried using prepared statements but it didn’t work so I’m sticking to the regular queries. Here’s my prepared statement

if ($prep = $conn->prepare("INSERT INTO confirm VALUES(?, ?, ?, ?, ?, ?, ?, ?)")) {
				$prep = $prep->bind_param("ssssssss", $first, $last, $mail, $pwd, $uname, $sud, $ll, $c_id);
				
				$first = $first_name;
				$last = $last_name;
				$mail = $email;
				$pwd = $password;
				$uname = $username;
				$sud = date("Y-m-d H:i");
				$ll = $sud;
				$c_id = $confirm_id;
				
				$prep->execute();

It logs error Call to a member function execute() on boolean meaning the query to the table returned false and I don’t know why.

Your problem there is that you’ve re-used $prep in the second line of your code, which was your pointer to the prepared statement. The result from bind_param() overwrites the pointer with either true or false, which causes a problem when you go on to call execute() on the same variable, so change it to something else.

$prep_bp = $prep->bind_param("ssssssss", $first, $last, $mail, $pwd, $uname, $sud, $ll, $c_id);

And then check for $preb_bp being false to see whether it worked or not.

1 Like

It worked. Thank you.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.