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
Related
I am working on a project where I want users to choose their space for parking and book it. I have a MySQL database which holds information about parking slots. I am fetching those values and display it using table and form. I have put checkboxes to make choice. and once they make choice they should be directed to payment page.
I am having problem with checkbox. I can see that in value fields it has values from database but when I hit submit button it doesn't pass any values to next page.
below is my code
<body>
<form method="POST" action="book.php">
<?php
//we create a table
echo "<table>";
// create table th
echo "<tr > <th> Parking Slot No </th> <th> Status </th>";
$sql=" select ParkingSlotNo,Status from fleming_dwing ";
$st=$conn->prepare($sql);
$st->execute();
$total=$st->rowCount();//get the number of rows returned
if($total < 1 ){//if no row was returned
echo "<tr> <td style> No Data: DataBase Empty </td> ";//print out error message
echo "<td> No Data: DataBase Empty </td> ";//print out error message
$ing = "<img src='img/occupied.png'/>" ;
}
else{
while($res = $st->fetchObject()){//loop through the returned rows
echo "<tr>";
if($res->ParkingSlotNo and $res->Status=='OCCUPIED')
{echo "<td> $res->ParkingSlotNo </td> ";
echo "<td> <img src='img/occupied.png'/> </td>";
echo"<td><a href=''> </a> </td>";
}
else
{
echo "<td> $res->ParkingSlotNo </td> ";
echo "<td> <img src='img/vacant.png'> </td>";
echo"<td><input type='checkbox' value='$res->ParkingSlotNo'></td>";
}
echo"</tr>";
}
}
?>
</table>
<input type="submit" value="Submit">
</form>
</body>
</html>
and this is the code for booking page
<?php
require_once("dbconfigpdo.php");
print_r($_POST);
?>
The checkboxes do not have name attributes. A form control can't be successful (included in the name=value pairs of data that are submitted) without one.
Any input must have a name attribute.
By name you can use a value. So, you need to add name="your name" to your checkboxes.
You need to set an attribute name to your input field, or it will not be processed.
Something like:
<input name='parkingslot' type='checkbox' value='$res->ParkingSlotNo'>
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 . "'>";
?>
This is my code for creating an html form that reads from a database and will allow the user to check and uncheck boxes for each of the 640 items. This is the form.php:
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned
if (mysql_num_rows($result) > 0) {
// yes
// print them one after another
echo "<html><body> <table cellpadding=10 border=1>";
while($row = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row['stickerID']."</td>";
echo "<td>" .$row['stickerName']."</td>";
echo "<td>".$row['stickerSection']."</td>";
echo "<td>"?>
<form name="some form" action="editform.php" method="post">
<input type="checkbox" name="<?php echo $row['stickerID'] ?>" value=" <?php echo $row['stickerStatus'] ?> ">
<?php "</td>";
echo "</tr>";
}
echo "</table></body></html>";
echo " " ?>
<input type="submit" name="editWish" value="Edit">
</form>
<?php " ";
} else {
// no
// print status message
echo "No rows found!";
}
The user must then be able to click on submit and have those values updated in the mysql database.
Right now when I click the submit button, it posts to edit form.php which has this:
<?php
//echo results
foreach($_POST['stickerID'] as $k=>$v ){
echo $k;
echo $v;
}
?>
But I don't get anything echoed. I was thinking the problem could be that Im actually creating a form for every row instead of 1 form with many rows/checkboxes. But when I move the form code after the and the tag to the line where line, I can't even load the form.php, it just loads blank.
Where is my problem? :) Thx
Name your checkbox like this:
<input type="checkbox" name="stickerID[]" value=" <?php echo $row['stickerStatus']; ?> ">
And as Amal already said update your code to PDO or MySQLi
you can do this with a tag :-
echo "<td>" .$row['stickerName']."</td>";
echo "<td>".$row['stickerSection']."</td>";
echo "<td>"?>
<form name="some form" action="editform.php" method="post">
<input type="checkbox" name="checkbox[]" value=" <?php echo $row['stickerStatus'] ?> ">
<?php "</td>";
echo "</tr>";
on your php code you get :-
$all_checkes_checkbox = $_POST['checkbox'];
here is your all checked checkbox:-
and this array also bale key and value
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
// Hy..I have the following piece of code:
mysql_select_db("baza_chestionar", $con);
$result = mysql_query("SELECT intrebari.descriere_intrebare,intrebari.nume_admin,intrebari.id_chestionar FROM intrebari WHERE intrebari.nume_admin='".$_SESSION['nume_admin']."' AND intrebari.id_chestionar='".$_SESSION['nr_chestionar']."' ");
$i=0;
while ($row = mysql_fetch_array($result))
{
$i++;
echo $i.") ". $row['descriere_intrebare'];
echo "<br><br>";
echo "<form method='POST' action='selectare_raspuns.php' id='myform' >
<input type='text' size='30' name='intrebare[]'>
</form> ";
echo "<br><br>";
}
echo "<input type='submit' value='Salveaza raspunsuri' onclick='myform.submit()'/>";
mysql_close($con);
// This select some data from a table and display it and for each data a textbox. I have another page that takes the data from the textboxes and insert it in another table. Here is the code:
foreach($_POST['intrebare'] AS $textbox)
{
$sql="INSERT INTO raspunsuri values ('','$textbox')";
var_dump($sql);
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
}
// But it only inserts the first value of the first text box. What am I doing wrong? I declared the name of the text box as an array an I loop thru with a foreach statement..
You're looping the myform form as as well. The FORM tag should be taken out of the while loop and the submit button should be within the form tag (so you can use it without JS). Also, just print_r your POST data to confirm you have the data there.
Your problem is that you use more than one form tag in your page. So only one value text box is send to your second page. You must declare the form tag before the while loop and you close the tag after your loop.
Your form tag is echoed inside the loop. Put is outside.
echo "<form method='POST' action='selectare_raspuns.php' id='myform' >\n";
while ($row = mysql_fetch_array($result))
{
$i++;
echo $i.") ". $row['descriere_intrebare'] ."<br /><br /><input type='text' size='30' name='intrebare[]'><br /><br />";
}
echo "<input type='submit' value='Salveaza raspunsuri' name="submit" />\n";
."</form>\n";