Passing a single variable from an array through an input - php

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.

Related

Save multiple values from checkboxes in php

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.

What will prevent a submit button of a form from working?

I am on the process of modifying old code. I want to have set of input fields as rows in a table with Update button along with them. Below code is included in seperate ajax_edit_designation.php page which is being called by the edit_designation page. When the update is selected it should enter the new value to the database. The script required for it is written in the edit_designation.php page.
I would like to know whether what I am doing is correct or wrong. Because for some reason it doesn't carryout required action when the 'Update' button is selected. In the browser console it doesn't throw any exceptions either. What am I doing wrong?
Is it something that happen due to some kind of version issue? Or am I really doing something wrong?
echo "<table>";
while($row_tay = mysql_fetch_array($result_tay)){
$temp++;
$designation = $row_tay['designation'];
$designationID = $row_tay['designationID'];
echo "<tr ";
if($temp%2 == 1)
{
echo " bgcolor =\"#F3F3F3\" ";
}
else
{
echo " bgcolor =\"#DBDBDB\" ";
}
echo " >";
echo "<form id=\"myform".$designationID."\" method=\"post\">";
echo "<td class=\"bodytext\">";
echo "<input type=\"text\" id=\"designation\" size=\"50\" value=\"".htmlentities($designation)."\" name=\"designation\" />";
echo "</td>";
echo "<td width=\"120\" align=\"center\">";
echo "<input name=\"addcat\" id=\"addcat\" type=\"submit\" value=\"Update\" class=\"Submit_Button_long\">";
echo "<input type=\"hidden\" name=\"designationID\" value=\"".$designationID."\">";
echo "</td>";
echo "</form>";
echo "</tr>";
}
echo "</table>";
Because, same thing happened to me in another page also, where I added seperate form tags as above. In that case, previously it had given all the Update buttons under same tag. When I remove that earlier form tag and add only the new tags as above, they didn't work as now. But if I add that previously available tag, by wrapping all those forms created with the table, Update buttons worked. What could be the reason. Please help?

Accessing a variable from another file in PHP, SQL

<?php
$paseoLocationTo=$_POST['locationTo'];
$paseoLocationFrom=$_POST['locationFrom'];
$PTime=$_POST['time'];
echo"The value of Location to is $paseoLocationTo </br>";
echo"The value of Location from is $paseoLocationFrom </br>";
echo"The value of time is $PTime </br>";
mysql_connect('localhost', 'root', '')
or die(mysql_error());
mysql_select_db('shuttle_service_system')
or die(mysql_error());
$TripID =mysql_query("
SELECT DISTINCT Trip_ID as 'TripID'
FROM trip
WHERE Timeslot LIKE '$PTime' AND Location_From Like '$paseoLocationFrom' AND Location_To LIKE '$paseoLocationTo'
");
echo "<form action='LastPage.php' method='post'>";
while($check = mysql_fetch_array($TripID))
**echo "<name='TripID' id='TripID'>" . $check['TripID'] . " ";**
echo "<p class='sure'> Are you sure with your reservation? </p>";
echo"<input type='submit' value='Submit' class='Log'>";
echo"</form";
?>
From another php file, this the LastPage.php
<?php
**$TripID=$_POST['TripID'];
echo"The value of trip ID is $TripID </br>";**
?>
Hi guys I was wondering why I can't access the "TripID" variable in the other php file? I was accessing it before but now there seems to be a problem, am I doing it right? I'm sorry a php and SQL newbie.
You need to add
<input type="text" name="TripID" value="'.$check['TripID'].'" ... />
in your form in order to retrieve values with $_POST['TripID'].
There is no such thing as
**echo "<name='TripID' id='TripID'>" . $check['TripID'] . " ";**
which was found in your code.
**echo "<name='TripID' id='TripID'>" . $check['TripID'] . " ";**
Looks like that should be:
echo "<input type='text' name='TripID' id='TripID' value='" . $check['TripID'] . "' />";
If you don't want it to be editable, display it then add a hidden field:
echo $check['TripID'];
echo "<input type='hidden' name='TripID' id='TripID' value='" . $check['TripID'] . "' />";
Basically, you're not putting your trip id into an actual form tag, so it's not getting posted over to your LastPage.php.
Edit: fixed the first input to wrap the tripID in the value attribute.
Replace following lines
**echo "<name='TripID' id='TripID'>" . $check['TripID'] . " ";**
with
echo "<input type="hidden" name="TripID" value="'.$check['TripID'].'" />";
So it will not be visible to user on current page but when you will post the Form, it will be available in $_POST['TripID'] variable.
One more thing your tag is not properly closed.
Use MySQLi and prepared statements to prevent SQL injection.
PS: accept the answer if its work for you.
Your form tag is not closed properly. It is now as echo </form";
It should be echo"</form>";
Also you didnt added name in input tag.
It should be
echo"<input type='submit' value="$check['TripID']" name="TripID" class='Log'>";
There is no name tag <name> but you used how?

$_GET value from <button>?

I'm working on trying to switch out a hidden input with a value set to a button with a value set in order to have more than one possible outcome from the same form.
echo "<button class='btn-mini btn' type='submit' formaction='inc/delete.php' value='" . $record['id'] . "'><i class='icon icon-remove'></i></button>";
Once, submitted I try to assign the value to a variable and dump it:
$getid = $_GET["id"];
var_dump('$getid');
But, I end up with this error:
Notice: Undefined index: id in C:\xampp\htdocs\address-book\inc\delete.php on line 5
string(6) "$getid"
This there a different way to pull the value of a <button> verse that of a <input> using $_GET?
There is a difference between using single quotes and double quotes.
Simple quote will not parse variables but, understand the input as 'string' only!
So, in essence:
suppose: your URL as : ?id=3
$getid = "hello world";
echo '$getid'; // O/P will be $getid
echo "$getid"; // O/P will be hello world
coming to your question, you need to define a name for anything that is submitted in the form:
echo "<button class='btn-mini btn' type='submit' formaction='inc/delete.php' value='" . $record['id'] . "'><i class='icon icon-remove'></i></button>";
should be:
echo "<button class='btn-mini btn' name='id' type='submit' formaction='inc/delete.php' value='" . $record['id'] . "'><i class='icon icon-remove'></i></button>";

Passing parameter to php to get specific information

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!

Categories