duanci1939 2014-08-20 15:43
浏览 46
已采纳

使用jquery和ajax发布文本字段数组

i dont want to use serialize() function please help me with this. I am a beginner

html

        <input type='button' value='Add Tier Flavor' id='Add'>
        <input type='button' value='Remove Tier Flavor' id='Remove'>

        <div id='batch'>
            <div id="BatchDiv1">
                <h4>Batch #1 :</h4>
                <label>Flavor<input class="textbox" type='text' id="fl1" name="fl[]" value=""/></label></br>
                <label>Filling<input class="textbox" type='text' id="fi1" name="fi[]" value="" /></label></br>
                <label>Frosting<input class="textbox" type='text' id="fr1" name="fr[]" value=""/></label></br>

            </div>
        </div>
        <br>

    </div>

this is a dynamically added fields using javascript the code is:

javascript

  <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

    <script type="text/javascript">
    $(document).ready(function(){

    var counter = 2;

    $("#Add").click(function () {

        if(counter>5){
            alert("Only 5 Tiers allow");
            return false;
        }
        var newBatchBoxDiv = $(document.createElement('div')).attr("id", 'BatchDiv' + counter);
        newBatchBoxDiv.html('<h4>Batch #'+ counter + ' : </h4>' +
            '<label> Flavor<input type="text" name="fl[]" id="fl' + counter + '" value=""></label><br>'+
            '<label> Filling<input type="text" name="fi[]" id="fi' + counter + '" value=""></label><br>'+
            '<label> Frosting<input type="text" name="fr[]" id="fr' + counter + '" value=""></label><br>' );

        newBatchBoxDiv.appendTo("#batch");

        counter++;
    });

    $("#Remove").click(function () {
        if(counter==1){
            alert("No more tier to remove");
            return false;
        }
        counter--;

        $("#BatchDiv" + counter).remove();
    });



});
 </script>

i am trying to post the values in an array to post it onto next .php page

i am using this

        var user_cupfl =  $('input[name^="fl"]').serialize();
        var user_cupfi =  $('input[name^="fi"]').serialize();
        var user_cupfr = $('input[name^="fr"]').serialize();

serialize is not passing the values. :(

on second page i am trying to mail it using

   $message .= "<tr><td><strong>Cake Flavors(according to batches):</strong> </td><td><pre>" .implode("
", $user_cupfl). "</pre></td></tr>";
            $message .= "<tr><td><strong>Filling type (Inside the cake):</strong> </td><td><pre>" .implode("
", $user_cupfi). "</pre></td></tr>";
            $message .= "<tr><td><strong>Frosting type (top of the cake):</strong> </td><td><pre>" .implode("
", $user_cupfr). "</pre></td></tr>";

i m posting array like this

  $user_cupfl=filter_var($_POST["userCupfl"], FILTER_SANITIZE_STRING);

$user_cupfi=filter_var($_POST["userCupfi"], FILTER_SANITIZE_STRING);
$user_cupfr=filter_var($_POST["userCupfr"], FILTER_SANITIZE_STRING);

your replies will be highly appreciated

  • 写回答

1条回答 默认 最新

  • dongzhan1383 2014-08-20 15:53
    关注

    Just because you name a variable user_* doesn't mean that is what the name of the field is in the serialized POST data. You would still be looking for $_POST['fl'], $_POST['fi'] etc.

    I don't understand why you think you need to serialize sets of input groups individually. You should just serialize the whole form at once.

    I also see no reason why you need to have all this logic around unique id's with the counter and what not. If you are not using id's at all, just drop them altogether.

    You might also consider simply using clone techniques to generate your dynamically added fields. You could greatly simplify all that javascript code by doing these things.

    A more reasonable implementation may look like this.

    HTML (cleaning up your code - consistent use of double quotes around properties, better strategy for class and id usage, etc.)

    <div id="batch">
        <div class="batchDiv">
            <h4 class="batchLabel">Batch #1 :</h4>
            <label>Flavor</label>
            <input class="textbox" type="text" name="fl[]" value=""/>
            </br>
            <label>Filling</label>
            <input class="textbox" type="text" name="fi[]" value="" />
            </br>
            <label>Frosting</label>
            <input class="textbox" type="text" name="fr[]" value=""/>
            </br>
        </div>
    </div>
    

    Javascript:

    $(document).ready(function() {
        $('#Add').click(function(){
            var $existingBatches = $('.batchDiv');
            var count = $existingBatches.size();
            if (count < 5) {
                // get last batch div
                var $newBatch = $existingBatches.last().clone();
                // reset input values to empty string
                $newBatch.find('input').val('');
                // change batch label
                count++;
                $newBatch.find('.batchLabel').html('Batch #' + count + ' :');
                // append to document
                $newBatch.appendTo('#batch');
            } else {
                // alert or whatever
            }
        });
    
        $('#Remove').click(function(){
            var $existingBatches = $('.batchDiv');
            var count = $existingBatches.size();
            // delete last batch item if more than 1 exists
            if(count > 1) {
                $existingBatches.last().remove();
            } else {
                // alert or whatever
            }
        });
    });
    

    Now you haven't shown your AJAX code, but all you would need to do is something like:

    var url = '/some/url';
    var postData = $('[some form selector]').serialize();
    var dataType = '...'; //whatever dataType you are expecting back
    $.post(url, postData, function(){
        // success handler
    }, dataType));
    

    Your data when then be available in PHP script at $_POST['fl'], etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
  • ¥20 关于#stm32#的问题:STM32串口发送问题,偶校验(even),发送5A 41 FB 20.烧录程序后发现串口助手读到的是5A 41 7B A0
  • ¥15 C++map释放不掉
  • ¥15 Mabatis查询数据
  • ¥15 想知道lingo目标函数中求和公式上标是变量情况如何求解
  • ¥15 关于E22-400T22S的LORA模块的通信问题
  • ¥15 求用二阶有源低通滤波将3khz方波转为正弦波的电路