$_GET value from <button>? - php

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>";

Related

Checking if $_POST variable is set using mysql variable

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'] . "\">";

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?

Passing a <td> value to a php script

Sorry if this is a noob question, but I'm still getting up to speed with PHP and can't find an answer to this one.
I have a php script that queries a mySQL table and then builds an HTML table from the results. This all works just fine. As part of that script, I add a <td> to each <tr> that gives the user a chance to delete each specific record from the database, one by one, if they so choose.
To make this work, I have to be able to pass over to the php script the unique identifier of that record, which exists as one of the values. Problem is, I don't know how to pass this value.
Here is my php script that builds the HTML table:
while ($row = mysql_fetch_array($result)) {
echo
"<tr class=\"datarow\">" .
"<td id=\"id_hdr\">" . $row['id'] . "</td>" .
"<td id=\"name_hdr\">" . $row['name'] . "</td>" .
"<td id=\"btn_delete\">
<form action=\"delete_item.php\">
<input type=\"image\" src=\"images/delete.png\">
</form>
</td>" .
"</tr>";
}
So, somehow I either need to explicitly pass 'id' along with "delete_item.php" and/or find a way on the php side to capture this value in a variable. If I can accomplish that I'm home free.
EDIT: Trying to implement both suggestions below, but can't quite get there. Here is how I updated my form based on how I read those suggestions:
"<td id='btn_delete'>".
"<form action='scripts/delete_item.php'>".
"<img src='images/delete.png'>".
"<input type='hidden' id='uid' value='" . $row['id'] . "'>".
"<input type='submit' value='Submit'/>".
"</form>".
"</td>" .
Then, in delete_item.php, I have this:
$id = $_POST['uid'];
$sql = "DELETE FROM myTable WHERE id=$id";
$result = mysql_query($sql);
if (!$result) {
die("<p>Error removing item: " . mysql_error() . "</p>");
}
But when I run it, I get the error:
Error removing item: You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near '' at line 1
And one final thing: this approach gives me a button with the word 'submit' directly under my image. I'd prefer not to have this if possible.
Thanks again!
<form action=\"delete_item.php\">
<input type=\"hidden\" value=\"$row['id']\" name=\"uid\" >
<input type=\"image\" src=\"images/delete.png\">
</form>
The unique id is placed in a hidden input. You can get this value using
$_POST['uid']
But you need to submit the form
<input type=\"submit\" name=\"submit\" value=\"delete\" ">
You could use an anchor tag with parameter for id.
ie, www.example.com/delete.php?id=20
Now you could get that id on page delete.php as $_GET['id']
Using that you could delete the data from the table and return to the required page by setting up header
If you required you could use the same logic with AJAX and with out a page reload you could permenently delete that data. I would recommend AJAX

Passing a single variable from an array through an input

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.

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