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
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>

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.