I am creating a wordsearch, I'm trying to post the value in another page, The problem is that I cannot display all the value of $thisChar to another page, all I get is the last letter. So the question is how can you post a variable that is something like this e.g., $rc[$r][$c]...
This is my form. I have hidden fields that named thisChar.
echo '<form method="post" action="#path" target="blank">';
echo '<table>';
#--Display the random letters and the words
for ($r=0;$r<=$row;$r++) {
echo '<tr>';
for ($c=0;$c<=$col;$c++) {
$thisChar=strtoupper($rc[$r][$c]);
echo '<input type="hidden" name="thisChar" value="'. $thisChar.'">';
echo '<td style="background:#fff">' . $thisChar . '</td>';
}
echo '</tr>';
}
echo '</table>';
echo '<input type="submit" name="submit">';
echo '</form>';
This is how I fetch the post data. All I get is Uninitialized offset error.
for ($r=0;$r<=$row;$r++) {
for ($c=0;$c<=$col;$c++) {
$thisChar=$_POST['thisChar'];
$pdf->Cell(10,10, strtoupper($thisChar) ,1,0,'C', );
}
}
I already tested other approach like foreach loop and adding square brackets on the name of hidden fields and it works, now I want to make it work using this approach. This method creates a loop that can display in a table. Any idea how can I make it works?
You need to post it as array, so your name attribute should look like this:
echo '<input type="hidden" name="thisChar[]" value="'. $thisChar.'">';
haven't check the rest of your code, but this will send all the values as an array
Use html input name as an array!
echo '<input type="hidden" name="thisChar[]" value="'.$thisChar.'">';
Then retrieve this variable in action PHP file as array:
$thisChars=$_POST['thisChar'];
foreach($thisChars as $thisChar)
{
...
}
Related
I have a form having multiple checkbox like this.
$res_rooms=$this->product_model->roomType();
foreach($res_rooms->result() as $val)
{
<input type="checkbox" name="room_list[]" value="<?php echo $val; ?>"id="room_list<?php echo $val;?>">
}
On Form submit I need all checkbox id on my controller whether it is checked or not.
You do not need to post ids here:
here
1) id contains room_list <?php echo $val;?>:
2) name contains room_list[]
3) Value contains <?php echo $val; ?>
Now you id is consist of room_list and $val.
$val you are already getting in post as a value
So in $POST data you can dynamically generate id of all checkbox
ex.
foreach($_POST['room_list'] as $rooms) {
$id = 'room_list'+$rooms; // here $rooms contains the `$val` values
}
Create hidden input fields to get all checkbox values:
$res_rooms=$this->product_model->roomType();
foreach($res_rooms->result() as $val)
{
<input type="hidden" name="room_list_hidden[]" value="<?php echo $val; ?>" />
<input type="checkbox" name="room_list[]" value="<?php echo $val; ?>"id="room_list<?php echo $val;?>">
}
So in $_POST['room_list_hidden'] you will get all ids
If check-box is checked then you get that check-box and its value on server and you area using array of check-box so you get in array format.
to get all the ids on server side.
make a query which gives you all the ids on the server directly.
make hidden field for all ids so at the time of form submit you will
$res_rooms=$this->product_model->roomType();
foreach($res_rooms->result() as $val)
{
" />
"id="room_list">
}
After a SQL request, I obtain a form which contains a list of data, with checkboxes at the end of each line. The goal is the following. If the user checks some of them, the line will be deleted in the database when the form will be submitted.
I name my checkboxes like this, with my SQL request results, so my Php script could find the line to delete:
<input type="checkbox" name="chk[<?echo '/'.'$t[datetr]'.'/'.'$t[beneficiaire]'.'/'.'$t[objet]'.'/'.'$t[montant]';?>]">
My goal is to get all the checkboxes values with $_POST to my Php script. But even with this...
foreach ($_POST as $key => $value) {
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
...my php script does not seem to get the checkboxes values... Did I do something wrong ? Thanks for help.
Aside from the other (entirely accurate) comments about unchecked boxes not being passed with the HTTP request - you have a couple of issues with your actual PHP.
<?echo '/'.'$t[datetr]'.'/'.'$t[beneficiaire]'.'/'.'$t[objet]'.'/'.'$t[montant]';?>
Firstly, your echo is wrong; it should probably be <?php echo or <?= rather than <?echo (although that might work with short start tags enabled).
Secondly, Apostrophes in PHP are non-interpolated string literals (i.e. '$t[objet]' will literally be treated as the string '$t[objet]' not the variable).
Finally, assuming $t is an array, your associative indexes need to have quotes or they'll be interpreted as constants - which is likely to throw an error.
I think what you want could be written as:
<?= "/{$t['datetr']}/{$t['beneficiaire']}/{$t['objet']}/{$t['montant']}"; ?>
Once you've sorted that out, the $_POST['chk'] data should be set properly and it'll be an associative array as you're expecting.
Then a foreach($_POST['chk'] as $key => $value) { ... } loop should work... though, of course none of your inputs actually have values at the moment.
When using <input type="checkbox" name="foo" value="42"/>, the variable foo=42 is sent only if the checkbox is checked. When the box is not checked, nothing is sent at all.
If you need some 0/1 information, i suggest you use either a <select> or a couple of <input type="radio"> tags instead:
<input type="radio" name="foo" value="1"/> Yes
<input type="radio" name="foo" value="0"/> No
Blank checkboxes do not get posted to the receiving script, only checked ones do. To get around this have a corresponding set of hidden fields, and set their values to something like "ON" or "OFF" depending on how the checkboxes are clicked. You will probably want to use the onClick event, determine of the box is clicked or not then set the appropriate hidden field, i.e.
Checkbox_One Hidden_One
Checkbox_Two Hidden_Two etc.
When you post the form, have your script ignore the checkboxes, and just process the hidden fields.
Something to remember when doing these multi-checkboxes is that when you submit, it will be interpreted in PHP as an array, in your case $_POST['chk'] will be an array of checked checkboxes.
However, you should also make sure you give the checkboxes a value, even if just a 1.
When handling your POST, initially just try using a var_dump($_POST); die(); to see what the data looks like.
use the code below:
foreach ($_POST['chk'] as $key => $value) {
echo "<tr>";
echo "<td>";
echo $key;
echo "</td>";
echo "<td>";
echo $value;
echo "</td>";
echo "</tr>";
}
If you name all your checkboxes like this name="boxArray[]" it will create an array named $_POST["boxArray"] when the form is submitted.
Then you can do your foreach loop to display the values:
foreach ($_POST["boxArray"] as $item) {
echo "<tr>";
echo "<td>";
echo $item;
echo "</td>";
echo "</tr>";
}
To add to this, if you want to delete only the items checked then for each checkbox assign the record's ID as its value:
<input type="checkbox" name="boxArray[]" value="RECORD ID">
Now when you run your foreach loop only the checked boxes will post values so you change the code to delete each item in the array:
foreach ($_POST["boxArray"] as $item) {
//SQL TO DELETE RECORD WHERE ID = $item;
}
So I'm iterating through a set value via a for loop, which will echo html input fields, every other echo'd html input field behaves as expected (including the name and id fields of the one below), however I keep getting syntax errors when trying to set the value as a postback to retain them on page submit.
Here is my code:
$type = "number".$i;
echo '<input type="text" name="'.$type.'" id="'.$type.'" value="'.<?php if (isset($_POST[$type])) { echo $_POST[$type]; } else { echo NULL;}.'" />';
Thanks in advance.
You have an extra <?php statement in the row. Since the line is echo '...'. you don't need to declare that more php code is coming. You can do something like this instead:
echo '<input type="text" name="'.$type.'" id="'.$type.'" value="';
if (isset($_POST[$type])) echo $_POST[$type];
echo '" />';
Although personally, I prefer to do something like <input [...] id="{$type}" [...]" outside of the php code, less messy.
my code starts with multiple php arrays with XML data being passed through multiple checkboxes in the following way($i cycles through items in XML doc, values pass specific data in row i that is selected):
$MoneyLine = $event->periods->period[0]->moneyline; //background info
$AwayMoneyLine[] = $MoneyLine->moneyline_visiting; //background info
<input type='checkbox' name='AwayMoneyLine' value='$Date[$i];$AwayRotNum[$i];$AwayParticipantName[$i];$ATotalPoints[$i]'/>
On my next page, I pass the variables in the following manner, but they are not resulting on the next page.:
if(isset($_POST['AwayMoneyLine'])){
foreach($_POST['AwayMoneyLine'] as $value) {
$d = explode(';',$value);
echo '<input type="hidden" name="hidden[]" value="$d[0];$d[1];$d[2];$d[3];$d[4]">';
Here is how I'm attempting to get the data on the next page. Any suggestions on how I can pass the variables through to this page? On the form (also on var_dump of $d), I get $d[0], $d[1], etc. Any help is greatly appreciated!:
foreach($_POST['hidden'] as $value) {
$f = explode(';',$value);
echo 'Here is your following bet:';
echo '<table cellpadding="0" cellspacing="0" border="1" bordercolor="#585858" width=100%>';
echo "<tr><td>$e[0]</td><td>$f[0]</td><td>$f[1]</td><td>$f[2]</td><td>$f[3]</td><td>$f[4]</td></tr>";
}
echo '</table>';
Change the code to:
echo "<input type=\"hidden\" name=\"hidden[]\" value=\"$d[0];$d[1];$d[2];$d[3];$d[4]\">";
$d[0];$d[1];$d[2];$d[3];$d[4] will not be printed as you put inside single quote. You should use double quote and escape attributes like type=\"hidden\", name=\"hidden[]\", etc.,
eg:
<?php
$name = 'foo';
echo "$name";
echo '<br />';
echo '$name';
?>
Output:
foo
$name
use $serial=serialize($array);
echo "<input type=\"hidden\" name=\"hiddenfield\" value=\"<?php echo$serial; ?>">";
I'm trying to add submit buttons and hidden input fields so that when a user clicks a submit button I can identify the item they selected. Although I can't figure out how to access the hidden values. I have this code (the value in input type="hidden" are the item's id's). How do I access the values?
foreach($dbh->query("SELECT * FROM beer WHERE country_id = $countryID") as $beer) {
echo "<a href='BeerSummary.php?beerID=$beer[id]'>$beer[2]</a> <br/>";
echo "ABV $beer[3]% - $beer[4] ml - Case Size $beer[5] - Price £$beer[6]";
echo '<input type="submit" value="Add to Cart"> <br/>';
echo '<input type="hidden" name="beer_id[]" value="'.$beer[0].'">';
echo "<br/>";
}
if(isset($_POST["beer_id"])) {
//
}
You have them defined as an input array, so the way to access it/them would be:
foreach($_POST['beer_id'] as $value)
{
echo $value;
}
for each of hidden fields, take id. id = hidden + $beer[i]
then u can easily access the hidden fields with document.getElementById("hidden" + $beer[i])
this will work in javascript.
if u want to so the same in php, #Ben's answer should work.