Jump to content

PHP Return Original Input After Validation Error


thunderousity

Recommended Posts

I have a drop down list which I want to be able to remember and return the original inputted value even after there is a PHP validation error. Below is the code I have - please feel free to offer some simpler looking code too as it looks a bit ugly. <select name="TestID" id="TestID"> <?phpdo { ?> <option value="<?php echo $row_rstTest['TestID']?>"<?php if (!(strcmp($row_rstTest['TestID'], 1))) {echo "SELECTED";} ?>><?php echo $row_rstTest['Test']?></option> <?php} while ($row_rstTest = mysql_fetch_assoc($rstTest)); $rows = mysql_num_rows($rstTest); if($rows > 0) { mysql_data_seek($rstTest, 0); $row_rstTest = mysql_fetch_assoc($rstTest); }?> </select>

Link to comment
Share on other sites

What kind of error might you get? You shouldn't release code that might produce any kind of error. EDIT: Are you talking about how best to debug your code? If so, what error are you getting?

Edited by niche
Link to comment
Share on other sites

This is for my own content management system. The inputting form is validated by php (no javascript validation), thus when I input something incorrectly or omit a value I get the error messages I expect through my php sanity checking scripts. What I'm interested in is keeping the values I have input after I receive the validation errors on submission so that I don't have to re-input them all again.

Link to comment
Share on other sites

Just off the top of my head, I'd save the error by redirecting it using radio buttons (kind of like a switching process on a model train layout). Using a two switch layout, you can redirect an error to four possible locations. Three switches, eight locations. Once saved, or temporarily stored, you can move the data any where you want using those radios as switches. Edit: Obviously, a long radio button will do the same thing. I got a little carried away thinking about trains.

Edited by niche
Link to comment
Share on other sites

check inside the loop if the value of option $row_rstTest['TestID'] matches $_POST['Test_Id'] append "selected=selected".for text box you can echo the value of $_POST field in input element's value attribute. if that partciular field is submited or exist prints the value else left it empty.

Link to comment
Share on other sites

This seems to work - The else section allocates a value of my choice when the page first loads,If I submit the page and the page reloads as part of my input validation error then the first part of the if statement is executed and returns the posted value as selected one this time: <form action="<?php echo $editFormAction; ?>" name="addnewtitle" id="addnewtitle" method="POST"> <table width="100%" border="1" bordercolor="#CCCCCC"> <tr> <td bgcolor="#CCCCCC"><h3 align="left">Title</h3></td> <td><h3 align="left"> <select name="titleid" id="titleid"> <?phpdo { ?> <option value="<?php echo $row_rsttitle['titleid']?>" <?php if (isset($_POST['titleid'])) { if (!(strcmp($row_rsttitle['titleid'], $_POST['titleid']))) {echo "SELECTED";} } else { if (!(strcmp($row_rsttitle['titleid'], 1))) {echo "SELECTED";} } ?>> <?php echo $row_rsttitle['title']?></option> <?php} while ($row_rsttitle = mysql_fetch_assoc($rsttitle)); $rows = mysql_num_rows($rsttitle); if($rows > 0) { mysql_data_seek($rsttitle, 0); $row_rsttitle = mysql_fetch_assoc($rsttitle); }?> </select>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...