On my page, I have two dropdown boxes: One for a list of dates, and one for options. The box for dates is populated as such:
echo "<form name='selectedDate' action='page.php' method='GET' class='auto-style1'>";
echo "<select name='date' onchange='this.form.submit()'>";
echo "<option value=select>Select a Date</option>";
while ($row = mysql_fetch_array($result))
{
$date = date("m-d-Y",strtotime($row['date']));
if($date==date("m-d-Y"))
{
echo "<option>TODAY</option>";
}
else
{
echo "<option>" . $date . "</option>";
}
}
echo "</select>";
echo "<br />";
echo "<noscript>";
echo "<input type='submit' value='Select'>";
echo "</noscript>";
echo "</form>";
The other dropdown is populated as:
echo "<form name='selectedFormat' action='page.php' method='GET' class='auto-style1'>";
echo "<select name='option' onchange='this.form.submit()'>";
echo "<option value=select>Select a Option</option>";
echo "<option value=Artist>Artist</option>";
echo "<option value=Song>Song</option>";
echo "</select>";
echo "<br />";
echo "<noscript>";
echo "<input type='submit' value='Select1'>";
echo "</noscript>";
echo "</form>";
Now, you notice that both of them are set for Submit() upon change. If I change the first one, the URL in the browser becomes page.php?date=02-26-2013, and if I change the second one, the URL in the browser becomes page.php?option=Song.
The problem that I'm having, is that if I change the date to an older date, and then change the option, my page reverts back to the date being the current date. I've tried setting the action to a different URL:
$url = "musicInfo.php?date=" . $nonFormatedDate . "&option=" . $option;
And I changed both forms to have:
echo "<form action='" . $url . "' method='GET' class='auto-style1'>";
If I echo $url;, it correctly displays what the browser URL should be. However, the browser URL is still populated by whichever dropdown I used last.
My question here is, how do I get both variables to be passed upon either submission?
So for instance, if I change the date to be 01-13-2012, and then change the option to Song, how can I ensure that the date will be passed as well? And vice versa. Or, say if I change the date to 01-12-2012, is it possible to have the date dropdown box have this selected upon reload?
Site I'm trying to do: http://www.legendary-immersion.com/random/test.php
As people say in comment you can submit only one form per request.
So the best way is to hide data that you need in your form.
In your php file add this code
$date = isset($_GET["date"]) ? $_GET["date"] : "";
$option = isset($_GET["option"]) ? $_GET["option"] : "";
Then in first form add
<input type="hidden" name="option" value="<?php echo $option; ?>"/>
And in the second form add
<input type="hidden" name="date" value="<?php echo $date; ?>"/>
As the result in every form submit you will get the url like
page.php?date=yourdate&option=youroption
Best regards, Hope this will help
Related
I would like some help printing what is in a certain textbox that was created by an echo command.
while($row = $result->fetch_assoc()){
$stringTest = $row['Price'];
$AssetId = $row['AssetId'];
echo "<center><div> <h3>Cost: ".$stringTest."";
echo '<form action="" method="get"><input type="text" name="uid">';
echo "</br><input class='myButton' type='submit' Name='Submit1' VALUE='I have bought'></a></form>";
/** ^ Input value I would like to get *//
echo "<a href='https://www.roblox.com/item-item?id=".$AssetId."' class='myButton'>Buy</a></h3></div></center>";
}
Use the code below to get the value from submit:
if(isset($_GET['Submit1'])) {
echo $_GET['Submit1'];
}
When the user clicks submit, it will echo the value of it.
If you want to print PHP element in a textbox you should put it in the value tag of the input
<?php
echo "<input type='text' value='" . $val . "'>";
?>
I have built a table in PHP to display a list of people and data relating to them. I want the user to be able to select one of those users to send an e-mail request to. So I have put a button at the end of each row for every user.
Im not sure how to write the PHP script to identify which button has been pressed. I have tried assigning the name of the button to be the e-mail address of the user for the row but I dont know how to read that value in the corresponding script...
Code for initial page:
if ($query_run = mysql_query($query))
{
echo "<fieldset>";
echo "<table>";
while ($row = mysql_fetch_assoc($sqldata))
{
echo "<tr><th>Name</th>";
echo "<td>";
echo $row['FirstName']." ".$row['LastName'];
echo "<td>";
echo $row['Country'];
echo "<td>";
echo $row['Profile'];
echo "<td>";
echo $row['Email'];
$Email = $row['Email'];
echo "<td>";
echo '<input type = "submit" name="$Email" value="connect">';
}
echo "</table>";
echo "</fieldset>";
Thanks!
If you are planning to do this in HTML5, you could alter the submit button, so that it would redirect to a page, where you can get the e-mail by $_GET[];
echo "<input type=\"submit\" formaction=\"request.php?email=".$Email."\" value="Submit">\";
Then in your request.php you could use:
$email = $_GET['email'];
I think, you have to change name attribute from "$Email" to, I don't know, maybe "user_email" and assign $email variable to value attribute. So in script which must handle this form you will just need to retrieve a value of $_POST['user_email'].
Also I think it's better to use $Email in double quotes:
echo "<input name='$Email'" ...>;
because you will get string '$Email' instead of expected email address.
Or you can create a form for each table row.
There are any number of ways you could approach this, but I would personally choose to use an HTML form for this purpose. Consider the following HTML:
<form name="myForm" method="GET" action="">
<button type="submit" name="email" value="test1#email.web">test 1</input>
<button type="submit" name="email" value="test2#email.web">test 2</input>
<button type="submit" name="email" value="test3#email.web">test 3</input>
</form>
If you were to try to click on one of these buttons, you'd notice your query string will now show ?email=test2%40email.web (assuming you clicked the second button).
The gist of this approach is to use the same name attribute for your submit buttons, but assign a different value to each. I chose button over input due to the ability to present button text different from the value attribute (as described in this question).
Applying this principle in your PHP, you'd get something like:
while ($row = mysql_fetch_assoc($sqldata))
{
echo "<tr><th>Name</th>";
echo "<td>";
echo $row['FirstName']." ".$row['LastName'];
echo "<td>";
echo $row['Country'];
echo "<td>";
echo $row['Profile'];
echo "<td>";
echo $row['Email'];
$Email = $row['Email'];
echo "<td>";
echo '<button type="submit" name="email_address" value="' . $Email . '">Send email</button>';
}
Then, you just check your email_address parameter for the email address.
I have a form with 2 selects, when you send the first, the second select charges the values that are called on my oracle bd with a query, then when i send the second select, it generates a table with checkboxes:
if(isset($idTActi)){
$stallTableTarifas=oci_parse($conn, "SELECT TARIFAS.ID, TARIFAS.ID_TIPO_ACTIVIDAD, TARIFAS.TIPO, TIPO_ACTIVIDAD.TEMPS_KM, TARIFAS.PRECIO
FROM TARIFAS, TIPO_ACTIVIDAD
WHERE TARIFAS.ID_TIPO_ACTIVIDAD = TIPO_ACTIVIDAD.ID
AND TARIFAS.ID_TIPO_ACTIVIDAD = $idTActi");
oci_execute($stallTableTarifas);
echo "<div class='divPrecios'>";
echo "<table>";
echo "<tr class='tabPreciosTitles'>";
echo "<td>Tipus Tarifa</td>
<td>Temps/Km</td>
<td>Preu</td>
<td><input type='submit' class='carrito' value=''></td>";
echo "</tr>";
while (($row=oci_fetch_array($stallTableTarifas,OCI_BOTH))!=false){
echo "<tr>";
echo "<td>".$row['TIPO']."</td>";
echo "<td>".$row['TEMPS_KM']."</td>";
echo "<td>".$row['PRECIO']."</td>";
echo "<td><input type='checkbox' name='checkbox[]' value='".$row['ID']."'/></td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
}
echo "</form>";
The variable $idTActi it's the id that i return from the second select, so when i click on the checkboxes and i send it on the button named class='carrito', that's an sprite that i generate on css, i see on the bottom another table with the information that i selected on the previous table:
echo "<div class='divPrecios'>";
echo "<table>";
echo "<tr class='tabPreciosTitles'>";
echo "<td>Nom Activitat</td>
<td>Nom Tipus Activitat</td>
<td>Tipus Tarifa</td>
<td>Temps/km</td>
<td>Preu</td>";
echo "</tr>";
foreach($_POST['checkbox'] as $item){
$stallTableCarrito=oci_parse($conn, "SELECT ACTIVIDAD.NOM AS NOM_ACTIVIDAD, TIPO_ACTIVIDAD.NOM AS NOM_TACTIVIDAD, TARIFAS.TIPO, TIPO_ACTIVIDAD.TEMPS_KM, TARIFAS.PRECIO
FROM TARIFAS, ACTIVIDAD, TIPO_ACTIVIDAD
WHERE TARIFAS.ID = $item
AND TARIFAS.ID_TIPO_ACTIVIDAD = TIPO_ACTIVIDAD.ID
AND TIPO_ACTIVIDAD.ID_ACTIVIDAD = ACTIVIDAD.ID");
oci_execute($stallTableCarrito);
$array=array(
0=>array(),
1=>array(),
2=>array(),
3=>array(),
4=>array()
);
while (($row=oci_fetch_array($stallTableCarrito,OCI_BOTH))!=false){
array_push($array[0],$row['NOM_ACTIVIDAD']);
array_push($array[1],$row['NOM_TACTIVIDAD']);
array_push($array[2],$row['TIPO']);
array_push($array[3],$row['TEMPS_KM']);
array_push($array[4],$row['PRECIO']);
}
for ($x=0;$x<count($array[0]);$x++){
echo "<tr>";
echo " <td>".$array[0][$x]."</td>";
echo " <td>".$array[1][$x]."</td>";
echo " <td>".$array[2][$x]."</td>";
echo " <td>".$array[3][$x]."</td>";
echo " <td>".$array[4][$x]."</td>";
echo " <td><input type='submit' class='carritoElim' value=''></td>";
echo "</tr>";
}
}
echo "</table>";
echo "</div>";
Basically that's a shopping form.
And where is the problem? When i send the pushed checkboxes with the button class='carrito', the form by default refresh the page and clears my array, what can i do?
In your first part of code, is your form tag open ? (I guess it is if this one works)
In the second part, is your <input type='submit' class='carritoElim' value=''> tag in a form ?
Because if it's not, you gonna have a bad time ;-)
Maybe in the last form you should generate hidden input with same names as your first form and same values.
If you don't I guess your variable $idTActi won't be set anymore and it won't succeed the first test if(isset($idTActi)). That could be why you get a cleared page.
If you have a multi step form in the same php page, for this kind of html code :
<form method=POST url="myURL">
<select name="select1">[...]</select>
<select name="select2">[...]</select>
<!-- VARIOUS PART : may not be displayed -->
<div id="checkboxes">
<input type="hidden" name="boxStep" value="1"/>
<input type="checkbox" name="cb1" value="1"/>
[...]
</div>
<!-- END OF VARIOUS PART -->
</form>
Then you need php tests in this order :
// if post request
if (isset($_POST)) {
if (isset($_POST['boxStep'])) {
// behavior when checkboxes values are sent
} else {
if (isset($_POST['select2'])) {
// behavior when second select is filled
// display "VARIOUS PART"
} else {
// behavior when only first select is filled
// Do not display "VARIOUS PART"
}
}
} else {
// default behavior (no select filled)
// Do not display "VARIOUS PART"
}
Apolo
I have a php page with this code that passes a variable of a button to next page:
<div><center><table>
while($row = mysqli_fetch_array($result))
{
echo "<td><form action= display.php method= 'post'><input type='hidden'
name='projectid' value=".$row['projectid'].">
<input type= 'submit' name= 'type'
value= 'View/Amend Project Details'></form></td>\n";
}
echo "</table></div>";
I have this on my next page in a table:
$projectid= $_POST['projectid'];
echo "<td>" . $row['projectname'] . "</td>";
I still cannot see the problem, any idea?
The problem is that you're trying to use the $row on your second page, and it isn't set there.
You have to either do the mysqli query again, or transfer the value of $row['projectname'] through the form, using a hidden input field.
<?php
while($row = mysqli_fetch_array($result,,MYSQLI_ASSOC))
{
echo " name='projectid' value=".$row['projectid']."> value= 'View/Amend Project Details'>\n";
}
I tried to search but was unable to find an answer for this question.
I am trying to get the value of the button in my submit button that is a variable.
CODE is as follows
$penrequest = "select * from request where status='pending';";
$penreg = mysql_query($penrequest);
echo "<form method='post' action=''>";
while ($row = mysql_fetch_array($peneg))
{
echo "<input type='submit' name='answer' value='$appdeny'>";
}
if (isset($_POST['answer']))
{
echo $appdeny;
}
Ok the code works...if you hit the button its caught by the if statement like its supposedt o be. the variable $appdeny is a messageID number filled from a mysql database which can change. When the user clicks a button i want to print the messageID of the number displayed as the value of the answer button.
Change:
echo "<input type='submit' name='answer' value='$appdeny'>";
to:
echo "<input type='submit' name='answer' value='" . $row['appdeny'] . "'>";
Change:
echo $appdeny;
to:
echo $_POST['answer'];
You also need to do:
echo "</form>";
after the while loop.