hi everyone, i have an HTML form with 4 textboxes. the first textbox is named Trans_Id. after submitting the form, before writing it into the database, i have to check first for matching records of Trans_Id. if found, i have to go back to the HTML form and inform the user that the ID he is trying to insert already exists. now, the problem i am having is, the HTML page is refreshed and all the values of the 4 textboxes are gone! my codes goes something like: <?php if ($ResultSet) { echo("<script language = \"javascript\" type = \"text/javascript\">"); echo("window.history.back()"); echo("</script>"); ?> this doesn't work...urgent help please, thanks in advance... regards, -hayunna
Webpages do not ordinarily retain information when you use the cache, some browsers do it, but it's not something you can rely on. Please post your entire code so that I might be able to help you .....
Hey hayunna, You're using javascript in this case. i'ld remove that part; contrary I'ld change it with this: <?php if ($ResultSet) { echo("<script language = \"javascript\" type = \"text/javascript\">"); echo("alert('Your ID already exists. Please choose another')"); echo("</script>"); ?> PHP: Make sure below you add the whole form again, and fill the values of the textboxes with the posted php variables Kireg
thanks for the very quick reply. DeViAnThans3, what your code does is to alert the user that a duplicate entry exists. yes, i am using javascript, but i would be grateful if you could suggest a better way. i am just a newbie. krakjoe, my codes are a little bit long. please take time to read: <form name = "CV" method = "POST" action = "cv_events.php" onsubmit = "return Validate()" > <table border = "1" cellspacing = "0" cellpadding = "0" style = "width:350px"> <tr> <td class = "TrHeader" colspan = "2"> Check Voucher </td> </tr> <tr> <td style = "width:25%"> CV Number </td> <td style = "width:75%"> <input type = "text" name = "CV_No" class = "InputChar" style = "width:100px" maxlength = "10" autocomplete = off /> </td> </tr> <tr> <td> CV Date </td> <td> <input type = "text" name = "CVDate" class = "InputDate" maxlength = "10" autocomplete = off> </td> </tr> <tr> <td> Payee ID </td> <td> <input type = "text" name = "PayeeCode" class = "InputChar" style = "width:100px" maxlength = "10" autocomplete = off> <input name = "btnUpdate" type = "button" value = "Pick" class = "InputButton" onclick = "PickPayee()"> </td> </tr> <tr> <td> Amount </td> <td> <input type = "text" name = "Amount" class = "InputNumeric" maxlength = "15" autocomplete = off> </td> </tr> <tr> <td> Check Number </td> <td> <input type = "text" name = "CheckNo" class = "InputChar" style = "width:100px" maxlength = "20" autocomplete = off> </td> </tr> <tr> <td> Check Date </td> <td> <input type = "text" name = "CheckDate" class = "InputDate" maxlength = "10" autocomplete = off> </td> </tr> <tr> <td> Date Released </td> <td> <input type = "text" name = "DateReleased" class = "InputDate" maxlength = "10" autocomplete = off> </td> </tr> <tr> <td> Bank </td> <td> <select name = "Bank_Id" style = "width:95%"> <option value = ""> (Select one) </option> [PHP]<?php if ($db->SqlConnect()); { $query = $db->SelectSql("Bank_Id, Name", "banks", "", "Name"); $db->AddListItem("Name", "Banks_Id"); $db->Close(); } ?>[/PHP] </select> </td> </tr> <tr> <td colspan = "2"> Remarks <br /> <textarea name = "Remarks" rows = "3" style = "width:97%"></textarea> </td> </tr> <tr> <td align = "right" colspan = "2"> <input name = "btnUpdate" type = "submit" value = "Update" class = "InputButton"> </td> </tr> </table> <input type = "hidden" name = "Payee_Id" value = "" /> </form> HTML: cv_events.php: <?php { echo("<script> window.history.go(-1); </script>"); } PHP:
I'm confused, the code you posted isn't as you described in your original post, where is Trans_Id in the form ?
oops...sorry, i just edited cv_events.php a few moments ago, but this is the original file: <?php define('RootPath', '../'); include(RootPath . "default.php"); $clsGet = new clsGet; $db = new DBTasks; $js = new clsJs; $cv_no = ToSqlString($clsGet->HtmlPost("CV_No")); if ($db->SqlConnect()) { $query = $db->SelectSql("CV_No", "cv", "cv_no = $cv_no", ""); if ($db->ResultSet) { echo("<script language = \"javascript\" type = \"text/javascript\">"); echo("window.history.back()"); echo("</script>"); return false; } $db->Close(); } ?> PHP:
<?php if( $_POST ) { define('RootPath', '../'); include(RootPath . "default.php"); $clsGet = new clsGet; $db = new DBTasks; $js = new clsJs; $cv_no = ToSqlString($clsGet->HtmlPost("CV_No")); if ($db->SqlConnect()) { $query = $db->SelectSql("CV_No", "cv", "cv_no = $cv_no", ""); if ($db->ResultSet) { printf("Warning : %s exists in the databse<br />", $cv_no ); } else { // your code here to take a successfull submission of the form, I dunno what that is } $db->Close(); } } ?> <form name = "CV" method = "POST" action = "" onsubmit = "return Validate()" > <table border = "1" cellspacing = "0" cellpadding = "0" style = "width:350px"> <tr> <td class = "TrHeader" colspan = "2"> Check Voucher </td> </tr> <tr> <td style = "width:25%"> CV Number </td> <td style = "width:75%"> <input type = "text" name = "CV_No" class = "InputChar" style = "width:100px" maxlength = "10" autocomplete = off value="<?=$_POST['CV_No'] ?>"/> </td> </tr> <tr> <td> CV Date </td> <td> <input type = "text" name = "CVDate" class = "InputDate" maxlength = "10" autocomplete = off value="<?=$_POST['CVDate'] ?>"> </td> </tr> <tr> <td> Payee ID </td> <td> <input type = "text" name = "PayeeCode" class = "InputChar" style = "width:100px" maxlength = "10" autocomplete = off value="<?=$_POST['PayeeCode'] ?>"> <input name = "btnUpdate" type = "button" value = "Pick" class = "InputButton" onclick = "PickPayee()"> </td> </tr> <tr> <td> Amount </td> <td> <input type = "text" name = "Amount" class = "InputNumeric" maxlength = "15" autocomplete = off value="<?=$_POST['Amount'] ?>"> </td> </tr> <tr> <td> Check Number </td> <td> <input type = "text" name = "CheckNo" class = "InputChar" style = "width:100px" maxlength = "20" autocomplete = off value="<?=$_POST['CheckNo'] ?>"> </td> </tr> <tr> <td> Check Date </td> <td> <input type = "text" name = "CheckDate" class = "InputDate" maxlength = "10" autocomplete = off value="<?=$_POST['CheckDate'] ?>"> </td> </tr> <tr> <td> Date Released </td> <td> <input type = "text" name = "DateReleased" class = "InputDate" maxlength = "10" autocomplete = off value="<?=$_POST['DateReleased'] ?>"> </td> </tr> <tr> <td> Bank </td> <td> <select name = "Bank_Id" style = "width:95%"> <option value = ""> (Select one) </option> <?php if ($db->SqlConnect()); { $query = $db->SelectSql("Bank_Id, Name", "banks", "", "Name"); $db->AddListItem("Name", "Banks_Id"); $db->Close(); } ?> </select> </td> </tr> <tr> <td colspan = "2"> Remarks <br /> <textarea name = "Remarks" rows = "3" style = "width:97%" value="<?=$_POST['Remarks'] ?>"></textarea> </td> </tr> <tr> <td align = "right" colspan = "2"> <input name = "btnUpdate" type = "submit" value = "Update" class = "InputButton"> </td> </tr> </table> <input type = "hidden" name = "Payee_Id" value = "" /> </form> PHP: It'll work better like that, then the form has been posted and you stop execution of the form because CV_No exists, you'll still have the posted values in the input boxes, dunno how to do the select box because it's non standard code.
by the way, there are two files, involved here. one is cv.php (the codes that i showed above which contains more HTML stuff) and cv_events.php (the one that triggers the SQL commands)...not only one PHP file do all the tasks.. ** just edited this post ** i found out that PHP has nothing to do about this problem. it's all about JavaScript window.open() function.. i am not sure at this point how and why it causes the problem like this. any suggestion?
I'm aware of that, but that's what you'll have to do to preserve the values in the form, theres no reason why the code shouldn't be one file.