jonathanbee Posted October 17, 2007 Share Posted October 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
dbo Posted October 17, 2007 Share Posted October 17, 2007 Can you post some of your relevant code? Quote Link to comment Share on other sites More sharing options...
jonathanbee Posted October 17, 2007 Author Share Posted October 17, 2007 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> Quote Link to comment Share on other sites More sharing options...
dbo Posted October 17, 2007 Share Posted October 17, 2007 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 } Quote Link to comment Share on other sites More sharing options...
jonathanbee Posted October 17, 2007 Author Share Posted October 17, 2007 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? Quote Link to comment Share on other sites More sharing options...
jonathanbee Posted October 17, 2007 Author Share Posted October 17, 2007 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? Quote Link to comment Share on other sites More sharing options...
jonathanbee Posted October 18, 2007 Author Share Posted October 18, 2007 I figured out how to use AJAX to query the db while the user is filling out the form Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.