如何在我的函数中将表单数组传递给我的PHP脚本?

时间:2015-10-08 08:49:00

标签: javascript php jquery mysql serialization

如何将表单数组传递给PHP脚本?我的php脚本将序列化准备输入到mysql的数据。

表单输入,[]将愉快地通过一个值传递给我的php。但我需要通过和数组所以思考使用[]然后序列化我的PHP。

var fcl_form_data = {
index: window.localStorage.getItem("fcl_form_data:index"),
$table: document.getElementById("fcl_form_data-table"),
$form: document.getElementById("fcl_form_data-form"),
$button_save: document.getElementById("fcl_form_data-op-save"),
$button_discard: document.getElementById("fcl_form_data-op-discard"),
init:
function() {
        // initialize storage index
        if (!fcl_form_data.index) {
            window.localStorage.setItem("fcl_form_data:index", fcl_form_data.index = 1);
        }

    // initialize form
    fcl_form_data.$form.reset();

    fcl_form_data.$form.addEventListener("submit", function(event) {
    var entry = {
    id: parseInt(this.id_entry.value),
    client_number:this.client_number.value,
    client_name:this.client_name.value,


    // HERE Just these middle fields are arrays []

    service:this.service.value,
    size:this.size.value,
    volume:this.volume.value,
    deliver_point:this.deliver_point.value,
    port_orgin:this.port_orgin.value,
    a_port_orgin:this.a_port_orgin.value,
    road_freight:this.road_freight.value,
    terms:this.terms.value,
    competitor:this.competitor.value,
    freight_speed:this.freight_speed.value,
    report_comments:this.report_comments.value,

    // HERE 

    company_stage:this.company_stage.value,
    client_user_name:this.client_user_name.value,
    client_user_name_status:this.client_user_name_status.value,
    client_user_name_kids:this.client_user_name_kids.value,
    client_user_name_hobbies:this.client_user_name_hobbies.value,
    client_user_comments:this.client_user_comments.value
    };

    if (entry.id == 0) { // add
    fcl_form_data.storeAdd(entry);
    // Adds data to table when pressed save - like a temp table above
    fcl_form_data.tableAdd(entry);
    }
    else { // edit
    fcl_form_data.storeEdit(entry);
    fcl_form_data.tableEdit(entry);
    }
    this.reset();
    this.id_entry.value = 0;
    event.preventDefault();
    },
    true);

表格输入是这样的 -

<input type="text" name="deliver_point[]" placeholder="Deliver Point">

我的Ajax -

    databaseAdd: function(entry) {

$.ajax({
  type: "POST",
  url: "//cms/by/m_upload.php",
  data: entry ,
  success: function(v) {alert("okay");},
  failure: function(v) {alert("fail");},
  dataType: JSON
});

},

我的PHP -

$client_number = $_POST['client_number'];
$client_name = $_POST['client_name'];

$service =serialize ($_POST['service']);
$size =serialize ($_POST['size']);
$volume =serialize ($_POST['volume']);
$deliver_point =serialize ($_POST['deliver_point']);
$port_orgin =serialize ($_POST['port_orgin']);
$a_port_orgin =serialize ($_POST['road_freight']);
$terms =serialize ($_POST['terms']);
$competitor =serialize ($_POST['competitor']);
$freight_speed =serialize ($_POST['freight_speed']);
$report_comments =serialize ($_POST['report_comments']);

$company_stage = $_POST['company_stage'];
$meeting_rating = $_POST['meeting_rating'];
$client_user_name = $_POST['client_user_name'];
$client_user_status = $_POST['client_user_status'];
$client_user_kids = $_POST['client_user_kids'];
$client_user_hobbies = $_POST['client_user_hobbies'];
$client_user_comments = $_POST['client_user_comments'];


$query="INSERT INTO l_reports (client_number,client_name,service,size,volume,deliver_point,port_orgin,a_port_orgin,road_freight,terms,competitor,freight_speed,report_comments,company_stage,meeting_rating,client_user_name,client_user_status,client_user_kids,client_user_hobbies,client_user_comments)

VALUES ('".$client_number."', '".$client_name."', '".$service."', '".$size."', '".$volume."', '".$deliver_point."', '".$port_orgin."', '".$a_port_orgin."', '".$road_freight."', '".$terms."', '".$competitor."', '".$freight_speed."', '".$report_comments."', '".$company_stage."', '".$meeting_rating."', '".$client_user_name."', '".$client_user_status."', '".$client_user_kids."', '".$client_user_hobbies."', '".$client_user_comments."');";

$result = $dbLink->query($query);

1 个答案:

答案 0 :(得分:1)

对您的代码进行小规模重构会让您的生活变得更轻松。

例如,您发送的ajax请求,您可以通过发送这样的对象使服务器端php更有效。

var postObjct = {
    data: entry
};

然后当你把它发送到php时:

$.ajax({
  type: "POST",
  url: "//cms/by/m_upload.php",
  data: postObjct ,
  success: function(v) {alert("okay");},
  failure: function(v) {alert("fail");},
  dataType: JSON
});

你只有1 json_decode可以使用php:

//php
$postedObject = json_decode($_POST['data']);
//This POST['data'] actually contains everything in your javascript 'entry' object. 

现在使用此php对象,您可以通过以下方式处理值:

//php
$client_number = $postedObject->{"client_number"};
//Or — TODO: Will need to test the structure
$client_number = $postedObject["entry"]["client_number"];

当然你应该检查空值等,但这会让你的生活变得更轻松!