简体   繁体   中英

echo back $array[] elements to html form on Submit

After alot of digging around some very informative posts and info to try and find out how to solve this issue I thought I would ask around to see if anyone has any pointers. I have an html form with various inputs (checkboxes, text boxes etc...). Each input section has its own submit or 'Upload' button. On Upload a php script is called and various bits of processing is done before data is sent over a pipe to a Python script for further stuff. I am currently echoing back input variables to the form on submission so that the html page does not refresh (or should I say the inputted data is not lost to the users view) on an Upload event, however, I now have to do the same for a bunch of checkboxes and text boxes the values of which are stored in an array. The code I have written so far is as follows (I am new to both php and html so please excuse the inefficiency that I'm sure is obvious)

html/php

<margin>CH1</margin><input type="checkbox"name="ANout[]"value="AN1_OUT"
<?php if(in_array('AN1_OUT',$_POST['ANout']))echo'checked';?>>
Voltage<input type="text"size="5"name="ANout[]"
value="<?php $ANout[$i]=$_POST['ANout'];
if(!empty($ANout[$i]))echo"$ANout[$i]";?>">
<br>

The code above works fine for the checkboxes which happily remain after an Upload button is pressed but not for the array. When the Upload event occurs I simply get 'Array' written in the text box. I have tried existing code I have written to echo back other text input in the form (see below) and which works but these are for sole entries, not arrays. I have tried various configurations of syntax but I always seem to get the same result.

Working Code:

<margin>Duty Cyle</margin><input type="text"name="PWM1DC"size="3"
value="<?php $PWM1DC = $_POST['PWM1DC']; 
if(!empty($PWM1DC))echo "PWM1DC";?>">
<br>

I'm sure it is something straightforward but I have been fiddling and staring at it for ages and can't seem to find the problem.

You are getting "Array", because you are trying to print out variable of type Array.

You probably want to give your fields separate names or indexes and do something like this:

<form method="post">

   <input type="checkbox" name="ANout[1]" value="AN1_OUT"
         <?php if(isset($_POST['ANout']) && in_array('AN1_OUT',$_POST['ANout']))echo'checked';?>>

   Voltage<input type="text"size="5"name="ANout[2]"
         value="<?php if(isset($_POST['ANout']) && !empty($_POST['ANout'][2])) echo $_POST['ANout'][2]; ?>">

   <input type="submit" value="ok">

</form>

(Just added form tags, submit button and isset checks to show working example.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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