jnerotrix Posted December 7, 2008 Share Posted December 7, 2008 I need a way for php to validate that the form is filled heres the code: <?php error_reporting(E_ALL); $hostname_php_result_conn = "localhost"; $database_php_result_conn = "sex1800_phpresults"; $username_php_result_conn = "sex1800_demo"; $password_php_result_conn = "demotable"; $php_result_conn = mysql_connect($hostname_php_result_conn, $username_php_result_conn, $password_php_result_conn); mysql_select_db($database_php_result_conn,$php_result_conn); if(isset($_POST['submit'])) { $roll_no = $_POST['roll_no']; $string = $roll_no; $replace = "0"; $result = preg_replace("/[^0-9s]/","$replace", $string); $roll_no = $result; $sql = mysql_query("SELECT * FROM result_table WHERE roll_no = $roll_no") or die (mysql_error()); $row1 = mysql_fetch_array($sql); $name = $row1['name']; $marks = $row1['marks']; if($row1 == 0) { echo 'Error, ID does not exist'; } else { echo 'Name = '.$name.'<br /> Marks = '.$marks; } } else { ?> <html> <head> <style type="text/css"> #header { width:100%; background:lightblue; margin:0px; padding:0px; } #leftbar { float:left; width:40%; height:30px; background:lightblue; text-align:right; } #rightbar { float:left; width:60%; height:30px; background:lightblue; text-align:center; } #bar { width:100%; background:lightblue; } #center {float:center; width:60%; height:10px; background:lightblue; align:center; } a { font-family:Arial; font-size:16px; background-color:aqua; color:blue; font-weight:bold; vertical-align:text-bottom; } a:hover { font-family:Lucida Calligraphy; font-size:ahoversize1; background-color:yellow; color:darkblue; } </style> </head> <center> <body> <br><Br><Br> <h1> Search Roll Number </h1> <Br><br> <div id="header"> <div id="center"> <table> <form action="search.php" method="post"> <tr> <td>Roll Number:</td><td><input name="roll_no" type="text" id="roll_no" /></td> </tr> <tr> <td align="center"><input type="submit" name="submit" value="search" /></td> </tr> </form> </table> </div> <div id="leftbar"> </div> <div id="rightbar"> </div> </div> <div id="bar"> </div> <div id="header"> <div id="leftbar"> </div> <div id="rightbar"> </div> </div> </body> </html> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/135946-solved-validate-form-field-filled/ Share on other sites More sharing options...
premiso Posted December 7, 2008 Share Posted December 7, 2008 Be more descritive next time. I take it you want to check that $roll_no actually contains a value? $roll_no = isset($_POST['roll_no'])?$_POST['roll_no']:null; if (!is_null($roll_no)) { // do processing here. } Quote Link to comment https://forums.phpfreaks.com/topic/135946-solved-validate-form-field-filled/#findComment-708649 Share on other sites More sharing options...
jnerotrix Posted December 7, 2008 Author Share Posted December 7, 2008 Doesnt Work did i add it in correctly <?php error_reporting(E_ALL); $hostname_php_result_conn = "localhost"; $database_php_result_conn = "sex1800_phpresults"; $username_php_result_conn = "sex1800_demo"; $password_php_result_conn = "demotable"; $php_result_conn = mysql_connect($hostname_php_result_conn, $username_php_result_conn, $password_php_result_conn); mysql_select_db($database_php_result_conn,$php_result_conn); if(isset($_POST['submit'])) { $roll_no = isset($_POST['roll_no'])?$_POST['roll_no']:null; if (!is_null($roll_no)) { $string = $roll_no; $replace = "0"; $result = preg_replace("/[^0-9]/","$replace", $string); $roll_no = $result; $sql = mysql_query("SELECT * FROM result_table WHERE roll_no = $roll_no") or die (mysql_error()); $row1 = mysql_fetch_array($sql); $name = $row1['name']; $marks = $row1['marks']; } Quote Link to comment https://forums.phpfreaks.com/topic/135946-solved-validate-form-field-filled/#findComment-708657 Share on other sites More sharing options...
premiso Posted December 7, 2008 Share Posted December 7, 2008 Maybe try this for the if statement: $roll_no = isset($_POST['roll_no'])?trim($_POST['roll_no']):null; if (!is_null($roll_no) && !empty($roll_no)) { And see if it still executes. Quote Link to comment https://forums.phpfreaks.com/topic/135946-solved-validate-form-field-filled/#findComment-708664 Share on other sites More sharing options...
jnerotrix Posted December 7, 2008 Author Share Posted December 7, 2008 Prefect Thanks Premo Quote Link to comment https://forums.phpfreaks.com/topic/135946-solved-validate-form-field-filled/#findComment-708668 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.