简体   繁体   中英

Jquery ajax not call not working

I have 2 files in my directory: one is js/form-validation-reg.js and one is HTML/registeruser.php . The javascript file validates a form from another html file which I have already checked; all values are passed all the way till the ajax but the values does not seem to be send to registeruser.php to be send in my database.

form-validation-reg.js:
//data string creation
var dataString = 'name='+ name
                    + '&pass=' + pass     
                    + '&nationality=' + nationality
                    + '&contactno=' + contactno
                    + '&dateofbirth=' + dateofbirth
                    + '&eaddress=' + eaddress
                    + '&address=' + address
                    + '&gender=' + gender
                    + '&monthlysub=' + monthlysub;      
        //ajax
        $.ajax({
        type:"POST",
        url: "HTML/registeruser.php",
        data: dataString,
        success: success(),
           error:function(jqXHR, textStatus, errorThrown){
                               alert("Error type" + textStatus + "occured, with value " + errorThrown);
                           }

    });
});  

no errors is displayed and i have also tried setting the url as "../HTML/registeruser.php" but it still doesn't work. PHP file(NOTE:i have also made sure my database details are correct.):

$name = stripslashes(strip_tags($_POST['name']));
$pass = stripslashes(strip_tags($_POST['pass']));
$nationality = stripslashes(strip_tags($_POST['nationality']));
$contactno = stripslashes(strip_tags($_POST['contactno']));
$dateofbirth = stripslashes(strip_tags($_POST['dateofbirth']));
$eaddress = stripslashes(strip_tags($_POST['eaddress']));
$address = stripslashes(strip_tags($_POST['address']));
$gender = stripslashes(strip_tags($_POST['gender']));
$monthlysub = stripslashes(strip_tags($_POST['monthlysub']));


$mysqli = new mysqli("localhost", "root", "","testdb")or exit("Error connecting to the database.");
$stmt = $mysqli->prepare("INSERT INTO user 
            (name, password, nationality, contactno, dateofbirth, email, address, gender, monthlysub) 
            VALUES (?,?,?,?,?,?,?,?,?)");
$stmt->execute();
$stmt->bind_param("sssssssss", $name, $pass, $nationality, $contactno, $dateofbirth, $eaddress, $address, $gender, $monthlysub);
$stmt->fetch();
$stmt->close();
$mysqli->close();

try:

success: success,

instead of :

success: success(),

Not sure if this might help, but your dataString variable looks like you're building it to go into a URL which would use get instead of post. It could be that your php script doesn't know how to parse what's getting passed to it because it's looking in the post variables instead. Since you're passing a password you wouldn't want to use get for security reasons though.

You need to be calling the $stmt->bind_param before the $stmt->execute();

If there is still an issue then you need to break it down to see what is given you the issue:

1) Ensure that the Javascript is getting called. Put an alert() in your Javascript and reload the page. If the alert pops up then your are fine there and you can rule out any include URL issues.

2) If you are using Firefox, install firebug and then hit F12 and view the console. You will see the HTML/registeruser.php getting called and you will also be able to see the parameters that are getting sent and other info. You can also do this in Chrome. If the URL is getting called and you are receiving a 200 response then there are no problems there and that is one more thing you can rule out.

3) At this point we have ruled out the Javascript and the Ajax call. So now we focus on the Database connection and then the SQL query. After your connection on the registeruser.php do a select statement of a table that you know has records, echo them out to ensure you definitely have a connection and that you are connected to the correct database.

4) Echo your SQL statement to ensure that it has all the parameters.

By the way, you should also check your error handling, instead of using exit you should be checking for the mysqli error so after the new mysqli call then add this:

if (mysqli_connect_errno()) {
  printf("Error connecting to the database: %s\n", mysqli_connect_error());
  exit();
}

You may use a library that does that automatically, like http://phery-php-ajax.net already do the bridge between jQuery and PHP automatically

phery.remote('registeruser', {
'name': name,
'pass': pass,     
'nationality': nationality,
'contactno': contactno,
'dateofbirth': dateofbirth,
'eaddress': eaddress,
'address': address,
'gender': gender,
'monthlysub': monthlysub,
});
Phery::instance()->set(array(
    'registeruser' => function($data){
      $data = array_map('strip_tags', $data);
      /* rest of mysql code */
      return PheryResponse::factory(); // do any DOM manipulation, redirect, etc
    }
))->process();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM