Jump to content

[SOLVED] validate form field filled


jnerotrix

Recommended Posts

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
}
?>

Link to comment
https://forums.phpfreaks.com/topic/135946-solved-validate-form-field-filled/
Share on other sites

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'];

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.