【问题标题】:AJAX only saving the first letter of inputAJAX 只保存输入的第一个字母
【发布时间】:2018-05-01 10:53:07
【问题描述】:

我有一个 AJAX 请求,它序列化表单数据并将其发送到数据库。当数据被序列化时,我看到它会渲染全文 inout,但是当它存储在数据库中时,它只保存每个单词的第一个字母?

例如它会序列化这个被发送:

splashText=银釉

但如果没有行,数据库将保存:

S

如果提交的表中有多行数据在数据库中捕获如下:

第 1 行将 = S 第 2 行将 = i

我试图得到它的地方如下:

第 1 行将 = 银釉 第 2 行将 = 行中选择的下一个选项,依此类推...

POST的序列化数据如下(2行):

job_ref=3123&job_desciption=321&sil_cost=1&sil_cost_price=2.00&sub_total=119.75&item_name%5B%5D=1&item_quantity%5B%5D=1&item_unit%5B%5D=1&item_glass%5B%5D=110&item_splash%5B%5D=20&item_colour%5B%5D=-&item_HQuan%5B%5D=1&item_HDiam%5B%5D=2.25&item_CQuan%5B%5D=1&item_Total%5B%5D=56.25&item_cil_row%5B%5D=0.000001&item_name%5B%5D=1&item_quantity%5B%5D=1&item_unit%5B%5D=1&item_glass%5B%5D=110&item_splash%5B%5D=20&item_colour%5B%5D=-&item_HQuan%5B%5D=1&item_HDiam%5B%5D=7.5&item_CQuan%5B%5D=1&item_Total%5B%5D=61.50&item_cil_row%5B%5D=0.000001&glassText=6mm Toughened Extra Clear6mm Toughened Extra Clear&splashText=Rainbow SparkleRainbow Sparkle&holeText=22mm27mm

序列化表单的代码:

  var form_data = $(this).serialize() + '&glassText=' + $('.item_glass').children("option").filter(":selected").text() + '&splashText=' + $('.item_splash').children("option").filter(":selected").text() + '&holeText=' + $('.item_HDiam').children("option").filter(":selected").text();

+ 用于获取输入的文本值而不是值。我认为这可能是问题所在,但我不确定如何解决?

PHP 帖子:

 for($count = 0; $count < count($_POST["item_name"]); $count++)
 {
  $query = "INSERT INTO tbl_order_items
  (order_id, item_name, item_quantity, item_unit,item_glass, item_splash, item_HQuan,item_HDiam,item_CQuan,item_colour,item_total,user_id)
  VALUES (:order_id, :item_name, :item_quantity, :item_unit,:item_glass,:item_splash,:item_HQuan,:item_HDiam,:item_CQuan,:item_colour,:item_total,:user_id)";
  $statement = $connect->prepare($query);
  $statement->execute(
   array(
    ':order_id'   => $order_id,
    ':item_name'  => $_POST["item_name"][$count],
    ':item_quantity' => $_POST["item_quantity"][$count],
    ':item_unit'  => $_POST["item_unit"][$count],
    ':item_glass'  => $_POST["glassText"][$count],
    ':item_splash'  => $_POST["splashText"][$count],
    ':item_HQuan'  => $_POST["item_HQuan"][$count],
    ':item_HDiam'  => $_POST["holeText"][$count],
    ':item_CQuan'  => $_POST["item_CQuan"][$count],
    ':item_colour'  => $_POST["item_colour"][$count],
    ':item_total'  => $_POST["item_Total"][$count],
    ':user_id'   => $user_id
   )
  );
 }
 $result = $statement->fetchAll();
 if(isset($result))
 {
  echo 'ok';
 }
}

这是一个正在保存的动态表。这就是计数。

为了在动态表上放置上下文,这里是一个截图:

【问题讨论】:

  • 你能确认并在这里发布你在 php 中得到的文本吗?
  • 请提供php代码。
  • 用php代码编辑
  • $_POST["item_name"] 很可能是一个字符串,而不是一个数组。因此,执行$string[0] 将返回$string 的第一个字母
  • 如果您发送的是splashText=Silver Glaze,那么$_POST["splashText"] 确实是一个字符串。所以$_POST["splashText"][$count] 将是该字符串中的一个字符。

标签: javascript php jquery ajax forms


【解决方案1】:

因为您不提供 html 代码,所以我假设“Review Quote”按钮是提交按钮。所以,试试这个:

改变

var form_data = $(this).serialize() + '&glassText=' + $('.item_glass').children("option").filter(":selected").text() + '&splashText=' + $('.item_splash').children("option").filter(":selected").text() + '&holeText=' + $('.item_HDiam').children("option").filter(":selected").text()

收件人:

var glassText = '';
$('.item_glass').each(function(){
    glassText+='&glassText[]='+$(this).find(":selected").text().replace(' ','+');
});

var splashText = '';
$('.item_splash').each(function(){
    splashText+='&splashText[]='+$(this).find(":selected").text().replace(' ','+');
});

var holeText = '';
$('.item_HDiam').each(function(){
    holeText+='&holeText[]='+$(this).find(":selected").text().replace(' ','+');
});

var form_data = $(this).serialize() + glassText + splashText + holeText; 

【讨论】:

    猜你喜欢
    • 2012-09-13
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 2016-03-28
    • 1970-01-01
    • 2019-08-22
    • 1970-01-01
    • 2021-06-12
    相关资源
    最近更新 更多