使用多个输入验证表单数据

时间:2014-03-10 16:02:42

标签: php html

我目前的代码是用户输入多个项目到用逗号分隔的文本输入,这被提交到php后端并使用explode,我可以检索正确的值并通过jQuery处理它们。

这是我的问题。我需要找到一种使用多个输入框验证输入的方法。最大值为8,我正在努力找到一种方法,使用我现有的代码。

Firebug声明POST是2个单独的box_add帖子,即box_add 192 box_add 193.我不能使用[],因为这会干扰验证器。我将非常感激有关如何推进这一进程的一些指导。非常感谢

HTML相关代码

<a href="#" id="INTKAddMoreFileBox" class="btn btn-info">Add More Box Fields</a>
    <div id="INTKInputsWrapper">
       <div>
            <input name="box_add" type="text" id="box_add" />
            <a href="#" class="removeclass">&times;</a>
            <a style="margin-left: 14px;" href="javascript:void(0)" title="Just an example" class="tooltip">Help</a>
       </div>
    </div>

PHP代码

<?php
    session_start();

    $connect = mysql_connect("localhost", "root", "") or die ('{"opp":"error","box":"mysql_connect FAILED"}');

    $db = mysql_select_db("sample");


    // test vars from jquery form
    $status = mysql_real_escape_string($_REQUEST['status']);
    $company = mysql_real_escape_string($_REQUEST['company']);
    $requested = mysql_real_escape_string($_REQUEST['requested']);
    $activity = mysql_real_escape_string($_REQUEST['activity']);
    $address = mysql_real_escape_string($_REQUEST['address1']);
    $service = mysql_real_escape_string($_REQUEST['service']);
    $destroyDate = mysql_real_escape_string($_POST['datepicker']);
    $date = explode('/', $_POST['datepicker']);
    $newdate = $date[2].'-'.$date[1].'-'.$date[0];
    //$box = mysql_real_escape_string($_REQUEST['box_add']); // never used
    $authorised = mysql_real_escape_string($_SESSION['kt_name_usr']);
    $dept = mysql_real_escape_string($_REQUEST['dept']);
    $boxerrortext = 'Error';

    // Split the box if multiples
    $array = explode(',', $_REQUEST['box_add']);

    $outString = ''; 

    foreach ($array as $box) {
        $box = mysql_real_escape_string($box);
        $sql = "SELECT item FROM act WHERE item = '$box'";
        $result = mysql_query($sql) or die ('{"opp":"error","box":"' . mysql_error() . '"}');

        // If there are dupe entries, send message to jquery
        if (mysql_num_rows($result) > 0) {

            $outString .= $box . ' ';       
    }   
    }
    if ($outString) {
        $error = array('opp' => "error", 'box' => $outString); 

        $output = json_encode($error);
        echo $output;
        exit();
    }

    foreach ($array as $box) {
        $outString .= "<br />company:  $company <br /> address: $address <br />service: $service <br />Destroy Date: $destroyDate <br />box: $box <br />";

        $box = mysql_real_escape_string($box);
        $sql = "INSERT INTO `temp` (service, activity, department, company, address, user, destroydate, date, item, new)";
        $sql .= "VALUES ('$service', '$activity', '$dept', '$company', '$address', '$requested', '$newdate', NOW(), '$box', 1)";
        $result = mysql_query($sql) or die ('{"opp":"error","box":"' . mysql_error() . '"}');
    }

    $json = array('opp' => 'insert', 'box' => $outString);
    $result = json_encode($json);
    echo $result;
?>

添加输入的脚本

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

var MaxInputs       = 8; //maximum input boxes allowed
var InputsWrapper   = $("#INTKInputsWrapper"); //Input boxes wrapper ID
var AddButton       = $("#INTKAddMoreFileBox"); //Add button ID

var x = InputsWrapper.length; //initlal text box count
var FieldCount=1; //to keep track of text box added

$(AddButton).click(function (e)  //on add input button click
{
        if(x <= MaxInputs) //max input box allowed
        {
            FieldCount++; //text box added increment
            //add input box
            $(InputsWrapper).append('<div><input type="text" name="box_add" id="box_add" /><a href="#" class="removeclass">&times;</a></div>');
            x++; //text box increment
        }
return false;
});

$("body").on("click",".removeclass", function(e){ //user click on remove text
        if( x > 1 ) {
                $(this).parent('div').remove(); //remove text box
                x--; //decrement textbox
        }
return false;
}) 

});
</script>

1 个答案:

答案 0 :(得分:1)

为什么不对输入进行枚举?

类似的东西:

    $(InputsWrapper).append('<div><input type="text" name="box_add'+FieldCount+'" id="box_add'+FieldCount+'" /><a href="#" class="removeclass">&times;</a></div>');

并且在删除输入时只需添加计数器

FieldCounter--;

现在,在php方面,您可以通过以下方式处理这些字段:

  • 使用正则表达式查找基于输入id发送的最大输入量(如果存在box_add7,那么7将是要迭代的box_add的最大数量)

  • 或在隐藏字段上发送此字段计数器值

  • 或者,如果使用AJAX,则作为具有FieldCount值的额外参数。

    for ($i = 1; $i <= fieldCounter; $i++){
      $value = $_POST["box_add".$i];
      //do something with $value
    }