$_post[] issue

Hi all,

just when i think i am finally getting to grips with coding I get stumped by another probably simple issue. This time I have a form that has two action buttons ‘Update’ and ‘Quit’. My problem is that the ‘Update’ button does not ever seem to recognise it has been clicked even though the ‘Quit’ one works fine!

The controller code:


	if (isset($_POST['action']) and $_POST['action'] == 'Quit')
	
		{
		include 'form.html.php';
		exit();
		}
			
	if (isset($_POST['action']) and $_POST['action'] == 'Update')

	{
		$test = 'yeah baby';
             }



HTML Form code for the buttons:


        <th colspan="2"><input name="action" type="submit" value="Update"/></th>
        <th><input name="action" type="submit" value="Quit"/></th>

if you need more information please ask, i’ll happily provide if someone can assist with an answer!

ty
Dibley

Of course!.. i knew is was going to be somethign so simple, i guess i was staring for too long in the wrong places!!

ty

It sounds like the values for both buttons are being passed when the form is submitted. You could confirm this by reversing the if statements and seeing that update now works but quit is broken.

Try giving both buttons the same name - that way only the value on the selected one should get passed when the form is submitted.

Add a print_r of $_POST at the top of the script, so you can check what exactly is being passed.

take exit() out, see what happens.

bingo! You are overwriting sumbit with quit so _POST only sees one button basically

Well i thought i had it sussed from your kind advice Ryan, i just cant seem to put it in place.

I opted for - controller :


	if (isset($_POST['quitaction']) and $_POST['quitaction'] == 'Quit')
	
		{
		include 'form.html.php';
		exit();
		}
			
	if (isset($_POST['updateaction']) and $_POST['updateaction'] == 'Update')


with corresponding name change in the HTML


        <th colspan="2"><input name="updateaction" type="submit" value="Update"/></th>
        <th><input name="quitaction" type="submit" value="Quit"/></th>

still no joy, I know it is goign to be somethign obvious - i think i may have been staring at it for far to long now! lol

I am trying to have two ‘Buttons’ on a form. One named ‘Quit’ and one named ‘Update’. Put simply when someone presses ‘Quit’ i want them to exit out of that page and return to the previous page without update the Database. However, if they press the other button, i want to update the data base with the new input.

The issue I seem to have is purely that the if statement to detect if the button pressed is update just isnt functioning (i’ve set variables within it to test with corresponding echo’s with now output, so guessing that means the variable isnt being set, therefore thinking the if statment is not being met)

I’m hoping new mornign eyes will assist, but any help out there greatly welcomed!

PS - The If statment for the 'Quit ’ button works as intended

Looks like you have both if statements for both button inside the edit if statement. It’s looking for the action for edit first before any of the other two.

Also within the ‘Update’ area you should have this:


echo 'Yeah baby';

Instead of:


$test = 'yeah baby';

OK - I am back to have another crack at this, ty for the posts.

I have implmented a print_r within the script and it is echoing the value ‘1’ constantly.

I am beginning to think i have something fundamentally wrong somewhere else, as i have set a variable at different places within the controller to see what is running (by the value it is returning). and nothing is coming back as i expected.

I have taken the liberty of pasting my complete code (well i have stripped out lots of none essentials)


<?php
include $_SERVER['DOCUMENT_ROOT']. '/landp/site/includes/db.inc.php';
include $_SERVER['DOCUMENT_ROOT']. '/landp/site/includes/magicquotes.inc.php';

$test='start';

/*//check logged in
require_once $_SERVER['DOCUMENT_ROOT']. '/landp/site/includes/access.inc.php';

	if (!userisloggedin())
	{
		include '../login.html.php';
		exit();
	}*/

// add property comparable details - call form
if (isset($_GET['addproperty']))
{

include 'form.html.php';

exit();
}

//add property details - add to DB
if (isset ($_POST['action']) and $_POST['action'] == 'Add')

{
	//get and declare variables from form
	
	// declare SQl variable

	
	if (!mysqli_query ($link, $sql))
	{
		$error = 'Error adding property details: ' .mysqli_error($link). " ";
		include 'error.html.php';
		exit();
	}
	header ('Location: .');
	exit();
}

//delete property details
if (isset($_POST["action"]) and $_POST['action'] == 'Delete')
{

	$id = mysqli_real_escape_string($link, $_POST['property_id']);
	$sql = "delete from property where id = '$id'";
	
	if (!mysqli_query($link, $sql))
	{
	$error = 'Error deleting property: ' . mysqli_error($link);
	include 'error.html.php';
	exit();
	}
	header('Location: .');
	exit();
}

//Edit Property information

if (isset($_POST['action']) and $_POST['action'] == 'Edit')
{
	$id = mysqli_real_escape_string($link, $_POST['property_id']);
	$result = mysqli_query($link, 'select * from property where id ="' .$id. '" ');
	$test = 'standard';
	
	if (!$result)
	{
		$error = 'Error fetching property list: ' . mysqli_error($link);
		include 'error.html.php';
		exit();
	}	
	
	while ($row = mysqli_fetch_array($result))
	{
		$updates[] = array(
		 'House_name' => $row['House_name'],
		 'house_number' => $row['house_number'],
		 'addline1' => $row['addline1'],
		 'addline2' => $row['addline2'],
		 'town' => $row['town'],
		 'county' => $row['county'],
		 'postcode' => $row['postcode'],
		 'bed' => $row['bed'],
		 'living' => $row['living'],
		 'kitchen' => $row['kitchen'],
		 'bath' => $row['bath'],
		 'parking' => $row['parking'],
		 'heating' => $row['heating'],
		 'size' => $row['size'],
		 'price' => $row['price'],
		 'sold_status' => $row['sold_status'],
		 'sold_date' => $row['sold_date'],
		 'cond' => $row['cond'],
		 'prop_note' => $row['prop_note']);
	}

	include './updateform.html.php';
	$test=='something';
	if (isset($_POST['action']) and $_POST['action'] == 'Quit')

	{
	$test = 'quit';
	include $_SERVER['DOCUMENT_ROOT']. '/landp/site/Property_comp/form.html.php';
	exit();
	}
			
	if (isset($_POST['action']) and $_POST['action'] == 'Update')

	{
		$test = 'yeah baby';
		//check to see if new information input
		

		
		// write old and new info into table property
		
		$sql = 
		
		if (!mysqli_query ($link, $sql))
		{
			$error = 'Error adding property details: ' .mysqli_error($link). " ";
			include 'error.html.php';
			exit();
		}
		header ('Location: .');
		exit();

				
		}

	$test = 'straight through';
	exit();
	
}

//get data from property table to list

$result = mysqli_query($link, 'SELECT * FROM property');
if (!$result)
{
	$error = 'Error fetching property list: ' . mysqli_error($link);
	include 'error.html.php';
	exit();
}

while ($row = mysqli_fetch_array($result))
{
	$props[] = // set array
}

include 'list.html.php';
?>

The strange part is that I have the buttons working as intended within others parts of the code (above shows a ‘edit’ and ‘delete’ buttons which do work!!).

Hence i am beginngin to think its isnt the If POST values that are incorrect?!?

many thanks

Dibley

Maybe you already did this, but let me quote myself anyway :slight_smile:

By making names the same, will make your if statement only run the first one. I think the ‘and $_POST[‘quitaction’] == ‘Quit’’ part is messing things up.


<form method="post" action="update.php">
<!-- whatever the form entails -->
<th colspan="2">
 <input name="updateaction" id="updateaction" type="submit" value="Update"/>
</th>
<th>
 <input name="quitaction" id="quitaction" type="submit" value="Quit"/>
</th>
</form>

(you shouldn’t be using TH here btw)

update.php


if(isset($_POST['quitaction'])){
  header( 'Location: url' ) ;
}
if(isset($_POST['updateaction'])){
  //connect to your db
  // run update query
  // output successful or something
}

Inputs can be named the same thing because the value is only sent that was clicked. In most cases only one submit can be clicked at a time so its fine to have two submits with the same name but different value. However, I would say its best practice to use separate names so that delegation of the action isn’t based on the submits label. So if the label for submit ever needs to change you just need to change the label and not touch the programming logic used to delegate the action.

Multiple Submits with same name


<?php
if(isset($_POST['submit'])) {
	echo "<p>You Clicked {$_POST['submit']}</p>";
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
	<input type="submit" name="submit" value="One">
	<input type="submit" name="submit" value="Two">
	<input type="submit" name="submit" value="Three">
	<input type="submit" name="submit" value="Four">
	<input type="submit" name="submit" value="Five">
</form>

There isn’t anything wrong with this code. Its probably something to do with the include path not being correct. I assume you don’t think its working because the form include isn’t occurring when Quit is clicked?


<?php

	if (isset($_POST['action']) && $_POST['action'] == 'Quit') {
		echo 'Quit clicked';
	}
			
	if (isset($_POST['action']) && $_POST['action'] == 'Update') {
		echo 'Update clicked';
  	}

?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <input name="action" type="submit" value="Update"/>
    <input name="action" type="submit" value="Quit"/>
</form>

what should happen

I’d already // the exit out, it should only be called when ‘Quit’ pressed anyways, and that does work.

I’m gonna try fresh eyes tomorrow i think!

I am going to give up staring at this code for a day or two.

I did start off with them being the same name (please see OP).

The crazy part of it is that I have got it working, albeit with different actions, in a different part of the website.

But can I see the difference between them!!! (nope grrrrrrrr)

ty for the help.

Each button needs it’s own name (and id (most people use the same name & id for the button in question))

So currently they are both called ‘action’ hence why it is failing?