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.
Related
I am currently working on an update profile section of a website I am creating for fun. In this part of the update profile business owners can update their restaurant menu (menu consists of category (appetizers, entrees, etc), menu item and allergens).
Right now the website is printing out what the business owner has previously submitted in input box format, this way business owners can simply just erase and re-enter their new information. However, we don't know exactly how many menu items each restaurant has so I devised a system to dynamically update each item being altered row by row.
<?php
$sql="SELECT * from menu_item as m, allergen as a WHERE a.restaurant_id=m.restaurant_id AND m.menu_item_id=a.menu_item_id AND m.restaurant_id='".$rest_id."'";
$result11=mysqli_query($con,$sql);
if (mysqli_num_rows($result)==0){
echo "<strong>You have not submitted any menu information</strong><br><br>";
echo "Please enter your menu information here: ";
echo 'Create Menu';
}
else{
$i=0;
echo "<table border='1' cellpadding='10'><tr><th>Category</th><th>Menu Item</th><th>Allergen</th></tr>";
while($rows=mysqli_fetch_assoc($result11)) {
echo '<tr><td><input type="text" name="category'.$i.'"value="'. $rows['category']. '"</td><br>';
echo '<td><input type="text" name="menu_item'.$i.'" value="'. $rows['menu_item']. '"</td><br>';
echo '<td><input type="text" name="allergen'.$i.'" value="'. $rows['allergen']. '"</td><br>';
echo '<td><input type="hidden" name="id'.$i.'" value="'. $rows['menu_item_id']. '"</td></tr><br>';
$i++;
}
}
echo "</table>";
var_dump(mysqli_num_rows($result11));
// var_dump($_POST);
$count=0;
while ($count<=mysqli_num_rows($result11)){
${"category".$count}=mysqli_real_escape_string($con, $_POST['category'.'$count']);
$count++;
}
var_dump($category0);
?>
This is where the while loop near the bottom comes into play. I want to be able to dynamically create variables for category, menu item, and allergen. Then I want to be able to create $result variables within this same while loop (mysqli_query) and then update rows accordingly. However, right now my very last var_dump is returning a value of "" which tells me I'm either concatenating the html name attribute wrong(first while loop) or there is something wrong with concatenation in my last while loop. Does anyone know what I'm doing wrong?
$_POST['category'.'$count'] in your second loop should be $_POST['category'.$count]. You also only want to run this code after the form has been submitted (I'm assuming the code you've posted is not your full script so it's not clear if that's what is happening) - otherwise you'll get the original values rather than any changes the user has made in the form.
In general you'll have an easier time if you can get the submitted data into a multi-dimensional array which you can loop through, instead of having to dynamically create variables. See the section "How do I create arrays in a HTML form?" on http://php.net/manual/en/faq.html.php. In your case I'd do something like:
// (in your first while loop)
echo '<input type="text" name="menu['.$i.'][category]" value="'. htmlspecialchars($rows['category']). '"><br>';
echo '<input type="text" name="menu['.$i.'][menu_item]" value="'. htmlspecialchars($rows['menu_item']). '"><br>';
// (etc.)
You then loop through this with something like:
foreach ($_POST['menu'] as $menuRow) {
// you now have:
// $menuRow['category']
// $menuRow['menu_item']
// ...and so on
}
You also want to escape the input values you are outputting with htmlspecialchars() as I have above.
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;
}
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)
{
...
}
How can I display this value in the loop ?
echo '<input type="hidden" name="eg_payamt_'.$i.'" id="amount_post_'.$i.'" value="">';
this code is being passed into next code by POST
On the next page,
Could it be like this ?
foreach($_POST["eg_payamt_"] as $key => $payamt)
{
echo "eg_payamt_$key => $payamt\n <br>";
}
I don't see any results,
Do you guys have any ideas ??
You have to change the name of the input element -- all input elements like this will have the same name:
<input type="hidden" name="eg_payamt[]" value="whatever" />
And then when you access $_POST['eg_payamt'] it will already be an array, so your code will work almost as-is (you need to lose a couple of underscores).
I have this code basis:
echo '<input required type = "text" name = "subject1" value="XXX" />';
However, I would like XXX to be a variable, I have searched this online, but all the things that one of the discussions says is to do this:
value=\"$firstName\"
or
value='$firstName'
I have tried both of these, but they don't work, and I was hoping that someone could help me with this problem, basically, what I want is to be able to asign the value of a text edit to a variable in php, but when the text edit itself, is embedded in an echo, nothing seems to work.
Thanks
Concatenation is a pretty easy way to go about this:
echo '<input required type = "text" name = "subject1" value="' . $foo . '" />'
edit: Those spaces around the concatenation .'s (. $foo .) aren't required - I just add them fore readability.
I think the problem is you are using single quotes, so variables are not rendered automatically... you can concat with . like this:
echo 'some text' . $variable . 'some more text';
echo '<input required type="text" name = "subject1 value="<?php echo $value; ?>" />';
acutally you don't need the php echo .... stuff.... /* brain not engaged */