简体   繁体   中英

Sending Input from a Form via Ajax Request

After following the example and answers by the following threads

jQuery AJAX submit form

submitting a form via AJAX

I have built a similar test form to get to learn the ajax request on submit. Your guess was right, it doesn't work for me (no alert popping up).

My testajax.php with the form:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="../test.js"></script>

<form name="feedback" id="idForm" action="(myurl)/testajax.php" method="post">

<input id="name" type="text">

<input type="submit" name="feedbacksent" value="Send" />
</p>
</form>

My test.js:

// this is the id of the form
$("#idForm").submit(function(e) {

var url = "(myurl)/testajaxinput.php"; // the script where you handle the form input.

e.preventDefault(); // avoid to execute the actual submit of the form.

alert("bla"); // does not work either

$.ajax({
       type: "POST",
       url: url,
       data: $("#idForm").serialize(), // serializes the form's elements.
       success: function(data)
       {
           alert(data); // show response from the php script.
       }
     });

});

My testajaxinput.php that should handle the input:

if (isset($_POST['feedbacksent'])){

echo "<h1>".WORKS."</h1>";  

}

Try this :

if (isset($_POST['feedbacksent'])){
    echo "<h1>".WORKS."</h1>";  
    return true;
}

Then try your alert and also check have you got any error in console.

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