简体   繁体   English

使用Ajax将动态HTML表单发送到php页面

[英]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. 好了,在坚持了几个小时之后,我终于决定在这里问了。我正在使用ajax发布一个HTML表单,其id为id = {form_id} _form(form_id是动态创建的)。

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.) 在表单中,我只有一个html textarea,其名称也是动态创建的。({form_id} .i:e,如果表单id为92,则textarea值也为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 但是我无法弄清楚我应该在ajax_reply.php页面中获取什么值($ _POST ['需要知道值:('])来获取textarea值并将其提交到数据库中。任何帮助都会非常有用有用

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. 您的textarea应该有一个名称,而不仅仅是一个值,因此它将在您的表格序列化中被选择。

EXAMPLE HTML 范例HTML

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

ajax_reply.php 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 使用mysqli示例,但是您将需要根据数据库语言选择使用等效项

$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 由于名称是为textarea动态生成的,因此我将遍历post数组,根据包含的单词模式对变量进行排序

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM