简体   繁体   English

使用ajax重新格式化从表单发布的php数组

[英]Reformat php array posted from a form with ajax

I have a form on my website that sends the captured values of the form to an email address with php. 我的网站上有一个表单,该表单会将捕获的表单值发送到带有php的电子邮件地址。 On the form there is an checkbox input where users can select multiple checkboxes and the selected values are pushed into an array. 表单上有一个复选框输入,用户可以在其中选择多个复选框,并将所选值推入数组。 When I submit the form the checkbox input returns an entire array in the email. 当我提交表单时,复选框输入将在电子邮件中返回整个数组。 To my understanding this is as far as php can reformat. 据我了解,这是php可以重新格式化的格式。 I am looking for a way to reformat the php array with ajax to make it more reader friendly. 我正在寻找一种使用ajax重新格式化php数组的方法,以使其对读者更友好。

This is what the array looks like in the submitted email: 这是提交的电子邮件中的数组:

array ( 0 => 'value1', 1 => 'value2', 2 => 'value3' )

I am trying to make the array return the values in a different format such as: 我试图使数组以不同的格式返回值,例如:

Array Value:  value1, value2, value3

Here is my form: 这是我的表格:

  <form id="form" method="post" action="/submit" enctype="multipart/form-data">
    <div class="field">
     <label for="categoryType">Category type:</label>
     <input  type="checkbox" name="check_list[]" value="value1">
     <label>value1</label>
     <input  type="checkbox" name="check_list[]" value="value2">
     <label>value2</label>
     <input  type="checkbox" name="check_list[]" value="value3">
     <label>value3</label>
   </div>
 </form>

Here is the submit.html: 这是submit.html:

<?php

  $check_list  = $this->EE->input->post(['check_list']);

  $msg .= '<p><strong>Category</strong> '.$value.'</p>';

    ob_start();
    var_dump($check_list);
    $result = ob_get_clean();


   $this->EE->email->wordwrap = true;
   $this->EE->email->mailtype = 'html';
   $this->EE->email->to('email@emailcom');
   $this->EE->email->subject('form');
   $this->EE->email->message(entities_to_ascii($msg . " - " . $result));
   $this->EE->email->Send();

    if($http_code > 200) {
     print "<p><strong>Error</strong> </p>";
   } else {
     header( 'Location: redirecturl' ) ;
   }
?>

Is there a way to show only the string values of the array by submitting the form with ajax? 是否可以通过使用ajax提交表单来仅显示数组的字符串值? Or is there a different way to reformat this php array? 还是有其他方法可以重新格式化此php数组? Do I need to submit this form with php or could I scratch this method and only submit with ajax? 我是否需要使用php提交此表单,或者我可以刮擦此方法而仅使用ajax提交吗?

Instead of 代替

ob_start();
var_dump($check_list);
$result = ob_get_clean();

Try 尝试

$result = implode(',', $check_list);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM