Here is my code in php. I have a php creditcard confirmation page, with 2 button, edit details and submit. I have an init file which will perform tasks based on what cc_confirm is and what editval is, confirm and editing details respectively.
if($_POST['cc_confirm1']=='y' && $_POST['$editval']!='y' && !isset($editval))
{echo '<input name="submitbtn" type="submit" value="Edit Details" /><input name="editval" type="hidden" value="y" /><input name="cc_confirm" type="hidden" value="n" />';
}
if($_POST['cc_confirm1']=='y' && $_POST['$editval']!='y' && !isset($editval)){
echo '<input name="submitbtn1" type="submit" value="Submit Card" /><input name="card1" type="hidden" value="y" /><input name="cc_confirm" type="hidden" value="y" />';
Now the problem is, because I am using two hidden items, always the one at the bottom is being executed. For this code, if i press on edit details, the details are being submitted, the credit card is being runned and then edit page is shown after that, which does not serve the purpose.
If i interchange both button codes, then even for submit card, it is showing only edit page details without submitting card. I have tried to change the name of the buttons but no use.How can i avoid this problem? Appreciate any effort to solve.
Why don't you split up the conditions by assigning a value to them
$continue = 0;
if($_POST['cc_confirm1']=='y'){
$continue++;
}
if($_POST['$editval']!='y'){
$continue++;
}
else{
$continue = 0;
$reason = 'editval';
}
if(!isset($editval)) {
$continue++;
}
else{
$continue = 0;
$reason = 'noeditval';
}
if($continue > 0){
echo '<input name="submitbtn" type="submit" value="Edit Details" /><input name="editval" type="hidden" value="y" /><input name="cc_confirm" type="hidden" value="n" />';
}
else{
if($reason == 'editval'){
//Process
}
elseif($reason == 'noeditval'){
//Process
}
}
Related
I'm having some difficulty updating a quantity value in a multi-dimensional array I've created and really hoping you can help me correct where I've gone wrong;
.
The background
I've got two "items" which both have a simple form tag followed by a hidden input field with a unique value (1 for the first item, 2 for the second).
The button will just point back to this same page using the POST method.
The div on the right of the page will then load a "basket" which will use these post values and add them to an array.
When the "add" button is used again the value should update to +1 rather than create another sub_array.
.
What is currently happening
Currently when I click "add" the first time it adds the array as expected;
However when clicking "add" for the second time it adds a second array rather than +1'in the quantity.
On the third time clicking "add" it does actually now find the original value and update it as I expected, if I click again and again it will continue to update the quantity
It just seems to be that second time I click "add".
.
The Script
<?php session_start();
function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
if (ISSET($_POST["prod"]))
{
if(in_array($_POST["prod"],$_SESSION["cart"])==TRUE)
{
$_SESSION["cart"][0] =
array($_POST["prod"],$_POST["name"],$_SESSION["cart"][0][2]+1);
}
else{
echo 'running else';
$_SESSION["cart"]=array($_POST["prod"],$_POST["name"],1);}}
if ($_POST['e']=='1')
{
$_SESSION['cart'] = '';
}
echo '<br /><br />';
print_r($_SESSION["cart"]);
}
Sample form
<form action="test.php" method="post" enctype="application/x-www-form-urlencoded">
MAST-O-MIR<br/>
img<br/>
£2.00<br/>
<input type="hidden" value="1" name="prod" />
<input type="hidden" value="MAST-O-MIR" name="name" />
<button class="plus-btn" type="Submit">Add</button>
</form>
Also, what you might well notice from my script is that when you "add" the second item it will actually overwrite the first one by creating the array from scratch so if you can help me with either or both of these I really would appreciate the expertise!
Many thanks to all in advance!
I tried to debug your code and a possible solution could be the following:
<?php
session_start();
if(!isset($_SESSION["cart"]))
{
$_SESSION["cart"]=[];
}
if (isset($_POST["prod"]))
{
$prod_id=$_POST["prod"];
//let suppose $_POST['prod'] is your item id
$found=false;
for($i=0;$i<count($_SESSION['cart']);$i++)
{
if(isset($_SESSION['cart'][$prod_id]))
{
echo "found! so add +1";
$_SESSION['cart'][$prod_id][2]+=1;
$found=true;
break;
}
}
if($found==false)
{
echo 'not found! so create a new item';
$_SESSION["cart"][$prod_id]=array($_POST["prod"],$_POST["name"],1);
}
}
if (isset($_POST['e']) && $_POST['e']=='1')
{
$_SESSION['cart'] = '';
}
echo '<br /><br />';
print_r($_SESSION["cart"]);
?>
<form action="cart.php" method="post" enctype="application/x-www-form-urlencoded">
MAST-O-MIR<br/>
img<br/>
£2.00<br/>
<input type="hidden" value="1" name="prod" />
<input type="hidden" value="MAST-O-MIR" name="name" />
<button class="plus-btn" type="Submit">Add</button>
</form>
<form action="cart.php" method="post" enctype="application/x-www-form-urlencoded">
MAST-O-MIR<br/>
img<br/>
£2.00<br/>
<input type="hidden" value="2" name="prod" />
<input type="hidden" value="MAST-O-MIR" name="name" />
<button class="plus-btn" type="Submit">Add</button>
</form>
Another way to do it is using associative arrays.
The following code creates a cart array in $_SESSION using the item name as key(so you don't need to loop over the cart array to find the item) and
an array with properties as name=>value for each item.
session_start();
if(!isset($_SESSION["cart"]))
{
$_SESSION["cart"]=[];
}
//let's suppose you have unique names for items
if (isset($_POST["prod"]))
{
$name=$_POST["name"];
if(isset($_SESSION['cart'][$name]))
{
echo "found! so add +1";
$_SESSION['cart'][$name]['quantity']+=1;
}
else
{
echo 'not found! so create a new item';
$_SESSION["cart"][$name]=array("id"=>$_POST["prod"],"name"=>$_POST["name"],"quantity"=>1);
}
}
if (isset($_POST['e']) && $_POST['e']=='1')
{
$_SESSION['cart'] =[];
}
echo '<br /><br />';
print_r($_SESSION["cart"]);
?>
<form action="cart2.php" method="post" enctype="application/x-www-form-urlencoded">
MAST-O-MIR<br/>
img<br/>
£2.00<br/>
<input type="hidden" value="1" name="prod" />
<input type="hidden" value="MAST-O-MIR" name="name" />
<button class="plus-btn" type="Submit">Add</button>
</form>
<form action="cart2.php" method="post" enctype="application/x-www-form-urlencoded">
MAST-O-MIR<br/>
img<br/>
£2.00<br/>
<input type="hidden" value="2" name="prod" />
<input type="hidden" value="MAST-OMIR" name="name" />
<button class="plus-btn" type="Submit">Add</button>
</form>
It's hard to test your code without a sample form but I guess both of your problems may be solved by replacing:
$_SESSION["cart"][0] = array($_POST["prod"], $_POST["name"], $_SESSION["cart"][0][2]+1);
For:
$_SESSION["cart"][0][2]+= 1;
By the way, try to properly indent your code when you are going to post it. It is hard to read.
I have two forms, one with login and another with logout and they both use the same controller/form processor,
I am using
$_SERVER['REQUEST_METHOD']
to see if the form is submitted.
But how can I know if login or logout was clicked.
$_POST['action'] == 'login'
and
$_POST['action'] == 'logout'
are not working.
okay here is the complete form :
<?php
$test = 'default';
if( $_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['action'] == 'login' ){
//do some stuff
$test = 'login';
}elseif( $_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['action'] == 'logout' ){
//do other stuff
$test = 'logout';
}
?><?php echo $test; ?>
<form method="post" action="">
<p>
<input type="text" name="username" placeholder="Username" value="">
</p>
<p>
<input type="password" name="password" value="" placeholder="Password">
</p>
<p>
<input type="submit" name="login" value="Log In">
</p>
</form>
<form method="post" action="">
<input type="submit" name="logout" value="Log Out">
</form>
But the $test doesn't change.
Give the buttons different names
If you have a input element of type submit, you will get a button that posts a value itself. Give it a name and a value, and that value will be posted along with the post data:
<input type="submit" name="button1" value="Click me">
<input type="submit" name="button2" value="Or me">
The value is often localized and I think you wouldn't want to check for that, but you can just check for the existence of Button1 or Button2 in the post variables to see which one was clicked, regardless what their value is.
Give the buttons (only) different values
Alternatively, if you know the button value is useful (it contains an id or name rather than a localized text from a template), you could give both buttons the same name (like 'action') and check the value of the post variable instead. In that case, the two buttons behave more or less like a group of radio buttons. Not my preference, but certainly possible and acceptable.
<input type="submit" name="action" value="Action 1">
<input type="submit" name="action" value="Action 1">
Or you could use a button tag in that case, which is a similar but slightly different element. It also has a name and value attribute, but you can specify the text as the content of the element, so you can decouple the value from the text you present to the user. This would be better than using the input version above.
<button type="submit" name="action" value="Action 1">Click me for action 1!</button>
<button type="submit" name="action" value="Action 2">Secondary action</button>
Note that type="submit" is the default for buttons, so you can omit it, as long as you don't need to support IE7.
Checking which one was clicked
Whichever solution you pick, don't forget to check thoroughly though. There are other ways of submitting a form, for instance through clicking enter, so make sure to propertly handle the case where neither button was clicked.
if (array_key_exists('button1', $_POST)) {
// Button 1 was clicked
} elseif (array_key_exists('button2', $_POST)) {
// Button 2 was clicked
} else {
// Neither was clicked.
}
or for the alternative
if (array_key_exists('action', $_POST)) {
switch ($_POST['action') {
case 'Action 1':
// Button 1 was clicked
break;
case 'Action 2':
// Button 2 was clicked
break;
default:
// An unknown button was clicked!
break;
}
} else {
// Neither was clicked.
}
Assign names to the buttons:
<input type="submit" name="Add" value="Add" />
<input type="submit" name="Delete" value="Delete" />
and verify which one is set as:
if (isset($_POST['Add'])) {
// add
} elseif (isset($_POST['Delete'])) {
// delete
}
You can identify through isset function:
if(isset($_POST['action']) && $_POST['action']=="login"){
//Login Button Logic
} else if(isset($_POST['action']) && $_POST['action']=="logout"){
//Logout Button Logic
}
I have two PHP files, lets call them fileOne.php and fileTwo.php. If fileOne.php has a form with action="fileTwo.php" and with the buttons button1 and button2, how do I check in fileTwo.php if isset($_POST['button1']) was pushed, or isset($_POST['button2'] was pushed.
This is the form for "fileTwo.php" in "fileOne.php": (execute.php = fileTwo.php)
< form action="execute.php" method = "POST">
< input type="hidden" name="edit" value="Edit"/>
< input type="hidden" name="delete" value="Remove"/>
< /form>
These are the buttons that are created in "fileOne.php" but in the form action= "fileTwo.php" : ("button1 = edit, button2 = delete)
echo "<td><input type='submit' name='Edit' value='Edit'>
<input type='submit' name='Delete' value='Remove'></td>\n";
This is where I try to check in "fileTwo.php" if either button was pushed:
//if edit is clicked
if(isset($_POST['edit'] == 'Edit'))
{
echo"hello";
}
//if remove is clicked
if(isset($_POST['delete'] == 'Remove'))
{
echo"good bye";
}
I would appreciate the solution in PHP, not JavaScript, AJAX, etc.
Rewrite it like this:
fileOne.php
<form action="fileTwo.php" method="post">
<input type="submit" name="edit" value="edit" />
<input type="submit" name="delete" value="delete" />
</form>
fileTwo.php
<?php
if (isset($_POST['edit']) && ($_POST['edit'] == 'edit')) {
echo "Hello";
}
else if (isset($_POST['delete']) && ($_POST['delete'] == 'delete')) {
echo "Goodbye";
}
?>
Ok, this way you don’t need any extra code, no hidden inputs and only 2 buttons. Both will submit your form with the values needed for the handler on the next page to deal with them correctly.
make two forms:
< form action="execute.php" method = "POST">
< input type="hidden" name="delete" value="Remove"/>
< input type="submit" value="Delete"/>
< /form>
< form action="execute.php" method = "POST">
< input type="hidden" name="edit" value="Edit"/>
< input type="submit" value="Edit"/>
< /form>
in fileTwo.php:
//input names(edit, delete) and values(Edit, Remove) are case sensetive
if( isset($_POST['edit']) && $_POST['edit'] == 'Edit') {
echo"hello";
}
//if remove is clicked
if( isset($_POST['delete']) && $_POST['delete'] == 'Remove') {
echo"good bye";
}
So I made a registration form such that the user must select one of the three radio options before continuing. However if the user didn't choose an option and clicked submit I want to display at least a message that says "please choose an option" or something. Here is my code so far:
<?php
$reg_type = "";
function setclick()
{
if(isset($_POST["Submit"]))
$clicked=1;
else
$clicked=0;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["Reg_type"])) {
$clicked=1;
header('Location: Registration_1.php');
}
else {
$reg_type = $_POST["Reg_type"];
header('Location: Registration_2.php');
}
}
echo $clicked;
?>
<form name="frmtype" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" >
<input type="radio" name="Reg_type" value="1"/> Registering myself with credit card or bank account <br/>
<input type="radio" name="Reg_type" value="2"/> Registering multiple people using credit card or bank account <br/>
<input type="radio" name="Reg_type" value="3"/> Registering multiple people using a purchase order <br/>
<input type="submit" name="Submit" value="Submit" />
<?php
setclick();
if($clicked)
echo "Please select an option";
?>
I'm having a real hard time getting the logic down to display the error message if they didn't choose an option and clicked submit.
Your setclick() function will always set $clicked to true if the form has been posted. Since you then call setclick() after your main php block, it will be true whether or not the rest of the logic has changed it. Try this:
if(isset($_POST['Reg_type']) && $_POST['Reg_type'] != ''){
$reg_type = $_POST['Reg_type'];
header('Location: Registration_2.php');
} else $clicked = true;
and then down the bottom of the page under the form:
if($clicked) echo "Please select an option";
<?php
$reg_type = $_POST['Reg_type'];
if($reg_type == "1"){
//action 1
} else if($reg_type == "2"){
//action 2
} else if($reg_type == "3") {
//action 3
} else {
// no radio button is checked. display the message
$message = "Please select an option";
}
?>
Try this
<script>
function checkform(){
var checked = false;
var buttons = document.getElementsByTagName('input')
for (var i = 0; i < buttons.length ; i++) {
if (buttons[i].checked) {
checked = true;
break;
}
}
return checked;
}
</script>
<form onsubmit="return checkform()">
<input type="radio" name="Reg_type" value="1"/> Registering myself with credit card or bank account <br/>
<input type="radio" name="Reg_type" value="2"/> Registering multiple people using credit card or bank account <br/>
<input type="radio" name="Reg_type" value="3"/> Registering multiple people using a purchase order <br/>
<input type="submit" name="Submit" value="Submit" />
Check the fiddle
This should get you started. Please be aware that this script is selecting elements by tag name, which means it will pick up any input in the form. If you are OK with using jQuery I'd highly recommend it, it will make this kind of tasks trivial.
I have a form with 2 buttons, that depending on which is selected will either be deleted or edited from the database. Those are each individual pages using SQL statements (questionedit and questiondelete). However, when i press a button, nothing happens...Any Ideas
Here is my javascript
function SelectedButton(button)
{
if(button == 'edit')
{
document.testedit_questionform.action ="testedit_questionedit.php";
}else if(button == 'delete'){
document.testedit_questionform.action ="testedit_questiondelete.php";
}
document.forms[].testedit_questionform.submit();
}
Here is my form (being echoed from a loop)
<form name="testedit_questionform" action="SelectedButton" method="POST">
<span class="grid_11 prefix_1" id="" >
Question:<input type="text" name="QuestionText" style="width:588px; margin-left:10px;" value="$row[0]"/>
<input type="button" value="Edit" name="Operation" onclick="submitForm('edit')" />
<input type="button" value="Delete" name="Operation" onclick="submitForm('delete')" />
<input type="hidden" name="QId" value="$row[3]" /><br />
</form>
First of all, your function should be named submitForm
function submitForm(button) {
if(button == 'edit') {
document.testedit_questionform.action ="testedit_questionedit.php";
} else if(button == 'delete') {
document.testedit_questionform.action ="testedit_questiondelete.php";
}
document.testedit_questionform.submit();
}
And then, call submit method from your form.
EDIT:
An alternative to calling forms is: document.forms['FORM_NAME'].submit()
Look at how you access the form to call submit(). Now look at how you access the form to change the action. One of them is clearly wrong.
It would be easier to make one PHP file, and buttons with different names like this:
<form method="post" action="actions.php">
<input type="submit" name="action1" value="Action 1" />
<input type="submit" name="action2" value="Action 2" />
[...]
And the file actions.php:
if(isset($_POST["action1"])) {
// action 1
}
elseif(isset($_POST["action2"])) {
// action 2
}