找到你要的答案

Q:How do I pass my form arrays to my PHP script within my function?

Q:我如何通过我的形式排列我的PHP脚本在我的功能?

How do I pass my form arrays to my PHP script? My php script will be serializing the data ready to be input into mysql.

Form input WHITHOUT the [] will happily pass through one value to my php. but i need to pass through and array so was thinking using the [] and then serializing on my 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);

Form inputs are like this -

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

my 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
});

},

MY 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);

我如何通过我的形式排列我的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);
answer1: 回答1:

A small refactoring of your code would make life a lot easier for you.

For example, the ajax request which you are sending, you could make it more effiecient for the server side php by sending an object like this.

var postObjct = {
    data: entry
};

Then when you send it to the php like so :

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

You only have 1 json_decode to work with on the php :

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

Now with this php object you can work with the values in this way :

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

Of course you should do checks for nulls etc, but this will make your life a little easier!

一个小的重构你的代码会让你的生活轻松许多。

例如,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"];

你当然应该做为空值等检查,但这将使你的生活更容易一点!

javascript  php  jquery  mysql  serialization