Multiple Submits on a page - php

I have this PHP code:
$result = mysql_query("SELECT distinct om_quote_no from `porders` order by om_quote_no desc") or die(mysql_error());
echo '<select name="project_no">';
while ($row = mysql_fetch_array($result)) {
echo "<option value=".$row['om_quote_no'].">".$row['om_quote_no']."</option>";
}
echo '</select>';
?>
<input type="submit" value="Submit" />
</form>
<div id="table">
<?php
if($_GET){
So as you can see the PHP is forming a dropdown input and once submitted its going to execute some code, but what i need to do is have two dropdown inputs and therefore two submit buttons, however i'm not sure how to form the PHP if statement to distinguish which submit was pressed, so i'll have(pseudo):
if (submit1){
}
if (submit2){
}
Is that possible?

If you give your <input type="submit"> elements names, the one that is clicked will have its name and value sent to the server.
<input type="submit" value="Submit" name="submit1">
if clicked will send submit1=Submit to the server. You could therefore check with if ($_GET['submit1']) to see if it was pressed.

This isn't the best way but can do something like this too:
<select name="test" onchange="document.location ='test.php?submit=dropdown1'">
<option>test</option>
<option>test1</option>
</select>
<br><br>
<select name="test1" onchange="document.location ='test.php?submit=dropdown2'">
<option>test2</option>
<option>test3</option>
</select>
within test.php file:
if($_GET['submit'] == 'dropdown1')
{
print "One";
//statements to execute
}elseif($_GET['submit'] == 'dropdown2'){
//statements here to execute
print "Two";
}

<input type="submit" value="Submit 1" name="submit1"/>
<input type="submit" value="Submit 2" name="submit2"/>
<?php
if(isset($_POST['submit1'])){
//do stuff
//grab select option
$select_option=$_POST['project_no'];
}else if(isset($_POST['submit2'])){
//do stuff
}else{
//do stuff
}
?>
Or if your method is GET than change $_POST to $_GET :)

Related

Keep select list on reload

I have a select list, but on page reload , the data in the list is not saved of corse.
I have fixed this with TextBoxes and Radio buttons by reading the variables from $_GET.
Here is an example of the form I have now:
<form action="" id="exampleForm" method="get">
<input type="checkbox" name="exampleCheckbox" <?php if (isset($_GET['exampleCheckboxStatus'])) {echo "checked";} ?>>Check this
</br>
<select name="exampleList" multiple>
<option>Apple</option>
<option>Banana</option>
<option>Cherry</option>
</select>
<input type="submit" value="Submit" id="submitButton"> </form>
I would like to keep the values of the 'exampleList' once submitted
(I stay on the same page)
I have seen posts on here that almost look like what I ask, but most of them want to use javascript. Is there an solution for my problem, wich look similiar to what I already have right now? I would like to fix this with php because I dont think I have enough knowledge of Javascript (yet)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
var opts = localStorage.getItem('opts'); // get selected items from localStorage key
opts = opts.split(','); // split result from localstorage to array
$('#exampleList').val(opts); // select options with array
});
</script>
<html>
<body>
<select id="exampleList" multiple>
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="cherry">Cherry</option>
</select>
</body>
</html>
When you POST the form you only need to write the selected option values, comma separated, to the localstorage.
I finally found a solution:
The only flaw is the order of the :)
But since I use a plugin for displaying it does not matter much.
The fix:
I created 2 Array lists
list1 with everying in it
list2 with all selected values
Then I subtract list2 from list1 and dont have duplicates
So I can print both in different print methods.
<?php error_reporting(E_WARNING);
$fruitArray = array("Apple", "Banana", "Cherry", "Durian", "Eggfruit", "Fig", "Grapefruit");
$selectedFruitArray = $_GET['exampleList'];
$fruitArray = array_diff($fruitArray, $selectedFruitArray);
?>
<form action="" method="get">
<select name="exampleList[]" multiple>
<?php
foreach($fruitArray as $value) {
echo "<option value='$value'>$value</option>";
}
foreach($selectedFruitArray as $value) {
echo "<option value='$value' selected>$value</option>";
}
?>
</select>
<input type="submit">
</form>
Use FormRepo, a plugin specially made for retaining form data
on page refreshes.
Its usage is also simple:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="FormRepo.js"></script>
<script>
var $form = $('#input')
, $output = $('#output')
, repo = new FormRepo('restclient')
;
// get the last submitted values back
repo.restore($form/*, $form.attr('id')*/ ); // don't necessarily need an identifier
$form.submit(function (e) {
// preserve the last submitted values
repo.preserve($form/*, $form.attr('id')*/ ); // don't necessarily need an identifier
});
console.log( repo.all() );
</script>
You can do it by using session. This is the way using it you can store last selected value in session. Session value will not be destroyed even if you reload paga.
For e.g.,
<?php
session_start(); // Other Code
<div>
<p>Subtitle needs to be
<input type="radio" name="subTitleRadio" <?php if ($_SESSION['subTitleRadio'] != "LIKE") echo "checked"; ?> value="LIKE">contain
<input type="radio" name="subTitleRadio" <?php if ($_SESSION['subTitleRadio'] == "=") echo "checked"; ?> value="=">be equal to
</p>
<input type="search" name="subTitleSearchBox" placeholder="filter for subtitle" class="chosenStyle" value="<?php echo $_GET['subTitleSearchBox'];?>">
</div> //Other Code
?>
PHP Code for set value in session after submit :
<?php
session_start(); //Not required if your form action is on same page, else required //Rest code
$_SESSION['subTitleRadio'] = $_GET['subTitleRadio'] // OR $_POST['subTitleRadio']; // Rest code
?>
Same code works for me.
first of all at value parameters to the options, then you can check if exampleList has the right value and use that. for example:
<option value="apple" <?php if (isset($_GET['exampleList']) && $_GET['exampleList'] == "apple") echo "selected=\"selected\""; ?>>Apple</option>
Well, you could try something along these lines. It's a bit lengthy, you could shorten it up quite a bit. By showing it this way, I hope it's simpler to understand.
<form action="" id="exampleForm" method="get">
<?php
if (isset($_GET['exampleCheckboxStatus'])) {
echo '<input type="checkbox" name="exampleCheckbox" checked> Check this';
} else {
echo '<input type="checkbox" name="exampleCheckbox"> Check this';
?>
<br />
<select name="exampleList[]" multiple>
<?php
if( in_array('apple', $_GET['exampleList']) ) {
echo '<option value="apple" selected>Apple</option>';
} else {
echo '<option value="apple">Apple</option>';
}
if( in_array('banana', $_GET['exampleList']) ) {
echo '<option value="banana" selected>Banana</option>';
} else {
echo '<option value="banana">Banana</option>';
}
if( in_array('cherry', $_GET['exampleList']) ) {
echo '<option value="cherry" selected>Cherry</option>';
} else {
echo '<option value="cherry">Cherry</option>';
}
?>
</select>
<input type="submit" value="Submit" id="submitButton">
</form>
Note that I added [] to the select's name and corrected the br tag.
Adding [] will change the type from "string" (text) to an array (several texts). Then we can check what texts are included.
Try it for yourself, play around with the code a bit.

Session variable not working

I am using one session variable in my php page. As per my infomation, it is accessible throughout the program and it is, but problem is that it is showing different value for the same variable at different place in php page?
the code is as follows
<html><body>
<?php session_start();
if(!isset($_SESSION['x']))
$_SESSION['x']=1;
echo "X=". $_SESSION['x'];
?>
<form>
<input type="submit" name="save" value="save" />
</form>
<?php
if (isset($_GET['save']))
{
if(isset($_SESSION['x']))
$_SESSION['x'] = $_SESSION['x']+1;
echo $_SESSION['x']."<br>";
}
else
echo "no submit";
?>
</body></html>
value becomes different before and after submit button click? Please tell me why it is so?
thanks in advavnce.
You are redeclaring the value of session variable 'x' here
$_SESSION['x'] = $_SESSION['x']+1;
This is why its appearing 1 greater than its initial value.
it is due to the code itself
if(isset($_SESSION['x'])) //It is set
$_SESSION['x'] = $_SESSION['x']+1; //Add 1 to the value
echo $_SESSION['x']."<br>"; return value with +1
Solution
The reason the output is different is the order you echo and update
//Echo
//Update Value
//Echo again
Simple solution would be to move this
if (isset($_GET['save']))
{
if(isset($_SESSION['x']))
$_SESSION['x'] = $_SESSION['x']+1;
echo $_SESSION['x']."<br>";
}
else
echo "no submit";
to above this
if(!isset($_SESSION['x']))
$_SESSION['x']=1;
echo "X=". $_SESSION['x'];
Also note set the method and the action in the form to make sure it calls itself
<form method="GET" action="[url to itself]">
<input type="submit" name="save" value="save" />
</form>
Do it like this :
<html><body>
<?php session_start();
if(!isset($_SESSION['x']))
$_SESSION['x']=1;
echo "X=". $_SESSION['x'];
?>
<form method="GET" action="">
<input type="submit" name="save" value="save" />
</form>
<?php
if (isset($_GET['save']))
{
if(isset($_SESSION['x']))
echo $_SESSION['x']."<br>";
}
else
echo "no submit";
?>
</body></html>
this way the code prints out the same value after submit as it did before.
Either way you try if you print value and change after or change value and print after, when page reloads it will change value. you could add another button called increment and add the following code inside the php :
if (isset($_GET['inc']))
{
if(isset($_SESSION['x']))
$_SESSION['x'] = $_SESSION['x']+1;
}
and this one inside the form:
<input type="submit" name="inc" value="inc" />
this way youre variable increment when you press the inc button

PHP get selected value from select box?

This is my code for get database data to select box and i wanna get the seleceted value.I tries many ways but im missing something. help me
<form id="search" action="" method="post" >
<select name="owner" id="owner">
<?php
$sql = mysql_query("SELECT designation FROM designation");
while ($row = mysql_fetch_array($sql)){
echo '<option value="'.$row['designation'].'">'.$row['designation'].'</option>';
}
?>
</select>
<input type="submit" value="Search">
</form>
As you didn't specify an action for your form, the default will be to send the post values to the same page.
See this for more information about action value.
So, in the same page you have the form, you should add a
if(isset($_POST['owner']))
{
// Do some stuff
}
else
{
// Print the form
}
First make sure to include the action. Secondly to get a POST request of a select tag all you have to do is the following:
$_POST["owner"];
$_POST['owner'] contains the value of select box once you submit the form.And $_POST contains all the value of input elements you submitted via the form.if you print_r($_POST); it will show you all the values submitted through the form.
If you
echo $_POST['owner'];//Will display the value of your selected value in the select box.
<form id="search" action="" method="post" >
<select name="owner" id="owner">
<?php
$owner="rejayi"
$sql = mysql_query("SELECT designation FROM designation");
while ($row = mysql_fetch_array($sql)){
if($row['designation'] == $owner){
echo '<option value="'.$row['designation'].'" selected="selected">'.$row['designation'].'</option>';
}else{
echo '<option value="'.$row['designation'].'">'.$row['designation'].'</option>';
}
}
?>
</select>
<input type="submit" value="Search">
</form>
Put Double quotes (") outside and single quotes (') inside
eg:
echo "<option value='".$row['designation']."'>".$row['designation']."</option>";

Need Transfer Value PHP

I am creating a new website. For that website I will need to transfer a quantity and amount value from one if condition statement into another if condition statement. Both if statements are accessed by separate submit buttons named "checkamt" & "buy".
I need to transfer the "quantity", "value", and "net" values to and from the checkamt if statement to the buy if statement.
Here's my code:
<form action="index.php" method="post">
<input type="submit" name="checkamt" value="Check Amount"/>
<input type="submit" name="buy" value="Buy"/>
</form>
<?php
if(isset($_POST[checkamt]))
{
$qun=1;
$val=5000;
$total=$qun*$val;
}
if(isset($_POST[buy]))
{
echo $qun;
echo $val;
echo $total;
}
?>
I think that the problem you're having is that variables don't persist on page change. If you want that, you'll need to use a session. First, you must call session_start before anything, including HTML, is sent to the user. Then, you can use the $_SESSION variable.
<?php
session_start();
?>
<form action="index.php" method="post">
<input type="submit" name="checkamt" value="Check Amount"/>
<input type="submit" name="buy" value="Buy"/>
</form>
<?php
if(isset($_POST[checkamt]))
{
$_SESSION['qun']=1;
$_SESSION['val']=5000;
$_SESSION['total']=$qun*$val;
}
if(isset($_POST[buy]))
{
echo $_SESSION['qun'];
echo $_SESSION['val'];
echo $_SESSION['total'];
}
?>
Improve your English! Not sure if this is what you want, but if you want to share the values of your variables between the two ifs? You have to declare them at a higher scope than your if:
<?php
$qun = 0;
$val = 0;
$total = 0;
if(isset($_POST[checkamt]))
{
$qun=1;
$val=5000;
$total=$qun*$val;
}
if(isset($_POST[buy]))
{
echo $qun;
echo $val;
echo $total;
}
?>
Not sure if I'm getting the question right, but why not do something like this :
if(isset($_POST[checkamt]) || isset($_POST[buy]))
{
$qun=1;
$val=5000;
$total=$qun*$val;
echo $qun;
echo $val;
echo $total;
}
You need to track your variables between the different forms. You can use SESSION like Xeon06 suggested, or do the following. I'm only showing for $qun:
<?php
if(isset($_POST['checkamt'])) {
$qun=1;
}
if(isset($_POST['buy'])) {
echo $qun;
}
?>
<form action="index.php" method="post">
<input type="hidden" name="qun" value="<?php echo $qun; ?>" />
<input type="submit" name="checkamt" value="Check Amount"/>
<input type="submit" name="buy" value="Buy"/>
</form>

Checking Which Button was Clicked

I have a PHP generated form which consists of a list of items, each with a button next to them saying "Remove This" it outputs similar to below:
Item A - [Remove This]
Item B - [Remove This]
...
I wish to be able to click Remove This and it will detect which item it is, and then remove that from the database. Here's my code so far:
selectPlaces.php
<?php
include 'data.php';
mysql_connect($host, $user, $pass) or die ("Wrong Information");
mysql_select_db($db) or die("Wrong Database");
$result = mysql_query("SELECT * FROM reseller_addresses") or die ("Broken Query");
while($row = mysql_fetch_array($result)){
$placeName = stripslashes($row['b_name']);
$placeCode = stripslashes($row['b_code']);
$placeTown = stripslashes($row['b_town']);
$outputPlaces .= "<strong>$letter:</strong> $placeName, $placeTown, $placeCode <input type=\"button\" onclick=\"removePlace()\" value=\"Remove This\" /><br />";
}
mysql_close();
?>
Coupled with my admin.php
<div id="content" style="display:none">
Remove a Place<br><br>
<?php include 'selectPlaces.php'; echo $outputPlaces; ?>
</div>
I know I need to add some javascript to detect which button is clicked but I can't seem to get it working. I tried modifying the onclick="removePlace()" by perhaps passing a variable in the function removePlace(placeID) or something like that, but I'm new to JavaScript and I have no idea how to receive this in the removePlace function.
This seems easier to do without JavaScript. For each entry instead of generating just a button, generate a form that posts to a PHP script that does the deleting.
<form action="deletePlace.php?id=<?php echo $idOfThePlace?>">
<input type="submit" value="Remove This" />
</form>
$idOfThePlace would be the ID with you use to identify the data row.
You don't need JavaScript for that. Try running this example:
<?php var_dump($_POST); ?>
<form action="post.php" method="post">
<p>
<input type="submit" value="a" name="action" />
<input type="submit" value="b" name="action" />
</p>
</form>
You'll see that $_POST['action'] will depend on which button was pressed. For your example, you just need to set the value to identify the item that needs to be deleted. It might be useful to use the <button> element for that: <button name="delete" type="submit" value="12345">delete item 12345</button>. It'll show up as $_POST['delete'] with 12345 as value when submitted.

Categories