简体   繁体   中英

Sending a dynamic html form to a php page using ajax

Ok after hours of stucking in the issue,I have finally decided to ask it here.I am posting a html form whose id is id={form_id}_form (form_id is dynamically getting created) using ajax.

In the form ,I have only one html textarea whose name is also dynamically getting created.({form_id}.i:e if form id is 92 then the textarea value is also 92.)

However I am not able to figure out what value I should fetch in ajax_reply.php page($_POST['need to know the value :( ']) to get the textarea value and submit it in my database.Any help will be highly useful

function test(form_id){
var url = "ajax_reply.php"; // the script where you handle the form input.

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

Your textarea should have a name, not just a value, so then it is picked up within your form serialize.

EXAMPLE HTML

<textarea id="form_id" name="my_textarea"></textarea>

ajax_reply.php

$textArea = $_POST['my_textarea'];

If you are writing this to a database then you should escape it also

EXAMPLE using mysqli , but you will need to use the equivalent depending on your DB language selection

$textArea = mysqli_real_escape_string($con, $_POST['my_textarea']);

UPDATE

As the name is dynamically generated for the textarea, I would loop through the post array, sorting the variables based on patterns of words contained

foreach ($_POST as $key => $value) {
    // Do something with $key and $value
} 

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