I am very new to php and am teaching it to myself, so please keep that in mind.
I am working on a project that presents users with a list of items, from a database, with checkboxes, and allows the user to check them. I want to save the values of the checked fields. This is the line of code that prints all of the options. It prints a course code and course title, with a checkbox.
echo "<input type='checkbox' name ='boxes'>" . $row['course'] . ' ' . $row['title'] . "<br>";
However, when I try to print the values selected, it doesn't work. I get an error that says invalid argument supplied foreach()
if(isset($_POST['submit_courses'])){
if(!empty($_POST['boxes'])){
foreach($_POST['boxes'] as $selected ){
echo $selected."</br>";
}
}
}
Please help!
Try:
echo "<input type='checkbox' name ='boxes[]'>" . $row['course'] . ' ' . $row['title'] . "<br>";
EDIT:
To better answer the question:
You need to add the value="" attribute to your input fields. So if you have an ID in your $row array then it would be like this...
echo "<input type='checkbox' name ='boxes[]' value=' . $row['id'] . '>" . $row['course'] . ' ' . $row['title'] . "<br>";
Now you should be able to get the selected id's from the populated values inside of the $_POST['boxes'] array. Just loop through them and do something with them like echo them out.
foreach ($_POST['boxes'] as $box_value) {
echo $box_value . "<br>";
}
Only the boxes that are selected will be in the array and you will have the identifier for whatever courses were selected.
Related
I have a php file that is receiving some checkbox values from a form. Both the checkbox name and value are set up to match an Item_Name field in a mysql table. My current attempt is below:
while($row = $items->fetch_assoc()){
if( isset($_POST[$row['Item_Name']])) {
\\ Code to perform if true, mostly echoes
}
}
//Checkbox setup:
echo "<input type='checkbox' name=" . $row['Item_Name'] . "value=" . $row['Item_Name'] . ">"
$items is the data returned by my query of the mysql table. Currently none of the echoes inside the if are triggering so I think something is wrong with my if statement, but I'm to new to php to know what is wrong exactly.
Your problem is in your checkbox setup; you are missing quotes around the name and value attributes. Try this instead:
echo "<input type='checkbox' name=\"" . $row['Item_Name'] . "\" value=\"" . $row['Item_Name'] . "\">";
I have a page that is running an SQL query. I am displaying information for each row that the query results in. I am now trying to implement a way to update the information for the things being displayed.
My understanding is that in order to get information from one page to another you need to use sessions.
My code is displaying the information from the MySQL tables, then underneath it is giving the user the choice to edit the information in a form then send it to another file
One way of easily doing this is to use <input type="hidden"> so that you can include $row['Toy_ID'] in your form.
Something like this:
$row = $result->fetch_assoc();
while ($row){
echo "Toy Name: " . $row['Toy_Name'] . "<br>" .
"Store Name: . $row['Store_Name'] . "<br>" .
"Cost: " . $row['Cost'] . "";
echo "<form action='update.php' method='post'>" .
"<input type='hidden' name='toyid' value='".$row['Toy_ID']."'>" . // here's the hidden input, which you can call by using `$_POST['toyid']`
"<label>Toy Name: </label><input name='tname'; value='" . $row['Toy_Name'] . "'><br>" .
"<label>Store Name: </label><input name='storename'; value='" . $row['Store_Name'] . "'><br>" .
"<label>Cost: </label><input name='cost'; value='" . $row['Cost'] . "'><br>" .
"<input type='submit' value='Submit'>" .
"</form></div><br><br>";
$row = $result->fetch_assoc();
}
Then change your query to make use of $_POST['toyid'] instead of $_SESSION['toyid']
First of all, I am a newbie when it comes to coding, so please be kind and patient :)
What I am trying to do is to select two rows ('ID', 'name') from a MySQL table (categories), populate a drop down list with one row ('name'), and on submission of a form, pass the other ('ID') to another table.
Now, I can populate the drop down list, no problem. I have populated this with both 'ID' and 'name' to test that both of the variables I am using to hold this information, contain the correct data. But I cannot seem to $_POST the information.
I guess I am either looking at the wrong part of the array, or I am simply using the wrong code.
This is the code to create a new product, under a category from the database.
<?php
include 'db_config.php';
?>
<form enctype="multipart/form-data" action="insert.php" method="post">
<h3>Add New Product</h3>
Category:
<!-- START OF categories (table) names (row) SQL QUERY -->
<? $sql = "SELECT ID, name FROM categories";
$result = $mysqli->query($sql);
echo "<select name='category_name'>";
while ($row = $result->fetch_assoc()) {
$cat_ID=$row['ID'];
$cat_name=$row['name'];
extract($row);
echo "<option value='" . $cat_ID . $cat_name . "'>" . $cat_ID . " " . $cat_name ."</option>";
}
echo "</select>";
?>
<!--END OF SQL QUERY -->
<br>
Code: <input type="text" name="code"><br>
Name: <input type="text" name="prod_name"><br>
Description: <input type="textarea" name="description"><br>
Image: <input type="file" name="image"><br>
<input type="Submit">
</form>
For now, I am just echoing this out in the insert.php script, to test the code above. This is a snippet of the insert.php script.
echo "ID: " . $_POST['$row["ID"]'] . "<br>";
echo "Category: " . $_POST['$row["name"]'] . "<br>";
echo "Code: ". $_POST['code'] . "<br>";
echo "Name: " . $_POST['prod_name'] . "<br>";
echo "Description: ". $_POST['description'] . "<br>";
echo "Image: " . $_POST['image'] . "<br>";
Don't worry about the last line above. I know this needs to be $_FILES, and I have all this covered. I have stopped writing the data to the table until I get my issue fixed. In the full script, image are being upload to "/images" and the location stored in the table. This all works fine.
The problem is with the first two lines, as they are blank when returned. I thought I was storing the information correctly, as I am calling the same variables to populate the drop down list, but I cannot seem to $_POST it.
Does that makes sense?
Thanks to all who help me. Once day I will be as good as you....I hope.
TIA
Smurf.
this bellow:
echo "ID: " . $_POST['$row["ID"]'] . "<br>";
echo "Category: " . $_POST['$row["name"]'] . "<br>";
is wrong, select element has its name category_name, so, instead of this, you should
do:
echo "Category: " . $_POST['category_name'] . "<br>";
echo "ID: " . $_POST['$row["ID"]'] . "<br>";
echo "Category: " . $_POST['$row["name"]'] . "<br>";
There aren't any form elements with those names in your form ($row["ID"] and $row["name"]). Those would be really strange names for a form element anyway. The form element you're creating is:
<select name='category_name'>
So the selected value would be posted as:
$_POST['category_name']
The option elements for that select appear to have values which are a combination of ID and Name:
"<option value='" . $cat_ID . $cat_name . "'>"
Thus, if the user selects an option with a value of 1SomeName then $_POST['category_name'] will evaluate to '1SomeName'.
It's certainly unconventional to use the combination of ID and Name for the option values, but it should work. The problem is presents is that you now have a composite string which needs to be parsed in order to be useful. Generally what one would do is just use the ID as the value and the Name as the display. All you should need to use it throughout the code is the ID.
The $_POST variable you want, is inside category_name
Cuz your select is...
<select name='category_name'>
So you need to get it by...
$_POST['category_name'];
Which will return whatever you've assigned to the select options...ie
2 Name
2 being the ID, and Name being the name
But if you then want to use that ID to retrieve from DB or anything, you're gonna have to explode that apart...like so....to get each piece.
$array = explode(" ", $_POST['category_name']);
That will leave you with...
$array[0] = ID
$array[1] = Name
But I would avoid all that part, by just assigning the ID to the value only...like so..
echo "<option value = '".$cat_ID."'> ".$cat_name." </option>";
That way you just pass the ID and have access to it on the other side.
I am pulling addresses out of a mysql database. The addresses are displayed in a form, through an array, as a list with a (hidden) input and a (submit) input attached to each address. The intension is that a user can click on the (submit) input by the address they want to view and the (hidden) input would pass the ID (as_id).
What I coded works to a point. It displays the addresses and an input at each address.
What does not work is that once a (submit) is clicked for the selected address it passes " " no matter which (submit) input is chosen.
I’ve read about turning the input into an array with “[]” after the “name”. I playsed with that for a while. I got a “strip_tags() expects parameter 1 to be string, array” error and got lost for a few hours on that. I gave it up; didn’t think I was going in the right direction.
`enter code here`
echo "<div class='searchres'>";
echo "<form name='choose' method='post' id='choose' action='asset_search.php'>";
echo "<table>";
while ($asset_info = mysql_fetch_array($search_rs)) {
array_pop($asset_info);
echo "<tr>";
echo "<td width='400px'>";
echo $asset_info['as_id'] . " ";
echo $asset_info['as_st_number'] . " ";
echo $asset_info['as_st_dir'] . " ";
echo $asset_info['as_st_name'] . " ";
echo $asset_info['as_st_desig'] . " ";
echo $asset_info['as_unit_num'] . " ";
echo $asset_info['as_city'] . " ";
echo "<br />";
echo "</td>";
echo "<td>";
echo "<input name='hide' type='hidden' value='" . htmlspecialchars($asset_info
['as_id']) . "'>";
echo "<input name='search' type='submit' id='search' value='View Property'>";
}
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</form>";
echo "</div>";
I only need to pass the as_id, not the whole array. Any help would be appreciated.
I am assuming you want to check this line:
echo "<input name='hide' type='hidden' value='" . htmlspecialchars($asset_info['as_id']) . "'>";
You use the name hide, this should be hide[] then you should check for the value of $_POST['hide'].
Edited:
But there is something else wrong with your code. You use one form for all addresses, with the same button for all addresses. You need to make a different form tag for each address. In that case you do not need to put [] behind the input name, although I would pick another name than 'hide'. Put the whole form in the while loop.
i have this quick issue please.
I have this code here which permits me to extract a user name and a photo, and when the name is clicked it takes me to this hostess.php file, well, i need to pass the id variable to the hostess.php and save it, in order to get information only for that id..
Here is the code:
while ($row = mysql_fetch_array($query)) {
echo "<div id='photo'>";
echo "<div id='picture'>";
echo "<td> <img src=foto/photo1/".$row['photo'] . "></td>";
echo "</div>";
echo "<div id='text'>";
echo '<td>'. $row['first_name_en']." ". $row['family_name_en']."</td>";
echo "</div>";
echo "</div>";
}
How can i just get the id and then how can i save it to the $id variable
Thanks
The table structure is like this:
The table name is called hostess and the field i need to retrieve from hostess is the [id]
Change the line:
echo '<td><a href="hostess.php">'. $row['first_name_en'] .
" ". $row['family_name_en']."</a></td>";
to this:
echo '<td><a href="hostess.php?id={$row[id]}">'. $row['first_name_en'] .
" ". $row['family_name_en']."</a></td>";
and in hostess.php, use this to extract the value:
$id = $_GET['id'];
EDIT
Here is another method to pass variable as POST. Again, change the aforementioned line to:
echo '<td><form method="POST" action="hostess.php">' . "<input type='hidden'
name='id' value='{$row[id]}' />" . "<input type='submit' value='" .
$row['first_name_en'] . " " . $row['family_name_en'] .
" /></form></td>";
You can use CSS to style that button as simple text too. And the value can be retrieved in hostess.php as follows:
$id = $_POST['id'];
Add/change this in your code:
$personID = $row['id']; #or whatever the field name is
echo '<td>'. $row['first_name_en']." ". $row['family_name_en']."</td>";
The addition of "?personID='.$personID will send the value in the GET statement, or within the URL.
On the receiving page get the value that way:
$personID = $_GET['personID']+0; #add zero to force a numeric value--be sure to scrub your data!
#be sure to do other validation if needed!