Cannot receive post parameter using forms created by PHP echo - php

I am making a webpage that allows you to add friends. For the confirm part I made a confirm button that made a post redirect to the confirm page:
while($rs=$friendsToConfirm->fetch_row()) {
echo "Friends $count to confirm's ID: $rs[0]";
echo "
<form name=\"confirm\" method=\"post\" action=\"confirmfriend.php\">
<input type=\"hidden\" name=\"userID\" value=\"$userID\">
<input type=\"hidden\" name=\"friendID\" value=\"$rs[0]\">
<input type=\"submit\" value=\"Confirm\">
</form>
";
echo "<br>";
$count++;
}
The $friendToConfirm variable is all the friends needed to confirm and is retrieved from MySQL. When I hit the button, I was expected to receive the userID and friendID from confirmfriend.php, but I did not receive anything using $_POST['userID'];. Is there another way to do this or an I doing something wrong.

This is because all of your forms have the same name [confirm]. You can try this:
$sl = 1;
while($rs=$friendsToConfirm->fetch_row()) {
echo "Friends $count to confirm's ID: $rs[0]";
echo "
<form name=\"confirm{$sl}\" method=\"post\" action=\"confirmfriend.php\">
<input type=\"hidden\" name=\"userID\" value=\"$userID\">
<input type=\"hidden\" name=\"friendID\" value=\"$rs[0]\">
<input type=\"submit\" value=\"Confirm\">
</form>
";
echo "<br>";
$count++;
$sl++;
}
Hope this will work.

Both of your hidden input fields are called "userId". I imagine the 2nd one needs to be renamed to "friendId".
All of the forms will also have the same name. It is usually preferred to ensure form names are unique.
If $_POST ['userId'] exists but is blank, check that $userId isn't blank.
To debug you can just var_dump the post array.

Related

Using PHP $_POST to fetch input from another PHP script's echo form

I understand the title is pretty ambiguous so let me explain in more detail. I am using PHP to output a form which will display certain user information, the information is that which the user had previously stored in the database. This part of the code is working fine and the data is being displayed as I wish. Once the data is displayed in the input boxes, I want the user to be able to edit the information and then once the user clicks the submit button the updateuserinfo.php script will be called:
while($return = mysql_fetch_assoc($result)) {
echo "<form action=UpdateUserInfo.php method=post>";
echo "<p> First Name: <input type=text name=firstname value= ".$return['FirstName']."/></p>";
echo "<p> Surname: <input type=text value= ".$return['Surname']." /></p>";
echo "<p> House Number: <input type=number value= ".$return['HouseNumber']." /></p>";
echo "<p> Address Line One: <input type=text value= ".$return['AddressLineOne']." /></p>";
echo "<p> Address Line Two: <input type=text value= ".$return['AddressLineTwo']." /></p>";
echo "<p> County: <input type=text value= ".$return['County']." /></p>";
echo "<p> Phone Number: <input type=number value= " .$return['PhoneNumber']." /></p>";
echo "<p> Email: <input type=email value= ".$return['Email']." /></p>";
echo "<p> Username: <input type=text value= ".$return['Username']." /></p>";
echo "<p> Password: <input type=password value= ".$return['Password']." /></p>";
echo "<p> User Type: <input type=text value= ".$return['UserType']." /></p>";
echo "<input id=submit type=submit value='Update Info' Info />";
echo "</form>";
In the script found in updateuserinfo.php I was hoping I could firstly display the users info i.e what the user has changed the input box values to and then update the database if the user confirms the changes. I am confident I will be able to update the information stored on the database as this is a simple SQL query but the problem I am having is that I can not display the information found in the input boxes when the user has made changes. I thought I could apply a name attribute to each input tag, like I have done with 'firstname' and then use:
echo "<p>First Name: " $_POST[firstname] "</p>";
in the updateuserinfo.php in order to display the information the user has entered in the input box. However this is not working. Any suggestions would be greatly appreciated as I'm extremely new to programming in PHP.
Thanks in advance.
Try to show the complete $_POST array by using this code:
var_export($_POST);
Also you are missing some quotes in your Form. For example the line
echo "<p> First Name: <input type=text name=firstname value= ".$return['FirstName']."/></p>";
should be
echo "<p> First Name: <input type=\"text\" name=\"firstname\" value=\" ".$return['FirstName']."\"/></p>";
or easier within single quotes (so you don't have to mask each double quote):
echo '<p> First Name: <input type="text" name="firstname" value= "'.$return['FirstName'].'"/></p>';
Read What is the difference between single-quoted and double-quoted strings in PHP? if you want to know what's the difference between single and double quotes in PHP
And in your update Script you are missing the dot operator and also the quotes:
echo "<p>First Name: " $_POST[firstname] "</p>";
should be
echo "<p>First Name: " . $_POST["firstname"] . "</p>";
You should read $_REQUEST array, not $_POST...
UPDATE
Sorry, I did never use $_POST... It has been introduced in 4.1.0.
So, only check your PHP version is >= 4.1.0...
Otherwise please post
print_r($REQUEST);
results...

Running own's php script upon pressin Paypal buy-now button

and thanks for the great help your community provides.
Here is a question regarding Paypal. The code below creates a buy-now non-hosted button. I would like to be able to do run my geoTestArray.php code, currently simply included, as the user presses the Buy Now button, and not when the button is displayed on the page.
Is this possible at all?
Thanks in advance,
Joe
<?php
include ('geoTestArray.php');
echo "<form action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\">
<input type=\"hidden\" name=\"business\" value=\"shop#mysite.com\">
<input type=\"hidden\" name=\"cmd\" value=\"$cmd\">
<input type=\"hidden\" name=\"amount\" value=\"$price\">
<input type=\"hidden\" name=\"shipping\" value=\"$shipping\">
<input type=\"hidden\" name=\"currency_code\" value=\"$currency\">
<input type=\"hidden\" name=\"shipto\" value=\"$shipto\">
<input type=\"hidden\" name=\"item_name\" value=\"$title\">";
if ($cmd == 'cart') {
echo "<input type=\"hidden\" name=\"add\" value=\"1\">";
}
include('./quantities2.php');
echo "<input type=\"hidden\" name=\"lc\" value=\"US\">
<input class=\"submit\" type=\"image\" src=\"$button\" border=\"0\" name=\"submit\" alt=\"$altaction\">
<img alt=\"\" border=\"0\" src=\"https://www.paypal.com/en_US/i/scr/pixel.gif\" width=\"1\" height=\"1\">
</form>";
?>
You can display only a form with one input, for example item_id.
After submitting this form you search for item details, execute your geoTestArray.php in backend and after that you can redirect your users to paypal using header("location: $query");
here's how I would do:
<?php
if( isset($_POST['do_action']) && $_POST['do_action'] != "" ) {
//check for non-empty item_id field
//run your geoTestArray.php script
//get item details from database and build your paypal query
$query = "https://www.paypal.com/cgi-bin/webscr&cmd=_xclick"; // add rest of paypal request fields separated by &
header( "Location: $query" );
}
?>
<form action='' method='post'>
<input type='hidden' name='item_id' value'your_item_id'>
<input type='submit' name='do_action' value='Buy'>
<form>
Hope I helped you! :d
Thanks everyone for comments and solution options. I have eventually chosen to modify an onSubmit javascript I had on my page, by adding:
function InterceptForm(formObj) {
var newshipto = httpGet('geoTestArray.php');
formObj.shipto.value = newshipto;
}
The above code makes use of the httpGet() function posted in this other stackoverflow post:
HTTP GET request in JavaScript?
To complete the puzzle:
The code below is in the html in my page
<script language="javascript">
// During onLoad, loop through all forms and for each form object do something
function InterceptForm(formobj) {
formobj.onsubmit = function ff() {
interceptform(formobj);
};
}
</script>
The code below gathers all forms on the page and runs the above javascript on each (NOTE: you may want to name your forms so that you only run the script on the relevant forms and not on all forms):
<script language="javascript">
function GetForms() {
var formsCollection = document.getElementsByTagName("form");
for(var i=0;i<formsCollection.length;i++)
{
if (formsCollection[i].name != "seecart")
{
InterceptForm(formsCollection[i]);
}
}
return true;
}
</script>
The code below is the onLoad function:
<script language="javascript">
function onLoadFunction () {
GetForms();
return true;
}
</script>
Cheers,
Joe

onclick action not working as intended with radio buttons

For the last 4 hours I've been struggling to get something to work. I checked SO and other sources but couldn't find anything related to the subject. Here is the code:
<?php
$email=$_SESSION['email'];
$query1="SELECT * FROM oferte WHERE email='$email'";
$rez2=mysql_query($query1) or die (mysql_error());
if (mysql_num_rows($rez2)>0)
{
while ($oferta = mysql_fetch_assoc($rez2))
{
$id=$oferta['id_oferta'];
echo "<input type='radio' name='selectie' value='$id' id='$id'> <a href='oferta.php?id={$oferta['id_oferta']}'>{$oferta['denumire_locatie']}</a>";
echo "</br>";
}
echo "</br>";
//echo "<input type=\"button\" id=\"cauta\" value=\"Vizualizeaza\" onclick=\"window.location.href='oferta.php?id={$oferta['id_oferta']}'\" />";
//echo " <input type=\"button\" id=\"cauta\"value=\"Modifica\" onclick=\"window.location.href='modifica.php?id={$oferta['id_oferta']}'\" />";
echo " <input type=\"button\" id=\"sterge\" value=\"Sterge\" onclick=\"window.location.href='delete.php?id=$id'\" />";
echo "</form>";
echo "</div>";
}
else
{
}
?>
The while drags all of the user's entries from the database and creates a radio button for each one of them with the value and id (because I don't really know which one is needed) equal to the entry's id from the db. I echoed that out and the id is displayed as it should so no problems there.
The delete script works ok as well so I won't attach it unless you tell me to. All good, no errors, until I try to delete an entry. Whatever I choose from the list of entries, it will always delete the last one. Note that I have two other inputs echoed out, those will be the "view" and "modify" buttons for the entry.
I really hope this is not JavaScript related because I have no clue of JS. I think this will be of major help to others having this problem. Please let me know if I need to edit my question before downrating. Thanks!
After edit:
This is the delete script, which as I said earlier works fine.
<?php
if (isset($_GET['id']))
{
$id = $_GET['id'];
echo $id;
require_once('mysql_connect.php');
$query = "DELETE FROM oferte Where id_oferta = '$id'";
mysql_query($query) or die(mysql_error());
//header('Location: oferte.php');
}
else
{
//header('Location: oferte.php');
}
?>
The session is started as well, like this:
<?php
session_start();
?>
The reason the last $id is deleted is because this line is outside/after the while loop:
echo " <input type=\"button\" id=\"sterge\" value=\"Sterge\" onclick=\"window.location.href='delete.php?id=$id'\" />";
You want to move this line inside the loop so that you have a button that executes delete for each radio button.
Update:
To have links to delete and
echo "<input type='radio' name='selectie' value='$id' id='$id'> ";
echo "<a href='oferta.php?id={$oferta['id_oferta']}'>{$oferta['denumire_locatie']}</a> ";
echo "<a href='delete.php?id=$id'>delete</a>";
Also I do not think the radio button is needed here at all since you are not really doing anything with it. You could simply echo out the value of your choice and have these links as follows:
echo $oferta['denumire_locatie'] . ' '; // replace $oferta['denumire_locatie'] with something of your choice
echo "<a href='oferta.php?id={$oferta['id_oferta']}'>{$oferta['denumire_locatie']}</a> ";
echo "<a href='delete.php?id=$id'>delete</a>";
echo "<br />";
The problem, in this case, is JavaScript related, yes. What I recommend you to do is to simply add a Remove link for each item.
echo "<a href='oferta.php?id={$oferta['id_oferta']}'>{$oferta['denumire_locatie']}</a>";
echo " - <a href='delete.php?id={$oferta['id_oferta']}'>Remove</a>";
echo "</br>";
Your $id is outside your while() loop.
The last one is getting deleted because the $id has the last one's value when the loops is exited.
Include all your code :
echo "</br>";
//echo "<input type=\"button\" id=\"cauta\" value=\"Vizualizeaza\" onclick=\"window.location.href='oferta.php?id={$oferta['id_oferta']}'\" />";
//echo " <input type=\"button\" id=\"cauta\"value=\"Modifica\" onclick=\"window.location.href='modifica.php?id={$oferta['id_oferta']}'\" />";
echo " <input type=\"button\" id=\"sterge\" value=\"Sterge\" onclick=\"window.location.href='delete.php?id=$id'\" />";
Inside your while loop.
When the rendered html reaches the browser, it will be something like this:
<input type='radio' name='selectie' value='1' id='1'> <a href='oferta.php?id=1'>TEXT</a>
<input type='radio' name='selectie' value='2' id='2'> <a href='oferta.php?id=2'>TEXT</a>
<input type='radio' name='selectie' value='3' id='3'> <a href='oferta.php?id=3'>TEXT</a>
<input type='radio' name='selectie' value='4' id='4'> <a href='oferta.php?id=4'>TEXT</a>
<br/>
<input type="button" id="sterge" value="Sterge" onclick="window.location.href='delete.php?id=5'" />
With this markup you won't be able to accomplish what you want without using javascript to update the onclick attribute whenever you select a radio button.
On the other hand, instead of using the client-side onclick event you can use the button's default behaviour, which is to submit the form.
You'll just have to set the action attribute:
<form method="post" action="http://myurl.php">
and write the myurl.php page which will just read the posted variable $_POST['selectie'] and call the delete method with the posted id.

Delete a row(s) from MySQL using an HTML Form

I am having difficulty deleting rows from my database.
I have a delete button in a form that when it is clicked then performs a DELETE FROM query but it doesnt work and Im wondering whether my theory is completly wrong (the theory being that having a form and submit button to INSERT data into a database works so why not use that to delete stuff? This is the code
$league_id = $_GET['id'];
$delete_entry = "<form action=\"".$_SERVER["REQUEST_URI"]."\ method=\"post\">
<input type=\"submit\" name=\"ooops\" value=\"Delete Entries\"></p>
</form>";
if ($_POST['ooops']) { //if the data is rubbish then delete and start again...
$delete_lge_sql = "DELETE FROM st_position WHERE league_id = '$league_id'";
$delete_lge_res = mysqli_query($statto, $delete_lge_sql)
or die(mysqli_error($statto));
}
When I click Delete Entries the page reloads and the URL looks like this
page.php?ooops=Delete+Entries
Many thanks for any help
You missed a double-quote in this statement
$delete_entry = "<form action=\"".$_SERVER["REQUEST_URI"]."\ method=\"post\">
<input type=\"submit\" name=\"ooops\" value=\"Delete Entries\"></p>
</form>";
Change it to
$delete_entry = "<form action=\"".$_SERVER["REQUEST_URI"]."\" method=\"post\">
<input type=\"submit\" name=\"ooops\" value=\"Delete Entries\"></p>
</form>";
Try :
$delete_entry = "<form action=\"".$_SERVER["REQUEST_URI"]."\ method=\"post\">
<input type=\"hidden\" name=\"ooops\" value=\"1\" />
<input type=\"submit\" value=\"Delete Entries\">
</form>";
This way, your $_POST['ooops'] variable will be equal to "1". Just make your test on this value and it will be ok.

Passing data between PHP webpages from a dynamically generated list

I have a PHP code which generates a dynamic list inside a form like the following, note that the list is built dynamically from database:
echo '<form name="List" action="checkList.php" method="post">';
while($rows=mysqli_fetch_array($sql))
{
echo "<input type='password' name='code' id='code'>";
echo "<input type='hidden' name='SessionID' id='SessionID' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
}
What I need is to POST the data corresponding to the user choice when he clicks on the button for that row to another page.
If we use hyperlinks with query strings there will be no problem as I'll receive the data from the other page using a GET request and the hyperlinks would be static when showed to the user.
Also I need to obtain the user input from a textbox which is only possible with POST request.
Simply from the other page (checkList.php) I need these data for further processing:
$SessionID=$_POST['SessionID'];
$Code=$_POST['code'];
As I have a while loop that generates the fields, I always receive the last entry form the database and not the one corresponding to the line (row) that the user chosed from the LIST.
I'm going to recommend that you clean up the names of variables so that your code can
at least tell us what it's supposed to do. It should be rare that someone looks at your code
and has a lot of trouble trying to see what you're trying to accomplish :P, ESPECIALLY when you need help with something ;]. I'm going to try some things and hope that it makes doing what you want easier to comprehend and perhaps get you your answer.
It's good to try your best to not echo large amounts of HTML unnecessarily within a script , so firstly I'm going to remove the
echos from where they are not necessary.
Secondly, I'm going to use a mysql function that returns an easier to process result.
$user = mysqli_fetch_assoc($sql)
Third, I don't know if form having a name actually does anything for the backend or frontend of php, so I'm
just going to remove some of the extra crust that you have floating around that is either invalid HTML
or just doesn't add any value to what you're trying to do as you've presented it to us.
And yes, we "note" that you're building something from the database because the code looks like it does =P.
I'm also sooo sad seeing no recommendations from the other answers in regard to coding style or anything in regard to echoing html like this :(.
<?php while($user = mysqli_fetch_assoc($sql)): ?>
<form action="checkList.php" method="post">
<input type='password' name='code' value='<?php echo $user['code'] ?>' />
<input type='hidden' name='SessionID' value='<?php echo $user['id'] //Whatever you named the field that goes here ?>' />
<input type='submit' value='Take Survey' />
</form>
<?php endwhile; ?>
i not sure this is correct
echo '<form name="List" method="post">';
while($rows=mysqli_fetch_array($result))
{
echo "<input type='password' name='code' id='code'>";
echo "<input type='button' value='Take Survey' onclick=show($rows[0])>";
echo "<br>";
}
and javascript
<script>
function show(id)
{
alert(id);
window.location="checkList.php?id="+id;
}
</script>
On checkList.php
$id=$_GET['id'];
echo $id;
You can just check in checkList.php whether $_POST['code'] exists and if exists retrieve $_POST['SessionID'] which will be generated from database. But one thing, if You have all hidden fields in one form, they all will be sent, so You need to think how to correct that - maybe seperate forms for each hidden field, submit button and maybe other POST fields.
And afterwards, You will be able to get data in the way You need - $SessionID=$_POST['SessionID'];
I suppose it is the easiest way to solve that.
You can try something like this:
while($rows=mysqli_fetch_array($sql))
{
$index = 1;
echo "<input type='password' name='code' id='code'>";
//Attach $index with SessionID
echo "<input type='hidden' name='SessionID_$index' id='SessionID' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
}
On checkList.php
<?php
$num = explode('_', $_POST['SessionID']);
$num = $num[1];
//in $num you will get the number of row where you can perform action
?>
$form = 1;
while($rows=mysqli_fetch_array($sql))
{
echo '<form name="List_$form" action="checkList.php" method="post">';
echo "<input type='password' name='code' id='code_$form'>";
echo "<input type='hidden' name='SessionID' id='SessionID_$form' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
echo '</form>';
$form++;
}

Categories