1

can jQuery or plain JavaScript check how the value of input field was changed? Maybe something similar to that:

$('input').change(function(e){
    e.preventDefault();
    console.log(e.changedFunction.name);
});

I know given code doesn't work and doesn't do what I want. But is this even possible somehow?

Why do I need that? I have a dialog box where I have multiple forms (each form changes one thing). When I submit form, the value resets back to value which was there previously. e.g. In the form there's a word 'Hello', when I change it to 'Hello, World!', it successfully sends the data to $.post, but then resets the value to 'Hello'. I can't seem to find any function, neither php, nor javascript that changes the input. That's why I need to check what or who changes my input value back.

EDIT:

Including sample code.

editblock.php

} else if ($_POST['what'] == 'email') {
$sql = mysql_query("SELECT id, email, loggedin FROM users WHERE id = " . mres($_POST['id']) . " LIMIT 1");
$edit = mysql_fetch_array($sql);
$output .= '<div id="block-' . $_POST['what'] . '"><form method="post" id="form-' . $_POST['what'] . '">';
$output .= '<input type="hidden" name="id" value="' . mres($_POST['id']) .'" />';
$output .= '<input type="text" name="value" value="' . $edit['email'] .'" /> ';
$output .= '<input type="hidden" name="what" value="' . mres($_POST['what']) .'" />';
$output .= '<input type="submit" name="submit" value="OK" />';
$output .= '</form></div>';
$output .= '<script>
  $("#form-' . $_POST['what'] . '").submit(function(event) {
    event.preventDefault(); 
    var $form = $( this ),
        doval = $form.find( "input[name=\"value\"]" ).val(),
        doid = $form.find( "input[name=\"id\"]" ).val(),
        dowhat = $form.find( "input[name=\"what\"]" ).val();

    $.post("/pages/profilis/doedit.php", { do: doval, id: doid, what: dowhat },
      function( data ) {
          $("#block-' . $_POST['what'] . '").empty().append( data );
          $form.find("input[name=\"value\"]").val(doval);
      }
    );
  });
</script>
';
}

doedit.php

else if ($_POST['what'] == 'email') {
if (empty($_POST['do'])) {
    $error[] = 'err';
} else {

    if ( ! preg_match("/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i", $_POST['do'])) {
        $error[] = "err";
    }

    $sql = mysql_query("SELECT `id` FROM `users` WHERE `email` = '" . mres($_POST['do']) . "' LIMIT 1");
    if (mysql_num_rows($sql) == 1) {
        $error[] = "err";
    }

    if ($edit['loggedin'] > 0) {
        $error[] = "err";
    }

    if (sizeof($error) >= 1) {
        echo join($error, '<br/>');

    } else {
        $sql = mysql_query("UPDATE users SET 
                        email = '" . mres($_POST['do']) . "'
                        WHERE id = " .(int)$edit['id'] . "
                        LIMIT 1");
        if ($sql) {
            echo 'OK';
            $logmsg = 'Changed email';
        } else {
            echo 'Error';
        }       
    }
}
} 

PHP function mres() escapes all the characters (for database injection protection - not really important here).

4

1 回答 1

1

根据你说明的情况。我希望你使用 jqueryajax

在此 Post 功能完成后,您可以使用更改后的值更改值

$.ajax({
  type: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
})
  .done(function( msg ) {
    alert( "Data Saved: " + msg ); // portion where you can change the field value to the updated one.
  });

谢谢并恭祝安康,

菲利普·昆朱蒙

于 2013-09-18T12:49:54.030 回答