2

I'm very new to ajax so I followed a tutorial but I can't get it to work. I tried search this forum for an answer but with no luck..

HTML (a bit stripped down from classes and bootstrap-stuff)

<form id="editUserForm" role="form">
  <input id="edit_employeenr" type="text" name="employeenr">
  <input id="edit_name" type="text" name="name">
  <select id="edit_membertype" name="membertype">
    <option value="1">Admin</option>
    <option value="2">Employee</option>
  </select>
  <input type="submit" value="Save">
</form>
<div id="editUserMsg">Successfully updated!</div>

JS

$(document).ready(function() {
  $("#editUserMsg").hide();

  $("#editUserForm").submit(function(event) {
    event.preventDefault();
    submitUserEdit();
  });

  function submitUserEdit(){
    var dataString = $("#editUserForm").serialize();

    $.ajax({
        type: "POST",
        url: "user_edit_process.php",
        data: dataString,
        success: function(text){
          if (text == "success"){
            userEditSuccess();
          }
        }
    });
  }

  function userEditSuccess(){
    $("#editUserMsg").show().delay(5000).fadeOut();
  }
});

PHP (user_edit_process.php)

<?php
  $employeenr = $_POST['employeenr'];
  $name = $_POST['name'];
  $membertype = $_POST['membertype'];

  $stmt = $link->prepare("UPDATE users SET employeenr = ?, name = ?, membertype = ?");
  $stmt->bind_param("isi", $employeenr, $name, $membertype);
  $stmt->execute();

  if ($stmt) {
    echo 'success';
  } else {
    echo 'fail';
  }
?>

if i put the $("#editUserMsg").show().dealy(5000).fadeOut(); just above the $.ajax the message appears, so that must mean that the ajax code isn't working correct. Any suggestions?

EDIT Solved. I had forgotten to include the file where the variable $link wes defined.

17
  • <div id="editUserMsg> missing double quote Commented May 8, 2016 at 9:44
  • Only missed it here on the question, has double quote in the original code, thanks for notice! @MedetAhmetsonAtabayev Commented May 8, 2016 at 9:45
  • is it that 's' in $s.ajax also a typo? Commented May 8, 2016 at 9:51
  • yes it is! @ᴀʀᴛᴜʀғɪʟɪᴘɪᴀᴋ Commented May 8, 2016 at 9:59
  • Check the error from the ajax, just after the success parameter add this: error: function (jqXHR, textStatus, errorThrown) { alert(textStatus); } Commented May 8, 2016 at 10:39

2 Answers 2

1

It seems that you have a problem in your either in your prepare statement or in the bind_parameter. You should always check for error, so I suggest you do like this to check for errors:

<?php
    $employeenr = $_POST['employeenr'];
    $name = $_POST['name'];
    $membertype = $_POST['membertype'];

    if (!($stmt = $mysqli->prepare("UPDATE users SET employeenr = ?, name = ?, membertype = ?"))) {
        echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
    }

    if (! $stmt->bind_param("isi", $employeenr, $name, $membertype)) {
        echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
    }

    if (!$stmt->execute()) {
        echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
    }
?>

and then in you JS, add this to your success method:

console.log(text);

Check your Firefox console (Ctrl-Shift-Q), and if there is an error you would find it under "Network" -> Click the "user_edit_process.php" in the list -> and in the right window under "Preview".

Sign up to request clarification or add additional context in comments.

1 Comment

This solved the case, even tho I thought my PHP code were just fine. I moved the code to a different file and forgot to make the link to my database where I get the $link from, so that's why I wasn't able to create any connection. Thank you!
1

Is that all code that you have in user_edit_process.php?

Is the $link variable properly initialize?

You can try to comment part of your code in PHP file, and write something like below to test if you Ajax code work properly:

<?php
  $employeenr = $_POST['employeenr'];
  $name = $_POST['name'];
  $membertype = $_POST['membertype'];

  // $stmt = $link->prepare("UPDATE users SET employeenr = ?, name = ?, membertype = ?");
  // $stmt->bind_param("isi", $employeenr, $name, $membertype);
  // $stmt->execute();

  if ($employeenr) {
    echo 'success';
  } else {
    echo 'fail';
  }

And then if you type something in first employeenr form input it should show Successfully updated!. If you leave this input empty and send form, it shouldn't show.

3 Comments

Nothing wrong with the PHP, it works like a charm if I use the <form action="user_edit_process"....> instead of using ajax. But I will still try this to make sure I haven't done anything with my PHP since last time it worked.
I changed the if($stmt) to $employeenr but still nothing. I'm using a server from our school for testing this, and I know that this server doesn't allow the email($sendto, $message... etc) maybe it blocks ajax?
And what about JavaScript console output in your browser? Does it show some errors, when you dispaly your form and when you send it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.