dongxuxian6930 2019-08-09 12:24
浏览 99

如何使用mySql(PHP,AJAX,JQuery,JS)从表单控件修复空数组

I'm trying to create a PHP form to a nonprofit organization and would like to send an email via SMTP using PHPMailer. My problem is that the form contains a dynamic table, where I want to store/send data to a .php file via Ajax and JSON, but it doesn't fill the array with values (even if I type $(tr).find('td:eq(0)').val(). The Ajax data also contains two addition values. Also if I try to json_decode on the code below, the PHP breaks.

  1. I tried getting the data by variable, and it works.
  2. I tried to collect them in a single string which was converted to JSON, it didn't work.(The PHP doesn't send anything nor error feedbacks)
  3. Right now I'm trying to create multiple arrays from rows. But like 2. it doesn't work

The HTML body:

<html>....
<input id="name" class="form-control" required>
<input type="email" id="email"class="form-control" required>
<table id="dynamic_field">
  <tr>
   <td>
    <textarea id="value1" class="form-control" required></textarea></td>
   <td>
    <textarea id="value2" class="form-control" required></textarea></td>
.....
</tr></table>
<input type="button" onclick="sendEmail()" value="Send &arr" class="btn btn-primary">

The Script:

function sendEmail() {
  var name = $("#name");
  var email = $("#email");
  var value1 = $("#value1");
  var value2 = $("#value2");
 ....  
  var TableData;
  TableData = JSON.stringify(storeTblValues());

  $.ajax({
          url: 'sendEmail.php',
          method: 'POST',
          dataType: 'json',
          data: {
                 name: name.val(),
                 email: email.val(),
                 value1: value1.val(),
                 value2: value2.val(),
                 TableData: TableData
   }, success:...

function storeTblValues(){
 var TableData = new Array();
 $("#dynamic_field tr").each(function (row, tr) {
   TableData[row] = {"value1": $(tr).find('td:eq(0)').text() , "value2": $(tr).find('td:eq(1)').text() , ......}
  ;});return TableData;
;}

I expect the output of the array TableData to be [{"value1":"Whatever in Value 1","value2":"Whatever in Value 2 ",....}] , but what I read from this string is [{"value1":" ","value2":" ",...}]

  • 写回答

1条回答 默认 最新

  • dtxb75622 2019-08-09 13:05
    关注

    Use $('form').serializeArray()

    Example:

    $('button').click(function () {
      var formData = $('form').serializeArray();
      console.log(formData);
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <form>
      <input type="text" name="your-name" placeholder="name">
      <textarea name="your-text" placeholder="text"></textarea>
      <button type="button">Submit</button>
    </form>

    Also you cant convert this array:

    var searchParams = new URLSearchParams("your-name=value1&your-text=value2");
    var result = {};
    // Display the key/value pairs
    for(var pair of searchParams.entries()) {
       result[pair[0]] = pair[1]
    }
    console.log(result);

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。