webmaster1 Posted November 22, 2008 Share Posted November 22, 2008 Hi All, I have three forms on the one page. Form 1 writes the user input to a mySQL database. Form 2 converts Miles to Kilometres (does not need to write to database). Form 3 converts Kilometres to Miles (does not need to write to database). Problem: When I click the submit button of either it clears the user input of the rest. The user should be able to calculate the conversion via Form 2/Form 3 half way through Form 1 without loosing their existing input. Here's my code: <form> <!--FORM ONE--> </form> <!--FORM TWO--> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input type="text" name="mileage_m" id="mileage_m"> <input type="submit" name="submit_m" id="submit_m"> <?php if (isset($_POST['submit_m'])) //1 Mile : 1.609 Kilometre $mileage_m = $_POST['mileage_m']; $mileage_m_result = 1.609*($mileage_m); echo "$mileage_m_result"; echo "Kilometres"; ?> </form> <!--FORM THREE--> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input type="text" name="mileage_km" id="mileage_km"> <input type="submit" name="submit_km" id="submit_km"> <?php if (isset($_POST['submit_km'])) //1.609 Kilometre : 1 Mile $mileage_km = $_POST['mileage_km']; $mileage_km_result =($mileage_km)/1.609; echo "$mileage_km_result"; echo "Miles"; ?> </form> Any ideas on how to prevent this? Quote Link to comment https://forums.phpfreaks.com/topic/133831-solved-three-forms-on-one-page-clearing-each-others-input/ Share on other sites More sharing options...
genericnumber1 Posted November 22, 2008 Share Posted November 22, 2008 The only way I can think of doing it (other than ajax) is to combine all of the forms into one form, and then decide which form to process based upon which submit button was pushed. Looking at how you have it set up currently, it's likely it would be trivial to implement. Once you combine them all into one form you'll be able to echo the post variables into the value setting of each input tag (after sanitizing them of course). Quote Link to comment https://forums.phpfreaks.com/topic/133831-solved-three-forms-on-one-page-clearing-each-others-input/#findComment-696523 Share on other sites More sharing options...
wildteen88 Posted November 22, 2008 Share Posted November 22, 2008 Yes just need one form with only three inputs, The first of which will be the distance, the second will be list of conventions the last one will be your submit button. <?php $dist = null; if(isset($_POST['submit'])) { $dist = $_POST['distance']; switch($_POST['action']) { // Miles to Kilometers case 0: $unit = ' Kilometers'; $result = $dist*1.609; break; // Kilometers to Miles case 1: $unit = ' Miles'; $result = $dist/1.609; break; } // display the result. echo number_format($result, 2, '.', ',') . $unit; } ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input type="text" name="distance" value="<?php echo $dist; ?>" /> <select name="action"> <?php $actions = array('Miles to Kilometers', 'Kilometers to Miles'); foreach($actions as $key => $action) { $selected = (isset($_POST['action']) && $_POST['action'] == $key) ? ' selected="selected"' : null; echo ' <option value="'.$key.'"'.$selected.'>'.$action."</option>\n"; } ?> </select> <input type="submit" name="submit" value="Convert"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/133831-solved-three-forms-on-one-page-clearing-each-others-input/#findComment-696537 Share on other sites More sharing options...
webmaster1 Posted November 22, 2008 Author Share Posted November 22, 2008 combine all of the forms into one form Do you mean I should inserts forms within a form? I think I'll run into the same problem with the input being cleared. If you meant I should insert the code and the fields I'm not sure how to implement an onclick event (on a regular button rather than a submit button) that will trigger my php calculation. Should I just consider using javascript to make this a standalone calculation? Quote Link to comment https://forums.phpfreaks.com/topic/133831-solved-three-forms-on-one-page-clearing-each-others-input/#findComment-696538 Share on other sites More sharing options...
wildteen88 Posted November 22, 2008 Share Posted November 22, 2008 combine all of the forms into one form Do you mean I should inserts forms within a form? I think I'll run into the same problem with the input being cleared. If you meant I should insert the code and the fields I'm not sure how to implement an onclick event (on a regular button rather than a submit button) that will trigger my php calculation. Should I just consider using javascript to make this a standalone calculation? Did you miss my post earlier? Quote Link to comment https://forums.phpfreaks.com/topic/133831-solved-three-forms-on-one-page-clearing-each-others-input/#findComment-696539 Share on other sites More sharing options...
webmaster1 Posted November 22, 2008 Author Share Posted November 22, 2008 Did you miss my post earlier? Yes sir! Working on it now. Quote Link to comment https://forums.phpfreaks.com/topic/133831-solved-three-forms-on-one-page-clearing-each-others-input/#findComment-696546 Share on other sites More sharing options...
genericnumber1 Posted November 22, 2008 Share Posted November 22, 2008 Wildteen, when you all come in here and give full answers and solutions you make me just look lazy for only giving them an explanation of the solution! I guess that means I SHOULD stop being lazy :sigh:. Quote Link to comment https://forums.phpfreaks.com/topic/133831-solved-three-forms-on-one-page-clearing-each-others-input/#findComment-696551 Share on other sites More sharing options...
webmaster1 Posted November 22, 2008 Author Share Posted November 22, 2008 Nuts. There is a problem: It doesn't tie in with my main form. Here's what my full code looks like when I added your suggested code: <?php //WT88 CODE $dist = null; if (isset($_POST['submit'])) //PROCESS THE FORM WHEN THE SUBMIT BUTTON IS PRESSED { //WT88 CODE $dist = $_POST['distance']; switch($_POST['action']) { // Miles to Kilometers case 0: $unit = ' Kilometers'; $result = $dist*1.609; break; // Kilometers to Miles case 1: $unit = ' Miles'; $result = $dist/1.609; break; //VALIDATE THE INDIVIDUAL FIELDS AND DEFINE THEM AS TRUE OR FALSE BASED ON THE USER'S INPUT //REGULAR TEXT FIELDS if (strlen($_POST['make']) > 0) {$make=TRUE;} else {$make=FALSE; $message_make=" *You forgot to enter the make!";} if (strlen($_POST['model']) > 0) {$model=TRUE;} else {$model=FALSE; $message_model=" *You forgot to enter the model!"; } if (strlen($_POST['price']) > 0) {$price=TRUE; if (is_numeric($_POST['price'])) {$pricenumericcheck=TRUE;} else {$pricenumericcheck=FALSE; $message_pricenumericcheck=" *Please enter numeric values only! (e.g. Do not input commas or the euro symbol!)"; //echo "$message_pricenumericcheck"; } } else {$price=FALSE; $message_price=" *You forgot to enter the price!";} if (strlen($_POST['engine']) > 0) {$engine=TRUE;} else {$engine=FALSE; $message_engine=" *You forgot to enter the engine!";} if (strlen($_POST['body']) > 0) {$body=TRUE;} else {$body=FALSE; $message_body=" *You forgot to enter the body!";} if (strlen($_POST['transmission']) > 0) {$transmission=TRUE;} else {$transmission=FALSE; $message_transmission=" *You forgot to enter the transmission!";} if (strlen($_POST['year']) > 0) {$year=TRUE; if (is_numeric($_POST['year'])) {$yearnumericcheck=TRUE; if((strlen($_POST['year']) > 4) || (strlen($_POST['year']) < 4)) { $yearnumericcheck=FALSE; $message_yearnumericcheck=" *Please enter a four digit numeric value only! (e.g. 1983, 1999, 2010.)"; //echo "$message_yearnumericcheck"; } } else {$yearnumericcheck=FALSE; $message_yearnumericcheck=" *Please enter a four digit numeric value only! (e.g. 1983, 1999, 2010.)"; //echo "$message_yearnumericcheck"; } } else {$year=FALSE; $message_year=" *You forgot to enter the year!";} if (strlen($_POST['colour']) > 0) {$colour=TRUE;} else {$colour=FALSE; $message_colour=" *You forgot to enter the colour!";} if (strlen($_POST['mileagem']) > 0) {$mileagem=TRUE; if (is_numeric($_POST['mileagem'])) {$mileagemnumericcheck=TRUE;} else {$mileagemnumericcheck=FALSE; $message_mileagemnumericcheck=" *Please enter numeric values only! (e.g. Do not input commas or the mile symbol!)"; //echo "$message_mileagemnumericcheck"; } } else {$mileagem=FALSE; $message_mileagem=" *You forgot to enter the mileage in miles!";} if (strlen($_POST['mileagekm']) > 0) {$mileagekm=TRUE; if (is_numeric($_POST['mileagekm'])) {$mileagekmnumericcheck=TRUE;} else {$mileagekmnumericcheck=FALSE; $message_mileagekmnumericcheck=" *Please enter numeric values only! (e.g. Do not input commas or the kilometre symbol!)"; //echo "$message_mileagekmnumericcheck"; } } else {$mileagekm=FALSE; $message_mileagekm=" *You forgot to enter the mileage in kilometres!";} if (strlen($_POST['owners']) > 0) {$owners=TRUE; if (is_numeric($_POST['owners'])) {$ownersnumericcheck=TRUE;} else {$ownersnumericcheck=FALSE; $message_ownersnumericcheck=" *Please enter numeric values onlyfor owners! (e.g. 1, 2, 3.!)"; //echo "$message_ownersnumericcheck"; } } else {$owners=FALSE; $message_owners=" *You forgot to enter the owners in miles!";} if (strlen($_POST['doors']) > 0) {$doors=TRUE; if (is_numeric($_POST['doors'])) {$doorsnumericcheck=TRUE;} else {$doorsnumericcheck=FALSE; $message_doorsnumericcheck=" *Please enter numeric values only for doors! (e.g. 2, 4, 6.!)"; //echo "$message_doorsnumericcheck"; } } else {$doors=FALSE; $message_doors=" *You forgot to enter the owners in doors!";} if (strlen($_POST['location']) > 0) {$location=TRUE;} else {$location=FALSE; $message_location=" *You forgot to enter the location!";} //TEXT AREA FIELD if (isset($_POST['info'])) { $info = trim($_POST['info']); if ($info == "" || empty($info)) { $message_info = "Please enter data for info!"; } } //UPLOAD FILE FIELDS //CHECK IF UPLOAD FILE FIELD 1 IS EMPTY $filesize1=$HTTP_POST_FILES['ufile']['size'][0]; if($filesize1==0) { $filesize1=FALSE; $message_filesize1="You forgot to enter this image file!"; //echo "$message_filesize1"; } else { $filesize1=TRUE; } //CHECK IF UPLOAD FILE FIELD 1 IS EMPTY $filesize2=$HTTP_POST_FILES['ufile']['size'][1]; if($filesize2==0) { $filesize2=FALSE; $message_filesize2="You forgot to enter this image file!"; //echo "$message_filesize2"; } else { $filesize2=TRUE; } //CHECK IF UPLOAD FILE FIELD 3 IS EMPTY $filesize3=$HTTP_POST_FILES['ufile']['size'][2]; if($filesize3==0) { $filesize3=FALSE; $message_filesize3="You forgot to enter this image file!"; //echo "$message_filesize3"; } else { $filesize3=TRUE; } //CHECK IF ANY THE FILES ARE THE SAME: $filenamecheck1= $HTTP_POST_FILES['ufile']['name'][0]; $filenamecheck2= $HTTP_POST_FILES['ufile']['name'][1]; $filenamecheck3= $HTTP_POST_FILES['ufile']['name'][2]; $filesize1=$HTTP_POST_FILES['ufile']['size'][0]; $filesize2=$HTTP_POST_FILES['ufile']['size'][1]; $filesize3=$HTTP_POST_FILES['ufile']['size'][2]; if ( (($filenamecheck1 == $filenamecheck2) || ($filenamecheck2 == $filenamecheck3) || ($filenamecheck1 == $filenamecheck3)) && ((!$filesize1==0 && isset($_POST['submit'])) && (!$filesize2==0 && isset($_POST['submit'])) && (!$filesize3==0 && isset($_POST['submit']))) ) { $filenamecheck_message = "one ore more of the files are the same"; //echo "$message_filenamecheck"; $message_filenamecheck = TRUE; $filenamecheck1= FALSE; $filenamecheck2= FALSE; $filenamecheck3= FALSE; } else { $message_filenamecheck = FALSE; $filenamecheck1= TRUE; $filenamecheck2= TRUE; $filenamecheck3= TRUE; } //CHECK IF FILE 1 IS NOT A *.JPEG OR *.GIF $filesize1=$HTTP_POST_FILES['ufile']['size'][0]; if ( ( ($HTTP_POST_FILES['ufile']["type"][0] == "image/gif") || ($HTTP_POST_FILES['ufile']["type"][0] == "image/jpeg") || ($HTTP_POST_FILES['ufile']["type"][0] == "image/pjpeg") ) // &&($HTTP_POST_FILES['ufile']["size"][0] < 50000) ) { $filetypecheck1=TRUE; } else { if (!($filesize1 == 0)) { $filetypecheck1=FALSE; $message_filetypecheck1="The first of the sumbitted files is not of a *.JPG ! *.GIF file type!"; //echo "$message_filetypecheck1"; } } //CHECK IF FILE 2 IS NOT A *.JPEG OR *.GIF $filesize2=$HTTP_POST_FILES['ufile']['size'][1]; if ( ( ($HTTP_POST_FILES['ufile']["type"][1] == "image/gif") || ($HTTP_POST_FILES['ufile']["type"][1] == "image/jpeg") || ($HTTP_POST_FILES['ufile']["type"][1] == "image/pjpeg") ) // &&($HTTP_POST_FILES['ufile']["size"][1] < 50000) ) { $filetypecheck2=TRUE; } else { if (!($filesize2 == 0)) { $filetypecheck2=FALSE; $message_filetypecheck2="The second of the sumbitted files is not of a *.JPG ! *.GIF file type!"; //echo "$message_filetypecheck2"; } } //CHECK IF FILE 3 IS NOT *.JPEG OR *.GIF $filesize3=$HTTP_POST_FILES['ufile']['size'][2]; if ( ( ($HTTP_POST_FILES['ufile']["type"][2] == "image/gif") || ($HTTP_POST_FILES['ufile']["type"][2] == "image/jpeg") || ($HTTP_POST_FILES['ufile']["type"][2] == "image/pjpeg") ) // &&($HTTP_POST_FILES['ufile']["size"][2] < 50000) ) { $filetypecheck3=TRUE; } else { if (!($filesize3 == 0)) { $filetypecheck3=FALSE; $message_filetypecheck3="The third of the sumbitted files is not of a *.JPG ! *.GIF file type!"; //echo "$message_filetypecheck3"; } } //CHECK IF FILE 1 EXISTS ALREADY $path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0]; $filesize1=$HTTP_POST_FILES['ufile']['size'][0]; if ( (file_exists($path1)) && (!($filesize1 == 0)) ) { $fileexistcheck1=FALSE; $message_fileexistcheck1="The first file already exist on the server!"; //echo "$message_fileexistcheck1"; } else { $fileexistcheck1=TRUE; } //CHECK IF FILE 2 EXISTS ALREADY $path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1]; $filesize2=$HTTP_POST_FILES['ufile']['size'][1]; if ( (file_exists($path2)) && (!($filesize2 == 0)) ) { $fileexistcheck2=FALSE; $message_fileexistcheck2="The second file already exist on the server!"; //echo "$message_fileexistcheck2"; } else { $fileexistcheck2=TRUE; } //CHECK IF FILE 3 EXISTS ALREADY $path3= "upload/".$HTTP_POST_FILES['ufile']['name'][2]; $filesize3=$HTTP_POST_FILES['ufile']['size'][2]; if ( (file_exists($path3)) && (!($filesize3 == 0)) ) { $fileexistcheck3=FALSE; $message_fileexistcheck3="The third file already exist on the server!"; //echo "$message_fileexistcheck3"; } else { $fileexistcheck3=TRUE; } //IF ALL INPUT FIELDS ARE DEFINED AS TRUE / VALIDATED if ($make && $model && $price && $pricenumericcheck && $engine && $body && $transmission && $year && $yearnumericcheck && $colour && $mileagem && $mileagemnumericcheck && $mileagekm && $mileagekmnumericcheck && $owners && $ownersnumericcheck && $doors && $doorsnumericcheck && $location && $info && $filesize1 && $filesize2 && $filesize3 && $filenamecheck1 && filenamecheck2 && filenamecheck3 && $filetypecheck1 && $filetypecheck2 && $filetypecheck3 && $fileexistcheck1 && $fileexistcheck2 && $fileexistcheck3) { //FIRST DEFINE PATHS AS VARIABLES $path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0]; $path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1]; $path3= "upload/".$HTTP_POST_FILES['ufile']['name'][2]; //NEXT POST THE IMAGES TO THE DESIGNATED FOLDER ON THE SERVER copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1); copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2); copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3); //PICK UP AND DEFINE INPUT AS INDIVIDUAL VARIABLES //CONNECT TO RELEVANT DATABASE //THE CONNECTION MUST PRECEDE THE mysql_real_escape_string FUNCTION. include("dbinfo.php"); mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to establish a connection to the relevant database."); $path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0]; $path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1]; $path3= "upload/".$HTTP_POST_FILES['ufile']['name'][2]; $make = mysql_real_escape_string($_POST['make']); $model = mysql_real_escape_string($_POST['model']); $price = mysql_real_escape_string($_POST['price']); $engine = mysql_real_escape_string($_POST['engine']); $body = mysql_real_escape_string($_POST['body']); $transmission = mysql_real_escape_string($_POST['transmission']); $year =mysql_real_escape_string($_POST['year']); $colour =mysql_real_escape_string($_POST['colour']); $mileagem = mysql_real_escape_string($_POST['mileagem']); $mileagekm = mysql_real_escape_string($_POST['mileagekm']); $owners = mysql_real_escape_string($_POST['owners']); $doors = mysql_real_escape_string($_POST['doors']); $location = mysql_real_escape_string($_POST['location']); $info = mysql_real_escape_string($_POST['info']); //DEFINE ADDITIONAL VARIABLES /* PHP uses unix timestamps for all its date functionality. It has methods to convert these timestamps into pretty much any text format you could want but internally it uses the timestamp format. A timestamp is simply an unsigned integer. Specifically, it’s the number of seconds that have elapsed since midnight on January 1st 1970 (greenwich mean time). MySQL has three date types for use in columns. These are DATETIME, DATE, and TIMESTAMP. DATETIME EXAMPLE: YYYY-MM-DD HH:MM:SS (e.g. 2006-12-25 13:43:15) DATE EXAMPLE: YYYY-MM-DD (e.g. 2006-12-25) */ //$now_datetime = date('Y-m-d h:i:s'); $ipaddress = getenv('REMOTE_ADDR'); //CONNECTION WAS 'ERE //INSERT THE INPUT INTO DATABASE $query = "INSERT INTO test VALUES ('','$make','$model','$price','$engine','$body','$transmission','$year','$colour','$mileagem','$mileagekm','$owners','$doors','$location','$info',NOW(),'$ipaddress','$path1','$path2','$path1')"; mysql_query($query); //NEXT DISPLAY A SUMMARY OF WHAT HAS BEEN UPLOADED echo "This item has been successfully submitted to the server.</br></br> The following image file have been appended to your uploaded:</br></br> "; echo "File Name :".$HTTP_POST_FILES['ufile']['name'][0]."<BR/>"; //echo "File Size :".$HTTP_POST_FILES['ufile']['size'][0]." KB<BR/>"; echo "File Type :".$HTTP_POST_FILES['ufile']['type'][0]."<BR/>"; //echo "<img src=\"$path1\" height=\"150\">"; echo "<P>"; echo "File Name :".$HTTP_POST_FILES['ufile']['name'][1]."<BR/>"; //echo "File Size :".$HTTP_POST_FILES['ufile']['size'][1]."<BR/>"; echo "File Type :".$HTTP_POST_FILES['ufile']['type'][1]."<BR/>"; //echo "<img src=\"$path2\" height=\"150\">"; echo "<P>"; echo "File Name :".$HTTP_POST_FILES['ufile']['name'][2]."<BR/>"; //echo "File Size :".$HTTP_POST_FILES['ufile']['size'][2]."<BR/>"; echo "File Type :".$HTTP_POST_FILES['ufile']['type'][2]."<BR/>"; //echo "<img src=\"$path3\" height=\"150\">"; echo "</br>It's best practice to confirm that your item has been submitted as desired</br></br> [place button here to link to view relevant section]</br></br> If you notice an error as a result of human input please find and delete the item indefinitely and reattempt its submission. Items should also be deleted as soon as they become redundant (e.g. vehichle sold)</br></br> Should you experience further technical difficulty please contact the web master:</br></br> [place button here to link to web master cms error log form] "; //VERY IMPORTANT! EXIT(); WILL NO LONGER DISPLAY THE FORM exit(); //CLOSE: IF ALL INPUT FIELDS TRUE: } //CLOSE: OVERALL VALIDATION: } ?> <HTML> <HEAD> </HEAD> <BODY> <!--BEGIN FORM AND SET IT TO PROCESS SELF AND HANDLE MULTIPART/FORM-DATA--> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data"> <fieldset> <?php // IF ANY INDIVIDUAL INPUT FIELD IS DEFINED AS FALSE THEN DISPLAY ERROR MESSAGE if ($message_make || $message_model || $message_price || $message_pricenumericcheck || $message_engine || $message_body || $message_transmission || $message_year || $message_colour || $message_mileagem || $message_mileagekm || $message_owners || $message_doors || $message_location || $message_info || $message_filesize2 || $message_filesize1 || $message_filesize2 || $message_filesize3 || $message_filesize1 || $message_filesize2 || $message_filesize3 || $message_filenamecheck || $message_filetypecheck1 || $message_filetypecheck2 || $message_filetypecheck3 || $message_fileexistcheck1 || $message_fileexistcheck2 || $message_fileexistcheck3 || $message_yearnumericcheck || $message_mileagemnumericcheck || $message_mileagekmnumericcheck || $message_ownersnumericcheck || $message_doorsnumericcheck) // echo '*Your request could not be sent because some of the information is missing.</br></br>'; ?> <ol> <!--BEGIN REGULAR TEXT FIELDS--> <php? /* THE PHP BLOCK OF CODE IN THE VALUE OF EACH TEXT FIELD IS SIMPLY GIVING THE ELEMENT THE VALUE OF WHATEVER IS INPUTTED INTO IT BY THE USER. CLEVER BUT NOT SURE IF ITS ACTUALLY DOING A WHOLE LOT. */ ?> <li> <label for="make">Make:</label> <input type="text" name="make" id="make" class="text" value="<?php if (isset($_POST['make'])) echo $_POST['make']; ?>"/> <?php if ($message_make) echo ''.$message_make.''; ?> </br> </li> <li> <label for="model">Model:</label> <input type="text" name="model" id="model" class="text" value="<?php if (isset($_POST['model'])) echo $_POST['model']; ?>"/> <?php if ($message_model) echo ''.$message_model.''; ?> </br> </li> <li> <label for="price">Price:</label> <input type="text" name="price" id="price" class="text" value="<?php if (isset($_POST['price'])) echo $_POST['price']; ?>"/> <?php if ($message_price) echo ''.$message_price.''; ?> <?php if ($message_pricenumericcheck) echo ''.$message_pricenumericcheck.''; ?> </br> </li> <li> <label for="engine">Engine:</label> <input type="text" name="engine" id="engine" class="text" value="<?php if (isset($_POST['engine'])) echo $_POST['engine']; ?>"/> <?php if ($message_engine) echo ''.$message_engine.''; ?></br> </li> <li> <label for="body">Body Type:</label> <input type="text" name="body" id="body" class="text" value="<?php if (isset($_POST['body'])) echo $_POST['body']; ?>"/> <?php if ($message_body) echo ''.$message_body.''; ?></br> </li> <li> <label for="transmission">Transmission:</label> <input type="text" name="transmission" id="transmission" class="text" value="<?php if (isset($_POST['transmission'])) echo $_POST['transmission']; ?>"/> <?php if ($message_transmission) echo ''.$message_transmission.''; ?></br> </li> <li> <label for="year">Year:</label> <input type="text" name="year" id="year" class="text" value="<?php if (isset($_POST['year'])) echo $_POST['year']; ?>"/> <?php if ($message_year) echo ''.$message_year.''; ?> <?php if ($message_yearnumericcheck) echo ''.$message_yearnumericcheck.''; ?> </br> </li> <li> <label for="colour">Colour:</label> <input type="text" name="colour" id="colour" class="text" value="<?php if (isset($_POST['colour'])) echo $_POST['colour']; ?>"/> <?php if ($message_colour) echo ''.$message_colour.''; ?></br> </li> <li> <label for="mileagem">Mileage M:</label> <input type="text" name="mileagem" id="mileagem" class="text" value="<?php if (isset($_POST['mileagem'])) echo $_POST['mileagem']; ?>"/> <?php if ($message_mileagem) echo ''.$message_mileagem.''; ?> <?php if ($message_mileagemnumericcheck) echo ''.$message_mileagemnumericcheck.''; ?> </br> </li> <li> <label for="mileagekm">Mileage KM:</label> <input type="text" name="mileagekm" id="mileagekm" class="text" value="<?php if (isset($_POST['mileagekm'])) echo $_POST['mileagekm']; ?>"/> <?php if ($message_mileagekm) echo ''.$message_mileagekm.''; ?> <?php if ($message_mileagemnumericcheck) echo ''.$message_mileagemnumericcheck.''; ?> </br> </li> <li> <label for="owners">Owners:</label> <input type="text" name="owners" id="owners" class="text" value="<?php if (isset($_POST['owners'])) echo $_POST['owners']; ?>"/> <?php if ($message_owners) echo ''.$message_owners.''; ?> <?php if ($message_ownersnumericcheck) echo ''.$message_ownersnumericcheck.''; ?> </br> </li> <li> <label for="doors">Doors:</label> <input type="text" name="doors" id="doors" class="text" value="<?php if (isset($_POST['doors'])) echo $_POST['doors']; ?>"/> <?php if ($message_doors) echo ''.$message_doors.''; ?> <?php if ($message_doorsnumericcheck) echo ''.$message_doorsnumericcheck.''; ?> </br> </li> <li> <label for="location">Location:</label> <input type="text" name="location" id="location" class="text" value="<?php if (isset($_POST['location'])) echo $_POST['location']; ?>"/> <?php if ($message_location) echo ''.$message_location.''; ?></br> </li> <!--BEGIN TEXT AREA--> <li> <label for="info">Additional Information:</label></br> <textarea name="info" rows="5" cols="50"/><?php if (isset($_POST['info'])) echo stripslashes($_POST['info']); ?></textarea> <?php if ($message_info) echo ''.$message_info.''; ?> </br> </li> <!--BEGIN FILE UPLOADS--> <li> <label for "ufile[]">Image File 1:</label> <input type="file" name="ufile[]" id="ufile[]"/> <?php if ($message_filesize1) echo ''.$message_filesize1.''; ?> <?php if ($message_filetypecheck1) echo ''.$message_filetypecheck1.''; ?> <?php if ($message_fileexistcheck1) echo ''.$message_fileexistcheck1.''; ?> </li> <li> <label for "ufile[]">Image File 2:</label> <input type="file" name="ufile[]" id="ufile[]"/> <?php if ($message_filesize2) echo ''.$message_filesize2.''; ?> <?php if ($message_filetypecheck2) echo ''.$message_filetypecheck2.''; ?> <?php if ($message_fileexistcheck2) echo ''.$message_fileexistcheck2.''; ?> </li> <li> <label for "ufile[]">Image File 3:</label> <input type="file" name="ufile[]" id="ufile[]"/> <?php if ($message_filesize3) echo ''.$message_filesize3.''; ?> <?php if ($message_filetypecheck3) echo ''.$message_filetypecheck3.''; ?> <?php if ($message_fileexistcheck3) echo ''.$message_fileexistcheck3.''; ?> </br> <?php if ($filenamecheck_message) echo ''.$filenamecheck_message.''; ?> </li> <li> <input name="submit" type="submit"> </li> </ol> </fieldset> </form> <!-- WT88 CODE --> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input type="text" name="distance" value="<?php echo $dist; ?>" /> <select name="action"> <?php $actions = array('Miles to Kilometers', 'Kilometers to Miles'); foreach($actions as $key => $action) { $selected = (isset($_POST['action']) && $_POST['action'] == $key) ? ' selected="selected"' : null; echo ' <option value="'.$key.'"'.$selected.'>'.$action."</option>\n"; } ?> </select> <?php // display the result. echo number_format($result, 2, '.', ',') . $unit; ?> </br><input type="submit" name="submit" value="Convert"> </form> </BODY> </HTML> I've prefixed Wildteens bits with WT88 if you want to use CTRL + F to jump to them. I know each (your code and my code) function fine by themselves but when combined I get the following error: Parse error: syntax error, unexpected $end in XXX on line 749 Quote Link to comment https://forums.phpfreaks.com/topic/133831-solved-three-forms-on-one-page-clearing-each-others-input/#findComment-696559 Share on other sites More sharing options...
webmaster1 Posted November 22, 2008 Author Share Posted November 22, 2008 Here's my code without the calculations or your suggested code. <?php if (isset($_POST['submit'])) //PROCESS THE FORM WHEN THE SUBMIT BUTTON IS PRESSED { //VALIDATE THE INDIVIDUAL FIELDS AND DEFINE THEM AS TRUE OR FALSE BASED ON THE USER'S INPUT //REGULAR TEXT FIELDS if (strlen($_POST['make']) > 0) {$make=TRUE;} else {$make=FALSE; $message_make=" *You forgot to enter the make!";} if (strlen($_POST['model']) > 0) {$model=TRUE;} else {$model=FALSE; $message_model=" *You forgot to enter the model!"; } if (strlen($_POST['price']) > 0) {$price=TRUE; if (is_numeric($_POST['price'])) {$pricenumericcheck=TRUE;} else {$pricenumericcheck=FALSE; $message_pricenumericcheck=" *Please enter numeric values only! (e.g. Do not input commas or the euro symbol!)"; //echo "$message_pricenumericcheck"; } } else {$price=FALSE; $message_price=" *You forgot to enter the price!";} if (strlen($_POST['engine']) > 0) {$engine=TRUE;} else {$engine=FALSE; $message_engine=" *You forgot to enter the engine!";} if (strlen($_POST['body']) > 0) {$body=TRUE;} else {$body=FALSE; $message_body=" *You forgot to enter the body!";} if (strlen($_POST['transmission']) > 0) {$transmission=TRUE;} else {$transmission=FALSE; $message_transmission=" *You forgot to enter the transmission!";} if (strlen($_POST['year']) > 0) {$year=TRUE; if (is_numeric($_POST['year'])) {$yearnumericcheck=TRUE; if((strlen($_POST['year']) > 4) || (strlen($_POST['year']) < 4)) { $yearnumericcheck=FALSE; $message_yearnumericcheck=" *Please enter a four digit numeric value only! (e.g. 1983, 1999, 2010.)"; //echo "$message_yearnumericcheck"; } } else {$yearnumericcheck=FALSE; $message_yearnumericcheck=" *Please enter a four digit numeric value only! (e.g. 1983, 1999, 2010.)"; //echo "$message_yearnumericcheck"; } } else {$year=FALSE; $message_year=" *You forgot to enter the year!";} if (strlen($_POST['colour']) > 0) {$colour=TRUE;} else {$colour=FALSE; $message_colour=" *You forgot to enter the colour!";} if (strlen($_POST['mileagem']) > 0) {$mileagem=TRUE; if (is_numeric($_POST['mileagem'])) {$mileagemnumericcheck=TRUE;} else {$mileagemnumericcheck=FALSE; $message_mileagemnumericcheck=" *Please enter numeric values only! (e.g. Do not input commas or the mile symbol!)"; //echo "$message_mileagemnumericcheck"; } } else {$mileagem=FALSE; $message_mileagem=" *You forgot to enter the mileage in miles!";} if (strlen($_POST['mileagekm']) > 0) {$mileagekm=TRUE; if (is_numeric($_POST['mileagekm'])) {$mileagekmnumericcheck=TRUE;} else {$mileagekmnumericcheck=FALSE; $message_mileagekmnumericcheck=" *Please enter numeric values only! (e.g. Do not input commas or the kilometre symbol!)"; //echo "$message_mileagekmnumericcheck"; } } else {$mileagekm=FALSE; $message_mileagekm=" *You forgot to enter the mileage in kilometres!";} if (strlen($_POST['owners']) > 0) {$owners=TRUE; if (is_numeric($_POST['owners'])) {$ownersnumericcheck=TRUE;} else {$ownersnumericcheck=FALSE; $message_ownersnumericcheck=" *Please enter numeric values onlyfor owners! (e.g. 1, 2, 3.!)"; //echo "$message_ownersnumericcheck"; } } else {$owners=FALSE; $message_owners=" *You forgot to enter the owners in miles!";} if (strlen($_POST['doors']) > 0) {$doors=TRUE; if (is_numeric($_POST['doors'])) {$doorsnumericcheck=TRUE;} else {$doorsnumericcheck=FALSE; $message_doorsnumericcheck=" *Please enter numeric values only for doors! (e.g. 2, 4, 6.!)"; //echo "$message_doorsnumericcheck"; } } else {$doors=FALSE; $message_doors=" *You forgot to enter the owners in doors!";} if (strlen($_POST['location']) > 0) {$location=TRUE;} else {$location=FALSE; $message_location=" *You forgot to enter the location!";} //TEXT AREA FIELD if (isset($_POST['info'])) { $info = trim($_POST['info']); if ($info == "" || empty($info)) { $message_info = "Please enter data for info!"; } } //UPLOAD FILE FIELDS //CHECK IF UPLOAD FILE FIELD 1 IS EMPTY $filesize1=$HTTP_POST_FILES['ufile']['size'][0]; if($filesize1==0) { $filesize1=FALSE; $message_filesize1="You forgot to enter this image file!"; //echo "$message_filesize1"; } else { $filesize1=TRUE; } //CHECK IF UPLOAD FILE FIELD 1 IS EMPTY $filesize2=$HTTP_POST_FILES['ufile']['size'][1]; if($filesize2==0) { $filesize2=FALSE; $message_filesize2="You forgot to enter this image file!"; //echo "$message_filesize2"; } else { $filesize2=TRUE; } //CHECK IF UPLOAD FILE FIELD 3 IS EMPTY $filesize3=$HTTP_POST_FILES['ufile']['size'][2]; if($filesize3==0) { $filesize3=FALSE; $message_filesize3="You forgot to enter this image file!"; //echo "$message_filesize3"; } else { $filesize3=TRUE; } //CHECK IF ANY THE FILES ARE THE SAME: $filenamecheck1= $HTTP_POST_FILES['ufile']['name'][0]; $filenamecheck2= $HTTP_POST_FILES['ufile']['name'][1]; $filenamecheck3= $HTTP_POST_FILES['ufile']['name'][2]; $filesize1=$HTTP_POST_FILES['ufile']['size'][0]; $filesize2=$HTTP_POST_FILES['ufile']['size'][1]; $filesize3=$HTTP_POST_FILES['ufile']['size'][2]; if ( (($filenamecheck1 == $filenamecheck2) || ($filenamecheck2 == $filenamecheck3) || ($filenamecheck1 == $filenamecheck3)) && ((!$filesize1==0 && isset($_POST['submit'])) && (!$filesize2==0 && isset($_POST['submit'])) && (!$filesize3==0 && isset($_POST['submit']))) ) { $filenamecheck_message = "one ore more of the files are the same"; //echo "$message_filenamecheck"; $message_filenamecheck = TRUE; $filenamecheck1= FALSE; $filenamecheck2= FALSE; $filenamecheck3= FALSE; } else { $message_filenamecheck = FALSE; $filenamecheck1= TRUE; $filenamecheck2= TRUE; $filenamecheck3= TRUE; } //CHECK IF FILE 1 IS NOT A *.JPEG OR *.GIF $filesize1=$HTTP_POST_FILES['ufile']['size'][0]; if ( ( ($HTTP_POST_FILES['ufile']["type"][0] == "image/gif") || ($HTTP_POST_FILES['ufile']["type"][0] == "image/jpeg") || ($HTTP_POST_FILES['ufile']["type"][0] == "image/pjpeg") ) // &&($HTTP_POST_FILES['ufile']["size"][0] < 50000) ) { $filetypecheck1=TRUE; } else { if (!($filesize1 == 0)) { $filetypecheck1=FALSE; $message_filetypecheck1="The first of the sumbitted files is not of a *.JPG ! *.GIF file type!"; //echo "$message_filetypecheck1"; } } //CHECK IF FILE 2 IS NOT A *.JPEG OR *.GIF $filesize2=$HTTP_POST_FILES['ufile']['size'][1]; if ( ( ($HTTP_POST_FILES['ufile']["type"][1] == "image/gif") || ($HTTP_POST_FILES['ufile']["type"][1] == "image/jpeg") || ($HTTP_POST_FILES['ufile']["type"][1] == "image/pjpeg") ) // &&($HTTP_POST_FILES['ufile']["size"][1] < 50000) ) { $filetypecheck2=TRUE; } else { if (!($filesize2 == 0)) { $filetypecheck2=FALSE; $message_filetypecheck2="The second of the sumbitted files is not of a *.JPG ! *.GIF file type!"; //echo "$message_filetypecheck2"; } } //CHECK IF FILE 3 IS NOT *.JPEG OR *.GIF $filesize3=$HTTP_POST_FILES['ufile']['size'][2]; if ( ( ($HTTP_POST_FILES['ufile']["type"][2] == "image/gif") || ($HTTP_POST_FILES['ufile']["type"][2] == "image/jpeg") || ($HTTP_POST_FILES['ufile']["type"][2] == "image/pjpeg") ) // &&($HTTP_POST_FILES['ufile']["size"][2] < 50000) ) { $filetypecheck3=TRUE; } else { if (!($filesize3 == 0)) { $filetypecheck3=FALSE; $message_filetypecheck3="The third of the sumbitted files is not of a *.JPG ! *.GIF file type!"; //echo "$message_filetypecheck3"; } } //CHECK IF FILE 1 EXISTS ALREADY $path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0]; $filesize1=$HTTP_POST_FILES['ufile']['size'][0]; if ( (file_exists($path1)) && (!($filesize1 == 0)) ) { $fileexistcheck1=FALSE; $message_fileexistcheck1="The first file already exist on the server!"; //echo "$message_fileexistcheck1"; } else { $fileexistcheck1=TRUE; } //CHECK IF FILE 2 EXISTS ALREADY $path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1]; $filesize2=$HTTP_POST_FILES['ufile']['size'][1]; if ( (file_exists($path2)) && (!($filesize2 == 0)) ) { $fileexistcheck2=FALSE; $message_fileexistcheck2="The second file already exist on the server!"; //echo "$message_fileexistcheck2"; } else { $fileexistcheck2=TRUE; } //CHECK IF FILE 3 EXISTS ALREADY $path3= "upload/".$HTTP_POST_FILES['ufile']['name'][2]; $filesize3=$HTTP_POST_FILES['ufile']['size'][2]; if ( (file_exists($path3)) && (!($filesize3 == 0)) ) { $fileexistcheck3=FALSE; $message_fileexistcheck3="The third file already exist on the server!"; //echo "$message_fileexistcheck3"; } else { $fileexistcheck3=TRUE; } //IF ALL INPUT FIELDS ARE DEFINED AS TRUE / VALIDATED if ($make && $model && $price && $pricenumericcheck && $engine && $body && $transmission && $year && $yearnumericcheck && $colour && $mileagem && $mileagemnumericcheck && $mileagekm && $mileagekmnumericcheck && $owners && $ownersnumericcheck && $doors && $doorsnumericcheck && $location && $info && $filesize1 && $filesize2 && $filesize3 && $filenamecheck1 && filenamecheck2 && filenamecheck3 && $filetypecheck1 && $filetypecheck2 && $filetypecheck3 && $fileexistcheck1 && $fileexistcheck2 && $fileexistcheck3) { //FIRST DEFINE PATHS AS VARIABLES $path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0]; $path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1]; $path3= "upload/".$HTTP_POST_FILES['ufile']['name'][2]; //NEXT POST THE IMAGES TO THE DESIGNATED FOLDER ON THE SERVER copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1); copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2); copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3); //PICK UP AND DEFINE INPUT AS INDIVIDUAL VARIABLES //CONNECT TO RELEVANT DATABASE //THE CONNECTION MUST PRECEDE THE mysql_real_escape_string FUNCTION. include("dbinfo.php"); mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to establish a connection to the relevant database."); $path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0]; $path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1]; $path3= "upload/".$HTTP_POST_FILES['ufile']['name'][2]; $make = mysql_real_escape_string($_POST['make']); $model = mysql_real_escape_string($_POST['model']); $price = mysql_real_escape_string($_POST['price']); $engine = mysql_real_escape_string($_POST['engine']); $body = mysql_real_escape_string($_POST['body']); $transmission = mysql_real_escape_string($_POST['transmission']); $year =mysql_real_escape_string($_POST['year']); $colour =mysql_real_escape_string($_POST['colour']); $mileagem = mysql_real_escape_string($_POST['mileagem']); $mileagekm = mysql_real_escape_string($_POST['mileagekm']); $owners = mysql_real_escape_string($_POST['owners']); $doors = mysql_real_escape_string($_POST['doors']); $location = mysql_real_escape_string($_POST['location']); $info = mysql_real_escape_string($_POST['info']); //DEFINE ADDITIONAL VARIABLES /* PHP uses unix timestamps for all its date functionality. It has methods to convert these timestamps into pretty much any text format you could want but internally it uses the timestamp format. A timestamp is simply an unsigned integer. Specifically, it’s the number of seconds that have elapsed since midnight on January 1st 1970 (greenwich mean time). MySQL has three date types for use in columns. These are DATETIME, DATE, and TIMESTAMP. DATETIME EXAMPLE: YYYY-MM-DD HH:MM:SS (e.g. 2006-12-25 13:43:15) DATE EXAMPLE: YYYY-MM-DD (e.g. 2006-12-25) */ //$now_datetime = date('Y-m-d h:i:s'); $ipaddress = getenv('REMOTE_ADDR'); //CONNECTION WAS 'ERE //INSERT THE INPUT INTO DATABASE $query = "INSERT INTO test VALUES ('','$make','$model','$price','$engine','$body','$transmission','$year','$colour','$mileagem','$mileagekm','$owners','$doors','$location','$info',NOW(),'$ipaddress','$path1','$path2','$path1')"; mysql_query($query); //NEXT DISPLAY A SUMMARY OF WHAT HAS BEEN UPLOADED echo "This item has been successfully submitted to the server.</br></br> The following image file have been appended to your uploaded:</br></br> "; echo "File Name :".$HTTP_POST_FILES['ufile']['name'][0]."<BR/>"; //echo "File Size :".$HTTP_POST_FILES['ufile']['size'][0]." KB<BR/>"; echo "File Type :".$HTTP_POST_FILES['ufile']['type'][0]."<BR/>"; //echo "<img src=\"$path1\" height=\"150\">"; echo "<P>"; echo "File Name :".$HTTP_POST_FILES['ufile']['name'][1]."<BR/>"; //echo "File Size :".$HTTP_POST_FILES['ufile']['size'][1]."<BR/>"; echo "File Type :".$HTTP_POST_FILES['ufile']['type'][1]."<BR/>"; //echo "<img src=\"$path2\" height=\"150\">"; echo "<P>"; echo "File Name :".$HTTP_POST_FILES['ufile']['name'][2]."<BR/>"; //echo "File Size :".$HTTP_POST_FILES['ufile']['size'][2]."<BR/>"; echo "File Type :".$HTTP_POST_FILES['ufile']['type'][2]."<BR/>"; //echo "<img src=\"$path3\" height=\"150\">"; echo "</br>It's best practice to confirm that your item has been submitted as desired</br></br> [place button here to link to view relevant section]</br></br> If you notice an error as a result of human input please find and delete the item indefinitely and reattempt its submission. Items should also be deleted as soon as they become redundant (e.g. vehichle sold)</br></br> Should you experience further technical difficulty please contact the web master:</br></br> [place button here to link to web master cms error log form] "; //VERY IMPORTANT! EXIT(); WILL NO LONGER DISPLAY THE FORM exit(); //CLOSE: IF ALL INPUT FIELDS TRUE: } //CLOSE: OVERALL VALIDATION: } ?> <HTML> <HEAD> </HEAD> <BODY> <!--BEGIN FORM AND SET IT TO PROCESS SELF AND HANDLE MULTIPART/FORM-DATA--> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data"> <fieldset> <?php // IF ANY INDIVIDUAL INPUT FIELD IS DEFINED AS FALSE THEN DISPLAY ERROR MESSAGE if ($message_make || $message_model || $message_price || $message_pricenumericcheck || $message_engine || $message_body || $message_transmission || $message_year || $message_colour || $message_mileagem || $message_mileagekm || $message_owners || $message_doors || $message_location || $message_info || $message_filesize2 || $message_filesize1 || $message_filesize2 || $message_filesize3 || $message_filesize1 || $message_filesize2 || $message_filesize3 || $message_filenamecheck || $message_filetypecheck1 || $message_filetypecheck2 || $message_filetypecheck3 || $message_fileexistcheck1 || $message_fileexistcheck2 || $message_fileexistcheck3 || $message_yearnumericcheck || $message_mileagemnumericcheck || $message_mileagekmnumericcheck || $message_ownersnumericcheck || $message_doorsnumericcheck) // echo '*Your request could not be sent because some of the information is missing.</br></br>'; ?> <ol> <!--BEGIN REGULAR TEXT FIELDS--> <php? /* THE PHP BLOCK OF CODE IN THE VALUE OF EACH TEXT FIELD IS SIMPLY GIVING THE ELEMENT THE VALUE OF WHATEVER IS INPUTTED INTO IT BY THE USER. CLEVER BUT NOT SURE IF ITS ACTUALLY DOING A WHOLE LOT. */ ?> <li> <label for="make">Make:</label> <input type="text" name="make" id="make" class="text" value="<?php if (isset($_POST['make'])) echo $_POST['make']; ?>"/> <?php if ($message_make) echo ''.$message_make.''; ?> </br> </li> <li> <label for="model">Model:</label> <input type="text" name="model" id="model" class="text" value="<?php if (isset($_POST['model'])) echo $_POST['model']; ?>"/> <?php if ($message_model) echo ''.$message_model.''; ?> </br> </li> <li> <label for="price">Price:</label> <input type="text" name="price" id="price" class="text" value="<?php if (isset($_POST['price'])) echo $_POST['price']; ?>"/> <?php if ($message_price) echo ''.$message_price.''; ?> <?php if ($message_pricenumericcheck) echo ''.$message_pricenumericcheck.''; ?> </br> </li> <li> <label for="engine">Engine:</label> <input type="text" name="engine" id="engine" class="text" value="<?php if (isset($_POST['engine'])) echo $_POST['engine']; ?>"/> <?php if ($message_engine) echo ''.$message_engine.''; ?></br> </li> <li> <label for="body">Body Type:</label> <input type="text" name="body" id="body" class="text" value="<?php if (isset($_POST['body'])) echo $_POST['body']; ?>"/> <?php if ($message_body) echo ''.$message_body.''; ?></br> </li> <li> <label for="transmission">Transmission:</label> <input type="text" name="transmission" id="transmission" class="text" value="<?php if (isset($_POST['transmission'])) echo $_POST['transmission']; ?>"/> <?php if ($message_transmission) echo ''.$message_transmission.''; ?></br> </li> <li> <label for="year">Year:</label> <input type="text" name="year" id="year" class="text" value="<?php if (isset($_POST['year'])) echo $_POST['year']; ?>"/> <?php if ($message_year) echo ''.$message_year.''; ?> <?php if ($message_yearnumericcheck) echo ''.$message_yearnumericcheck.''; ?> </br> </li> <li> <label for="colour">Colour:</label> <input type="text" name="colour" id="colour" class="text" value="<?php if (isset($_POST['colour'])) echo $_POST['colour']; ?>"/> <?php if ($message_colour) echo ''.$message_colour.''; ?></br> </li> <li> <label for="mileagem">Mileage M:</label> <input type="text" name="mileagem" id="mileagem" class="text" value="<?php if (isset($_POST['mileagem'])) echo $_POST['mileagem']; ?>"/> <?php if ($message_mileagem) echo ''.$message_mileagem.''; ?> <?php if ($message_mileagemnumericcheck) echo ''.$message_mileagemnumericcheck.''; ?> </br> </li> <li> <label for="mileagekm">Mileage KM:</label> <input type="text" name="mileagekm" id="mileagekm" class="text" value="<?php if (isset($_POST['mileagekm'])) echo $_POST['mileagekm']; ?>"/> <?php if ($message_mileagekm) echo ''.$message_mileagekm.''; ?> <?php if ($message_mileagemnumericcheck) echo ''.$message_mileagemnumericcheck.''; ?> </br> </li> <li> <label for="owners">Owners:</label> <input type="text" name="owners" id="owners" class="text" value="<?php if (isset($_POST['owners'])) echo $_POST['owners']; ?>"/> <?php if ($message_owners) echo ''.$message_owners.''; ?> <?php if ($message_ownersnumericcheck) echo ''.$message_ownersnumericcheck.''; ?> </br> </li> <li> <label for="doors">Doors:</label> <input type="text" name="doors" id="doors" class="text" value="<?php if (isset($_POST['doors'])) echo $_POST['doors']; ?>"/> <?php if ($message_doors) echo ''.$message_doors.''; ?> <?php if ($message_doorsnumericcheck) echo ''.$message_doorsnumericcheck.''; ?> </br> </li> <li> <label for="location">Location:</label> <input type="text" name="location" id="location" class="text" value="<?php if (isset($_POST['location'])) echo $_POST['location']; ?>"/> <?php if ($message_location) echo ''.$message_location.''; ?></br> </li> <!--BEGIN TEXT AREA--> <li> <label for="info">Additional Information:</label></br> <textarea name="info" rows="5" cols="50"/><?php if (isset($_POST['info'])) echo stripslashes($_POST['info']); ?></textarea> <?php if ($message_info) echo ''.$message_info.''; ?> </br> </li> <!--BEGIN FILE UPLOADS--> <li> <label for "ufile[]">Image File 1:</label> <input type="file" name="ufile[]" id="ufile[]"/> <?php if ($message_filesize1) echo ''.$message_filesize1.''; ?> <?php if ($message_filetypecheck1) echo ''.$message_filetypecheck1.''; ?> <?php if ($message_fileexistcheck1) echo ''.$message_fileexistcheck1.''; ?> </li> <li> <label for "ufile[]">Image File 2:</label> <input type="file" name="ufile[]" id="ufile[]"/> <?php if ($message_filesize2) echo ''.$message_filesize2.''; ?> <?php if ($message_filetypecheck2) echo ''.$message_filetypecheck2.''; ?> <?php if ($message_fileexistcheck2) echo ''.$message_fileexistcheck2.''; ?> </li> <li> <label for "ufile[]">Image File 3:</label> <input type="file" name="ufile[]" id="ufile[]"/> <?php if ($message_filesize3) echo ''.$message_filesize3.''; ?> <?php if ($message_filetypecheck3) echo ''.$message_filetypecheck3.''; ?> <?php if ($message_fileexistcheck3) echo ''.$message_fileexistcheck3.''; ?> </br> <?php if ($filenamecheck_message) echo ''.$filenamecheck_message.''; ?> </li> <li> <input name="submit" type="submit"> </li> </ol> </fieldset> </form> </BODY> </HTML> Quote Link to comment https://forums.phpfreaks.com/topic/133831-solved-three-forms-on-one-page-clearing-each-others-input/#findComment-696560 Share on other sites More sharing options...
wildteen88 Posted November 22, 2008 Share Posted November 22, 2008 of course my code wont work with your code. Mine is just an example of how to archive what you're tying to do with just one form rather than three forms. What are you tying to do? I cannot see where you are wanting to convert mileage to kilometres and vies versa Quote Link to comment https://forums.phpfreaks.com/topic/133831-solved-three-forms-on-one-page-clearing-each-others-input/#findComment-696567 Share on other sites More sharing options...
webmaster1 Posted November 22, 2008 Author Share Posted November 22, 2008 Ah, sorry about that. I thought the code chunk in my first post broke it down. Synopsis: [*]User inputs data into main form. [*]User will have either a kilometer or mile mileage but the form will prompt them for both. [*]User inputs whatever they have into a subsidiary (separate) form (the one you posted) to calculate the requisite mileage. [*]User copies and pastes the resulting mileage into the main form and continues completing it. My problem is if I create two seperate forms the clicking of a submit button of one wipes out the existing content of another (meaning the user will have to restart their input each time they wish to make a calculation). Quote Link to comment https://forums.phpfreaks.com/topic/133831-solved-three-forms-on-one-page-clearing-each-others-input/#findComment-696570 Share on other sites More sharing options...
webmaster1 Posted November 23, 2008 Author Share Posted November 23, 2008 Any takers on this? To recap and to save any of you having to read through the entire thread here's a summary of the post: [*]I asked how combine three forms without the user input being reset as the user interchanges forms. [*]Genericnumber1 advised that I combine the forms. [*]Wildteen88 showed me how to combine two of the calculation forms but not how to combine them with the main form that actually writes to the database. [*]Back to square one. I still have two forms and as a result my original problem persists. I've been trying to digest Wildteen88's code in the meantime: Mine is just an example of how to archive what you're tying to do with just one form rather than three forms. It seems like I could add a third case (this could be what he was suggesting) though I don't have a clue how to manipulate the $actions array. Quote Link to comment https://forums.phpfreaks.com/topic/133831-solved-three-forms-on-one-page-clearing-each-others-input/#findComment-696617 Share on other sites More sharing options...
webmaster1 Posted November 23, 2008 Author Share Posted November 23, 2008 No assist on this one? Its certainly quite here today. Quote Link to comment https://forums.phpfreaks.com/topic/133831-solved-three-forms-on-one-page-clearing-each-others-input/#findComment-696985 Share on other sites More sharing options...
wildteen88 Posted November 23, 2008 Share Posted November 23, 2008 User inputs whatever they have into a subsidiary (separate) form (the one you posted) to calculate the requisite mileage. User copies and pastes the resulting mileage into the main form and continues completing it. What I'd do is just have one form field for entering the total mileage, eg 10,000. Next to that field I'd have a list which specifies the unit, eg Miles or Kilometers. What you're now need to do in your script is work out the opposite mileage based on what the user entered. Eg if the user entered Miles then you'll need to populate the Kilometers data field for the database and vise versa for Kilometers to Miles. To do this Change: if (strlen($_POST['mileagem']) > 0) {$mileagem=TRUE; if (is_numeric($_POST['mileagem'])) {$mileagemnumericcheck=TRUE;} else {$mileagemnumericcheck=FALSE; $message_mileagemnumericcheck=" *Please enter numeric values only! (e.g. Do not input commas or the mile symbol!)"; //echo "$message_mileagemnumericcheck"; } } else {$mileagem=FALSE; $message_mileagem=" *You forgot to enter the mileage in miles!";} if (strlen($_POST['mileagekm']) > 0) {$mileagekm=TRUE; if (is_numeric($_POST['mileagekm'])) {$mileagekmnumericcheck=TRUE;} else {$mileagekmnumericcheck=FALSE; $message_mileagekmnumericcheck=" *Please enter numeric values only! (e.g. Do not input commas or the kilometre symbol!)"; //echo "$message_mileagekmnumericcheck"; } } else {$mileagekm=FALSE; $message_mileagekm=" *You forgot to enter the mileage in kilometres!";} to: $milage_unit_options = array('Miles', 'Kilometers'); if( array_key_exists($_POST['mileageu'], $milage_unit_options) ) { if (is_numeric($_POST['mileagev']) && $_POST['mileagev'] > 0) { $mileagecheck = TRUE; // convertion take place here switch($_POST['mileageu']) { // user entered Miles case 0: $mileagem = $_POST['mileagev']; // we need to work out the kilometers $mileagekm = $mileagem*1.609; break; // user entered Kilometers case 1: $mileagekm = $_POST['mileagev']; // we need to work out the milage $mileagem = $mileagekm/1.609; break; } /*echo $mileagem . ' Miles<br />'; echo $mileagekm . ' Kilometers';*/ } else { $mileagecheck = FALSE; $message_mileagecheck = " *Please enter numeric values only! (e.g. Do not input commas or the mile symbol!)"; //echo "$message_mileagenumericcheck"; } } else { $mileagecheck = FALSE; $message_mileagecheck = " *Please ensure you have choosen Miles or Kilometers from the list provided"; //echo "$message_mileagenumericcheck"; } Next remove the following lines: $mileagem = mysql_real_escape_string($_POST['mileagem']); $mileagekm = mysql_real_escape_string($_POST['mileagekm']); Lastly change <li> <label for="mileagem">Mileage M:</label> <input type="text" name="mileagem" id="mileagem" class="text" value="<?php if (isset($_POST['mileagem'])) echo $_POST['mileagem']; ?>"/> <?php if ($message_mileagem) echo ''.$message_mileagem.''; ?> <?php if ($message_mileagemnumericcheck) echo ''.$message_mileagemnumericcheck.''; ?> </br> </li> <li> <label for="mileagekm">Mileage KM:</label> <input type="text" name="mileagekm" id="mileagekm" class="text" value="<?php if (isset($_POST['mileagekm'])) echo $_POST['mileagekm']; ?>"/> <?php if ($message_mileagekm) echo ''.$message_mileagekm.''; ?> <?php if ($message_mileagemnumericcheck) echo ''.$message_mileagemnumericcheck.''; ?> </br> </li> to <li> <label for="mileage">Mileage:</label> <input type="text" name="mileagev" id="mileagev" class="text" value="<?php if (isset($_POST['mileagev'])) echo $_POST['mileagev']; ?>"/> <select name="mileageu"> <?php $options = array('Miles', 'Kilometers'); foreach($options as $key => $value) { $selected = (isset($_POST['milageu']) && $_POST['milageu'] == $key) ? ' selected="selected"' : null; echo ' <option value="'.$key.'"'.$selected.'>'.$value."</option>\n"; } ?> </select> <?php if (isset($message_mileage)) echo $message_mileage; ?> <?php if (isset($message_mileagecheck)) echo $message_mileagecheck; ?> </br> </li> The script should automatically work out the opposite mileage without the user specifying it. Quote Link to comment https://forums.phpfreaks.com/topic/133831-solved-three-forms-on-one-page-clearing-each-others-input/#findComment-697010 Share on other sites More sharing options...
webmaster1 Posted November 24, 2008 Author Share Posted November 24, 2008 Thanks Wildteen88! Form fully functioning as desired! This works fantastically and as a process is far more inteligent than I had originally tried to implement. It minimizes user task by automating the calculations in the background. As a result of stewing over your code for a bit (I'm very slow with PHP at the moment ) I now have a handle on arrays and dropdown menus. Not bad aspects to add to a newbs repertoire, eh? Here's a little feedback for you: I modified the mileage process just to deliver three error messages rather than two: [*]If the dropdown is empty. [*]If the text field is empty. [*]If the text field is not numeric. Its consists of an IF within an IF within an IF. I'm sure it could be streamlined a little more but its sufficently functional nonetheless. Here's how it goes: <?php //DEFINE THE ARRAY AS A VARIABLE $milage_unit_options = array('Miles', 'Kilometers'); //IF THE A VALUE IN THE DROPDOWN IS SELECTED if( array_key_exists($_POST['mileageu'], $milage_unit_options) ) { $mileageudropdowncheck = TRUE; //IF THE TEXT FIELD IS NOT EMPTY if (strlen($_POST['mileagev']) > 0) { $mileagev=TRUE; //IF THE TEXT FIELD IS NUMERIC if (is_numeric($_POST['mileagev'])) { $mileagevnumericcheck=TRUE; //IF THE FORM PROCESSES THIS FAR IT HAS PASSED THE IF EMPTY AND IF NUMERIC CHECKS AND IS NOW ALLOWED THE CONVERSION CALCULATION //CONVERSION TAKES PLACE HERE switch($_POST['mileageu']) { //CASE 0: MILES IS SELECTED FROM DROPDOWN case 0: $mileagem = $_POST['mileagev']; //CONVERT MILES TO KILOMETRES $mileagekm = $mileagem*1.609; break; //CASE 1: KILOMETRES IS SELECTED FROM DROPDOWN case 1: $mileagekm = $_POST['mileagev']; //CONVERT KILOMETRES TO MILES $mileagem = $mileagekm/1.609; break; } } //ELSE: IF THE TEXT FIELD IS NOT NUMERIC else { $mileagevnumericcheck=FALSE; $message_mileagevnumericcheck="Please input numeric values only (Do not include M or Km symbols.)!"; //echo "$message_mileagevnumericcheck"; } } //ELSE: IF THE TEXT FIELD IS EMPTY else { $mileagev=FALSE; $message_mileagev="You forgot to input the mileage in either miles or kilometres!"; //echo "$message_mileagev"; } } //ELSE: IF THE A VALUE IN THE DROPDOWN IS NOT SELECTED else { $mileageudropdowncheck = FALSE; $message_mileageudropdowncheck = " *Please ensure you have choosen Miles or Kilometers from the list provided"; //echo "$message_mileageudropdowncheck"; } ?> The error messages are called on beneath the relative fields as follows: <?php if (isset($message_mileageudropdowncheck)) echo $message_mileageudropdowncheck; ?> <?php if (isset($message_mileagev)) echo $message_mileagev; ?> <?php if (isset($message_mileagevnumericcheck)) echo $message_mileagevnumericcheck; ?> One thing I'm uncertain of is why I was supposed to remove the following two lines: $mileagem = mysql_real_escape_string($_POST['mileagem']); $mileagekm = mysql_real_escape_string($_POST['mileagekm']); I'm going to make an educated leap and assume that this was a typo and that you were instead referring to the removal of && $mileagem && $mileagemnumericcheck && $mileagekm && $mileagekmnumericcheck from the condition that checks that all variables are defined as TRUE before inserting the user input and uploading the image files. Two final questions before I close this thread off: [*]Why is it important to include an error message if the dropdown is blank? Will it not always have an option selected by default or will it vary from browser to browser? [*]I know from another thread that type="upload" fields will reset (blank) each time the form refreshes after a failed process (whenever an error is encountered). Does this hold true with dropdown menus (resets to first option with each refresh)? Thanks so much again! Quote Link to comment https://forums.phpfreaks.com/topic/133831-solved-three-forms-on-one-page-clearing-each-others-input/#findComment-697385 Share on other sites More sharing options...
wildteen88 Posted November 24, 2008 Share Posted November 24, 2008 One thing I'm uncertain of is why I was supposed to remove the following two lines: $mileagem = mysql_real_escape_string($_POST['mileagem']); $mileagekm = mysql_real_escape_string($_POST['mileagekm']); Because mysql_real_escape_string should only used on strings and nothing else. $mileagem and $milagekm only contain numbers and nothing else. No SQL Injection can be performed from just numbers. I'm going to make an educated leap and assume that this was a typo and that you were instead referring to the removal of && $mileagem && $mileagemnumericcheck && $mileagekm && $mileagekmnumericcheck from the condition that checks that all variables are defined as TRUE before inserting the user input and uploading the image files. Umm, I didn't see those lines. For now just change those to your new variable names. Two final questions before I close this thread off: [*]Why is it important to include an error message if the dropdown is blank? Will it not always have an option selected by default or will it vary from browser to browser? [*]I know from another thread that type="upload" fields will reset (blank) each time the form refreshes after a failed process (whenever an error is encountered). Does this hold true with dropdown menus (resets to first option with each refresh)? 1. if you want you can remove that error message. I was using that error for debugging purposes. 2. My code should automatically select the option chosen from the list when the from is submitted. However looking back at my code I had a typo. $_POST['milageu'] should of been $_POST['mileageu'] in the following snippet: $selected = (isset($_POST['milageu']) && $_POST['milageu'] == $key) ? ' selected="selected"' : null; Quote Link to comment https://forums.phpfreaks.com/topic/133831-solved-three-forms-on-one-page-clearing-each-others-input/#findComment-697934 Share on other sites More sharing options...
webmaster1 Posted November 24, 2008 Author Share Posted November 24, 2008 Thanks again. Fixed the typo (oddly enough it didn't cause an issue either way). Quote Link to comment https://forums.phpfreaks.com/topic/133831-solved-three-forms-on-one-page-clearing-each-others-input/#findComment-697936 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.