dongzhi2014 2013-02-19 22:39
浏览 44

用javascript形成一个数组,然后将数组发送到php以提交到数据库

I am trying to form a javascript function from this form:

<form class="responseForm" action="javascript:parseResponse123<?php echo $id; ?>()" id="responseForm<?php echo $id; ?>"> 
<input name="hiddenField5<?php echo $id; ?>" id="hiddenField5<?php echo $id; ?>" type="hidden" value="<?php echo $id; ?>" />
<input name="hiddenField4<?php echo $id; ?>" type="hidden" id="hiddenField4<?php echo $id; ?>" value="<?php echo $qty; ?>" />

<font color="red"><?php echo $val; ?></font><br />
<?php
$i=1;
while($i<=$qty)
{
?>
Participant <?php echo $i; ?>:<input name="hiddenField2[]" id="hiddenField2" type="text" /><br />
<?php  $i++;
 }
 ?>
<input name="submit" type="submit" value="Submit" />
</form>

the participant input field(hiddenfield2) can be anywhere from 1 or more of the field by same name and id.

I then have this code for submission to php page:

function parseResponse123<?php echo $id; ?> () 
{
var hiddenField2<?php echo $id; ?> = $("#hiddenField2<?php echo $id; ?>");
var hiddenField4<?php echo $id; ?> = $("#hiddenField4<?php echo $id; ?>");
var hiddenField5<?php echo $id; ?> = $("#hiddenField5<?php echo $id; ?>");
var hiddenField1<?php echo $id; ?> = $("#hiddenField1<?php echo $id; ?>");

var url = "insert.php";

    $.post(url,{  hiddenField2: $('[name="hiddenField2<?php echo $id; ?>[]"]').serialize(),hiddenField: hiddenField5<?php echo $id; ?>.val(), hiddenField1:hiddenField1<?php echo $id; ?>.val(), hiddenField4:hiddenField4<?php echo $id; ?>.val()  } , 
    function(data) {

    });
    setTimeout(function() {

    $.ajax({
    type:"POST",
    data:"getNews=true",
    success: function(r){
        $("#newsContent").html(r);

    },
    error: function(){
 alert($(".hiddenField2<?php echo $id; ?>").val());     
$("#error").text($(".hiddenField2<?php echo $id; ?>")).fadeIn(300)
    }
})


},200);
 }

please ignore all the echo id's that are in there, they are there more for future use than anything. I need help forming the array for hiddenField2 using javascript, then submitting it to insert.php, then how to decode the array so that php can use a foreach loop. any help with this?

  • 写回答

1条回答 默认 最新

  • doukui4836 2013-02-19 23:09
    关注

    You should look into the jQuery Serialize function:

    http://api.jquery.com/serialize/

    $.ajax({
    type:"POST",
    data: $("#form-id").serialize(),
    success: function(r){
     // do something here after success
    });
    

    edit: elaborated

    I did not test this but you could do something like this

    <form class="responseForm" action=""> 
    <input name="formData[id]" type="hidden" value="<?php echo $id; ?>" />
    <input name="formData[qty]" type="hidden" value="<?php echo $qty; ?>" />
    
    <font color="red"><?php echo $val; ?></font><br />
    <?php
    $i=1;
    while($i<=$qty)
    {
    ?>
    Participant <?php echo $i; ?>:<input name="formData[participants][]" type="text" /><br />
    <?php  $i++;
     }
     ?>
    <input name="submit" type="submit" value="Submit" />
    </form>
    

    Use something this for the javascript

    <script>
        $("form.responseForm").submit(function(e){
            e.preventDefault();
            var formData = $(this).serialize();
            $.ajax({
                url : '/insert.php', // assuming from root of your domain dir 
                type:"POST",
                data: formData,
                success: function(response){
                    $("#newsContent").html(response);
                },
                error: function(){
    
                }
            });
        });
    </script>
    

    and in your insert.php

    <?php
    
        $formData = $_POST['formData']; // an array of the form
        foreach($formData['participants'] as $participant){
            // do something
        }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值