Jump to content

[SOLVED] 2 actions on a form


jonathanbee

Recommended Posts

Hi there,

 

I've been trying to figure out how I can have 2 actions on my address form.  One button's action takes the postal code and queries the database for the address. The other's action proceeds to the next page. I can get either one or the other to work.  But how can I get them both to work on the same page without losing the data that the user enters into the form? Thanks for any advice.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/73594-solved-2-actions-on-a-form/
Share on other sites

sorry, I'm sure this code is not that good, I'm still learning :-\ :

 

<?php

 

// check which button was clicked

if ($_GET['lookup'])

{

 

$new_code = $_GET['code'];

$new_telephone = $_GET['telephone'];

 

$c_new_code = mysql_real_escape_string($new_code);

$d_new_code = ereg_replace ('[^0-9]+', '',$c_new_code);

 

$query="select * from postal_codes where code='$d_new_code'";

$result=mysql_query($query);

 

while($row = mysql_fetch_array($result)){

$addy1 = $row['add1'];

$addy2 = $row['add2'];

$addy3 = $row['add3'];

$faddy = "{$addy1}{$addy2}{$addy3}"; }

 

}

 

else { }

 

?>

 

<form action="<?php echo $_SERVER['PHP_SELF'] ?>"  method="GET">

<fieldset>

 

<label>郵便場号:</label>

<input type="text" name="code" value="<?php echo $new_code?>"></input>

 

<label>都道府県市町村郡:</label>

<input type="text" name="code2" value="<?php echo $faddy?>"></input>

 

 

<label>電話</label>

<input type="text" name="telephone" value="<?php echo $new_telephone?>"></input>

 

</fieldset>

 

<input type="submit" value="Find>" name="lookup"></input>

<input type="submit" value="Go>" name="redirect"></input>

</form>

Well there are a few ways you can determine which form has been used.

 

Option 1: Put a hidden field in each form

<input type="hidden" name="formname" value="formone" />

 

and

<input type="hidden" name="formname" value="formtwo" />

 

Then on the post back you could say:

if( $_POST['formname'] == "formone" )
{
   //DO STUFF FOR FORM ONE HERE
}
else if( $_POST['formname'] == "formtwo" )
{
   //DO STUFF FOR FORM TWO HERE
}

 

Option 2: Alternatively you can test different buttons to determine which form needs to be processed.

if( isset($_POST['formonebutton']) )
{
   //DO STUFF FOR FORM ONE HERE
}
else if( isset($_POST['formtwobutton']) )
{
   //DO STUFF FOR FORM TWO HERE
}

Thanks for your reply, so... I *must* use 2 forms to accomplish this?

 

The 1st form's action would run the function that queries the db for the address based on the postal code,

 

the 2nd form's action would advance to the next page.

 

But, then is there some way for me to get data from both forms if the user clicks the 2nd button?

If I declare this at the start:

 

$ney = $_SERVER['PHP_SELF'];

 

And change the form action to:

 

<form action="<?php echo $ney?>"  method="GET">

 

then change the else to:

 

else if ($_GET['redirect']) {

$ney = "invoice.php";

 

it almost works.. am I on the right track or is this totally wrong?

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.