Home > Software engineering >  JS Get Input Value
JS Get Input Value

Time:09-15

I am trying to get the value of an input text field, so I can have a SQL query to change its password. For some reason using the default values provided on the JQuery website e.g. name = "John", time = "2pm". I want to send two strings to get changed, but for some reason A) there is no error or B) any idea if it actually makes it to the test file. Any help would be great!

$("#password_change").submit(function(event) {
  var $form = "#password_change";
  $collar = $form.find("input[name='collar']").val(),

    $.post("test.php", {
      name: $collar,
      time: "2pm"
    })
    .done(function(data) {
      alert("Data Loaded: "   data);
    });
});

CodePudding user response:

Consider the following.

$("#password_change").submit(function(event) {
  event.preventDefault();

  var $form = $(this);
  var dt = new Date();
  var hour = dt.getHours();
  var mer = (hour > 11 ? "PM" : "AM");

  if (hour > 11 && hour != 12) {
    hour = hour - 12;
  }

  var $collar = $("input[name='collar']", $form).val();
  var myData = {
    name: $collar,
    time: hour   mer
  };
  console.log(myData);

  /*
  $.post("test.php", myData)
    .done(function(data) {
      alert("Data Loaded: "   data);
    });
  });
  */
  return false;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="password_change">
  <input name="collar" type="hidden" value="Tim" />
  <input type="password" placeholder="New Password" />
</form>

You will want to preventDefault() on the Form Submission. This ensures it does not try to submit the data regularly.

A few minor changes to help improve the script.

  • Related