Hi everyone, I'm hoping someone here can help me. Basically, what I'm trying to do is create a form which is populated by data from a dropdown list. It sounds pointless, so let me explain. The dropdown list is filled with default addresses, and I would like that data to be passed across the form fields below it. Line 1, Line 2, City, County, Postcode... In the dropdown list, the data is hardcoded and concatenated to allow for separating into the specific fields below. Complete code below - ANY help is appreciated as I'm nearly bald from pulling my hair out over it! Thanks. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title></title> <link href="main.css" rel="stylesheet" type="text/css" /> </head> <body> <?php session_register('stdaddress'); session_register('da1'); session_register('da2'); session_register('da3'); session_register('da4'); session_register('da5'); session_register('defaultaddress1'); $_SESSION['stdaddress'] = $_POST['stdaddress']; ?> <form id="default01" name="default01" method="post" action=""> <input type="hidden" value="Line 1" id="al1" /> </form> <?php $_SESSION['da1'] = $_POST['Line 1']; $_SESSION['da2'] = $_POST['Line 2']; $_SESSION['da3'] = $_POST['City']; $_SESSION['da4'] = $_POST['County']; $_SESSION['da5'] = $_POST['Postcode']; $da1 = "Line 1"; $da2 = "Line 2"; $da3 = "City"; $da4 = "County"; $da5 = "Postcode"; $none = "Please Specify"; $defaultaddress1 = $da1 . ", " . $da2 . ", " . $da3 . ", " . $da4 . ", " . $da5; ?> <input type="hidden" id="defaddress1" value="<?php $_REQUEST['defaultaddress1']; ?>" /> <form id="stdaddress" name="stdaddress" method="post" action="stepthree.php"> Use Existing Address: <select name='address' id="address" onchange="document.location.href = 'stepthree.php?var=' + this.value"> <option value='$none'>Select Existing Address</option> <option value='$defaddress1'><?php echo $defaultaddress1; ?></option> </select> <script type="text/javascript"> document.getElementById('address').value = "<?php echo $_GET['var'];?>"; </script> </form> <br /> <br /> <form id="form" name="form" method="post" action="stepfour.php"> <?php $address = $_POST['address']; ?> <ul> <li>Line 1:<input type="text" class="inputfield" name="line1" id="line1" value="<?php if ($_POST['address'] == $defaddress1) { echo $da1; } else { echo "Please Specify"; } ?>" /></li> <li>Line 2:<input type="text" class="inputfield" name="line2" value="<?php if ($_POST['address'] == $defaddress1) echo "$da2"; else echo 'Please Specify'; ?>" id="line2"/></li> <li>Town / City:<input type="text" class="inputfield" name="city" id="city" value="<?php echo $_POST['address']; ?>" /></li> <li>County:<input type="text" class="inputfield" name="county" id="county" value="<?php echo $_POST['address']; ?>" /></li> <li>Postcode:<input type="text" class="inputfield" name="postcode" id="postcode" value="<?php echo $_POST['address']; ?>" /></li> </ul> <input type="submit" name="continue" id="continue" value="Continue" /> </form> </body> </html> Code (markup):
The immediate problem that I see is that your new href has a get variable called "var" but your form looks for a post variable called "stdaddress" I'd create a javascript array of the possible addresses and when one is selected (the value of the dropdown would be an id or array key) I'd use normal javascript to pick up the new values from the array and populate the fields. If the list of possible addresses gets too long or there are security issues then you'd use jquery or prototype to make an ajax call down to the server to pick up the values.
I appreciate you taking the time to look at the code, I really do. I think the variable "var" is dead (as in I don't do anything else with it, and it's just left over from previous attempts). I'm a little confused as to how to do the rest of what you suggest. At the moment, the only address I need is a template "Line 1, Line 2, City, County, Postcode". Any chance you could provide a step-by-step explanation? I know I'm asking alot, it's just that I've been looking at the same page of code for about 12 hours, and I'm getting more and more confused! *begs*
If anyone can help on this, I'd be really grateful. I really need to know what to do to make this work as it should - I've been scouring the web for explanations of Javascript and the structure of this type of form, but I'm still not completely sure. Is it missing anything obvious - like a particular command? Are things in the wrong order? Have I referenced the wrong things? Any help, as I say, is hugely appreciated and I'll love you forever!
NAILED IT! It's always really satisfying when you do it yourself - though I do have to thank SarahK for bring the arrays to my attention - they appear to have been the major part of the problem - after that I just had to use the right method to call them in. Now it's working great!