Dynamic Error Display - php

Please is there a way i can make my error display dynamic for only the fields that have errors display under each textbox.
The I normally use displays only one error for all of the fields when one field is empty...
Thanks
if (isset($_POST['submit'])) {
$a = mysql_real_escape_string($_POST ['a']);
$b = mysql_real_escape_string($_POST ['b']);
$c = mysql_real_escape_string($_POST ['c']);
$d = mysql_real_escape_string($_POST ['d']);
$e = mysql_real_escape_string($_POST ['e']);
if (($a == "") || ($b == "") || ($c == "") || ($d == "") || ($e == "")) {
echo "<div id=\"content\" >" ;
echo "<div class=\"error\" >" ;
echo "empty";
echo "</div>";
echo "</div>";
} else {
$query = "INSERT INTO user (a, b, c, d, e)
VALUES ($a, $b, $c, $d, $e)";
mysql_query($query);
}
}
?>
Enter Texts <br/>
<form action="<?php echo $_SERVER ['PHP_SELF']; ?>" method="post">
A:<input type="text" name="a" ><br/>
B:<input type="text" name="b" ><br/>
C:<input type="text" name="c" ><br/>
D:<input type="text" name="d" ><br/>
E:<input type="text" name="e" ><br/>
<input type="submit" name ="submit" value="Go"/>
</form>
thank you.

Here is an alternative way todo it, checking and building upon an error array as you go, then if the error array is empty do query.
<?php
//Check form was posted
if($_SERVER['REQUEST_METHOD']=='POST'){
//Pre build allowed array
$allowed=array('a','b','c','d','e','submit');
//Pre build errors array
$errors=array('A was not set',
'B was not set',
'C was not set',
'D was not set',
'E was not set');
//Create Blank error array
$error=array();
//Loop through the POST
foreach($_POST as $key=>$value){
//If key is in allowed array
if(in_array($key,$allowed)){
//Check its at good length
if(strlen(trim($value)) >= 1){
//Assign variable variable the key and value + escape
$$key = mysql_real_escape_string(trim($value));
}else{
//Assign key/value null
$$key = null;
//Assign the error from the errors array to the output error array
$error[$key] = $errors[array_search($key,$allowed)];
}
}else{
$error=array('Rouge POST key');
}
}
//If all is good do query
if(empty($error)){
$query = "INSERT INTO user (a, b, c, d, e)
VALUES ($a, $b, $c, $d, $e)";
mysql_query($query);
}
}?>
Enter Texts <br/>
<form action="" method="post">
A:<input type="text" name="a" ><?php echo (isset($error['a'])?$error['a']:null)?><br/>
B:<input type="text" name="b" ><?php echo (isset($error['b'])?$error['b']:null)?><br/>
C:<input type="text" name="c" ><?php echo (isset($error['c'])?$error['c']:null)?><br/>
D:<input type="text" name="d" ><?php echo (isset($error['d'])?$error['d']:null)?><br/>
E:<input type="text" name="e" ><?php echo (isset($error['e'])?$error['e']:null)?><br/>
<input type="submit" name="submit" value="Go"/>
</form>

Instead of showing error at top, attach following piece of code with each field. It will not show error on form load if your form and action code is on same page.
Try something like this:
A:<input type="text" name="a" ><br />
<?php if( isset($_POST['a']) && trim($_POST['a']) == '' ) { echo 'This field is required'; } ?>
B:<input type="text" name="b" ><br/>
<?php if( isset($_POST['b']) && trim($_POST['b']) == '' ) { echo 'This field is required'; } ?>
C:<input type="text" name="c" ><br/>
<?php if( isset($_POST['c']) && trim($_POST['c']) == '' ) { echo 'This field is required'; } ?>
D:<input type="text" name="d" ><br/>
<?php if( isset($_POST['d']) && trim($_POST['d']) == '' ) { echo 'This field is required'; } ?>
E:<input type="text" name="e" ><br/>
<?php if( isset($_POST['a']) && trim($_POST['a']) == '' ) { echo 'This field is required'; } ?>

Instead of checking all variables at once in ($a == "") || ($b == "") || ($c == "") || ($d == "") || ($e == "") check them one at a time and set individual error variables. Use these to display error messages close to your input field.
For example:
if ( $a == "" ) { $errorA = true; }
A:<input type="text" name="a" ><br/>
<?php print ($errorA ? '<span class="error">A is empty.</span><br/>' : ''); ?>
To prevent the form from being submitted, you could rather use following lines:
$error['A'] = $error['B'] = $error['C'] = .... = $error['Z'] = 0;
if ( $a == "" ) { $error['A'] = 1; }
if ( array_sum($error) > 0 ) {
// do not submit!
}
A:<input type="text" name="a" ><br/>
<?php print ($error['A'] ? '<span class="error">A is empty.</span><br/>' : ''); ?>
Or you simply set a broader error variable like this:
$error = false;
if ( $a == "" ) { $errorA = true; $error = true; }
if ( $b == "" ) { $errorB = true; $error = true; }
if ( !$error ) { //submit form
}
A:<input type="text" name="a" ><br/>
<?php print ($errorA ? '<span class="error">A is empty.</span><br/>' : ''); ?>

Related

How to give a value to a checkbox if it is not selected - php

In my form I have two checkboxes
-role
-commercial
I implemented an if loop that sees that the checkbox called role must be used
the second "commercial" checkbox is optional but how can I pass the "N" value when it is not selected?
Because in the current state in the db the commercial field, if its checkbox is not selected and I send the form, it gives me a null result
code:
<?php
session_start();
if(isset($_SESSION['USER_ID']))
{
if ($_SESSION['RUOLO'] == 'N' )
{
if (isset($_POST['submit']))
{
include 'FILE_DI_CONNESSIONE.php';
$id = $VARIABILE_FILE_DI_CONNESSIONE->real_escape_string($_POST['id']);
$role = $VARIABILE_FILE_DI_CONNESSIONE->real_escape_string($_POST['role']);
$commerciali = $VARIABILE_FILE_DI_CONNESSIONE->real_escape_string($_POST['commerciali']);
$query = mysqli_query($VARIABILE_FILE_DI_CONNESSIONE, "UPDATE tabella SET role='$role', commerciali='$commerciali' WHERE id = ".$_SESSION['USER_ID']);
if( $query )
{
if(isset($_POST['role']))
{
$role = ($_POST['role']);
echo "ok";
}
else
{
echo "errore";
}
}
}
}
}
?>
<form method="post" action="n.php">
<input class="form-control" name="id" value="<?php echo $_SESSION['USER_ID']; ?>"><br>
<label>Privacy</label>
<input class="form-control" type="checkbox" value="S" required name="role" placeholder="<?php echo $_SESSION['RUOLO']; ?>"><br>
<label>Termini e Condizioni</label>
<input class="form-control" type="checkbox" value="S" name="commerciali" placeholder="<?php echo $_SESSION['RUOLO']; ?>"><br>
<input class="btn btn-primary" name="submit" type="submit" value="Register..."><br>
</form>
maybe this is what you are looking for?
if(array_key_exists('submit', $_POST) {
if(isset($_SESSION['USER_ID'] && $_SESSION['RUOLO'] == 'N') {
include 'FILE_DI_CONNESSIONE.php';
$id = $VARIABILE_FILE_DI_CONNESSIONE->real_escape_string($_POST['id']);
$role = $VARIABILE_FILE_DI_CONNESSIONE->real_escape_string($_POST['role']);
if(!isset($_POST['commerciali'])) {
$commerciali = "N";
} else {
$commerciali = $VARIABILE_FILE_DI_CONNESSIONE->real_escape_string($_POST['commerciali']);
}
$query = "UPDATE `tabella` SET `role`='$role', `commerciali`='$commerciali' WHERE `id` = $id";
$result = mysqli_query($VARIABILE_FILE_DI_CONNESSIONE, $query);
if($result && isset($_POST['role'])) {
$role = ($_POST['role']);
echo "ok";
} else {
echo "error";
}
}
the if(array_key_exists('submit',$_POST) { helps check that unless the form's submit button is clicked and sent with the form's post array.. nothing should run.. personally find more secure cause of bots that are capable of submitting forms with their own data.. also takes care of the if statement for submit check.
And answering your question is the if(!isset($_POST['commerciali'])) {part.
I hope this helps!

Error: strpos() [function.strpos]: Empty delimiter

I got this littel problem here with this code
<form action="index.php" method="post">
1<input type="text" name="A" <br>
2<input type="text" name="B" <br>
3<input type="text" name="C" <br>
<input type="submit" value="Submit"><br>
</form>
and PHP
<?php
$a = $_POST['A']; $b = $_POST['B']; $c = $_POST['C'];
if(!isset($a , $b) || trim($a , $b ) == '' )
{
echo "empty" ;
}
$szukaj = strpos($a,$b );
if ($szukaj === false ) {
echo 'hehe ';
} else {
echo 'hihi ';
}
?>
Icant deal with this error... Can somebody give some tip ,or advice... what i do wrong?
In your case you should do like this:
<?php
$a = $_POST['A']; $b = $_POST['B']; $c = $_POST['C'];
if(!empty($a) || empty($b) || trim($a) == '' || trim($b) == '') {
echo "empty" ;
} else {
$szukaj = strpos($a, $b);
if ($szukaj === false ) {
echo 'hehe ';
} else {
echo 'hihi ';
}
}
?>
Your checking for empty were a bit off, and you have to use an else on the empty conditions to only run the strpos if they aren't empty.
I've also placed the other if in the else to be sure the $szukaj variable is set.
your html is wrong, I've tidied it up a bit for you
<form action="index.php" method="post">
1<input type="text" name="A" /><br />
2<input type="text" name="B" /><br />
3<input type="text" name="C" /><br />
<input type="submit" value="Submit" /><br />
</form>

while loop doesn't work properly,why?

It keeps (echo)ing sth.
<?php
$offset=0;
if (isset( $_POST['text']) && isset( $_POST['search_for']) && isset($_POST['replace'])){
$text= $_POST['text'];
$replace= $_POST['replace'];
$search= $_POST['search_for'];
$string_length=strlen($search);
if (!empty ($text) && !empty($search) && !empty($replace)){
while ($strpos=strpos($text,$search,$offset))
echo $strpos.'<br>';
echo $offset=$strpos+$string_length.'<br>';
} else {
echo 'please fill all fields';
}
}
?>
<form action='index.php' method ='POST'>
<textarea name='text' rows=6 cols=30 > </textarea><br><br>
Search for:<br>
<input type ='text' name='search_for'><br><br>
Replace with:<br>
<input type='text' name='replace'><br><br>
<input type='submit' value='Find & Replace'>
</form>
You forgot brackets around the while body:
if (!empty ($text) && !empty($search) && !empty($replace)) { // here
while ($strpos=strpos($text,$search,$offset)) {
echo $strpos.'<br>';
echo $offset=$strpos+$string_length.'<br>';
} // here
} else {
Without these brackets just first command is executed (echo $strpos) during the loop and after the loop the second echo is written.
Your code was the same as:
if (!empty ($text) && !empty($search) && !empty($replace)) { // here
while ($strpos=strpos($text,$search,$offset)) {
echo $strpos.'<br>';
} // here the while loop ends
echo $offset=$strpos+$string_length.'<br>';
} else {
You did a mistake { :
while ($strpos=strpos($text,$search,$offset)) {
So :
<?php
$offset=0;
if (isset( $_POST['text']) && isset( $_POST['search_for']) && isset($_POST['replace'])){
$text= $_POST['text'];
$replace= $_POST['replace'];
$search= $_POST['search_for'];
$string_length=strlen($search);
if (!empty ($text) && !empty($search) && !empty($replace)){
while ($strpos=strpos($text,$search,$offset)) { // You forgetten the {
echo $strpos.'<br>';
echo $offset=$strpos+$string_length.'<br>';
} else {
echo 'please fill all fields';
}
}
?>
<form action='index.php' method ='POST'>
<textarea name='text' rows=6 cols=30 > </textarea><br><br>
Search for:<br>
<input type ='text' name='search_for'><br><br>
Replace with:<br>
<input type='text' name='replace'><br><br>
<input type='submit' value='Find & Replace'>
</form>

php word check if they are the same

I need that they check my word witch i written in text field. If 2 words the same i should get message - "They are the same".
<?php
function array_random($arr, $num = 1) {
shuffle($arr);
$r = array();
for ($i = 0; $i < $num; $i++) {
$r[] = $arr[$i];
}
return $num == 1 ? $r[0] : $r;
}
$a = array("snow", "ball", "side");
//print_r(array_random($a));
//print_r(array_random($a, 3));
?>
<form name='form' method='post' align = "center">
<?php
$zod = (array_random($a)); echo $zod; ?> <input type="text" name="name" id="name" ><?php
if((isset($_POST['name'])) && !empty($_POST['name']))
{
$name = $_POST['name'];
echo ' '.$name;
}
?><br/><br>
<input name="select" type="submit" onclick="select()" value="select" />
</form>
A simple comparison uses the '==' operator. These can be used to compare something. You want to compare a posted value with the one from your array. which is random:
<?php
function array_random($arr, $num = 1) {
shuffle($arr);
$r = array();
for ($i = 0; $i < $num; $i++) {
$r[] = $arr[$i];
}
return $num == 1 ? $r[0] : $r;
}
if ( isset ( $_POST['submit'] ) && isset ( $_POST['name'] ) ) {
if ( $_POST['compare'] == $_POST['name'] )
echo "They are the same, hurray!<br />";
else
echo "These don't match!";
}
$a = array ( "snow", "ball", "side" );
$rand = array_random ( $a );
?>
<form name='form' method='post' align = "center">
Please type the word: <?php echo $rand; ?>
<input type="text" name="name" id="name" >
<input type="hidden" name="compare" id="compare" value="<?php echo $rand ?>" >
<input name="submit" type="submit" onclick="select()" value="select" />
</form>
As you wanted, a random value is picked and compared to what the user posted.

creating a find and replace application in php

i have a php code :
<?php
$offset=0;
if(isset($_POST['text']) && isset($_POST['searchfor']) && isset($_POST['replacewith']))
{
$text=$_POST['text'];
$search=$_POST['searchfor'];
$replace=$_POST['replacewith'];
$search_length=strlen($search);
if(!empty($text)&&!empty($search)&&!empty($replace))
{
while($strpos=strpos($text,$search,$offset))
{
$offset=$strpos + $search_length;
$text=substr_replace($text,$replace,$strpos,$search_length);
}
echo $text;
}
else
{
echo 'pls fill in all fields';
}
}
?>
<form action='string_search.php' method='POST'>
<textarea name='text' rows='6' cols='30'></textarea><br><br>
Search For:
<input type='text' name='searchfor'><br><br>
Replace with:
<input type='text' name='replacewith'><br><br>
<input type='submit' value='find and replace'>
</form>
When i type a string.Forexample 'i found my dog' in textarea and search for 'dog' in 'search for' then replace with 'cat' in 'replace' then submit it will out put 'i found my cat'.However it always output the original string ('i found my dog').I don't know why ?
Why do you use:
while($strpos=strpos($text,$search,$offset)) {
$offset=$strpos + $search_length;
$text=substr_replace($text,$replace,$strpos,$search_length);
}
echo $text;
??
I think its the same if you just use:
$strpos=strpos($text,$search);
$text=substr_replace($text,$replace,$strpos,$search_length);
echo $text;
And probably is your while who is doing the bad work. I don't know, it's a suggestion. With this, your code should work.
<?php
$offset = 0;
if (isset($_POST['user_input_box']) && isset($_POST['search']) && isset($_POST['replace'])) {
$user_input_box = $_POST['user_input_box'];
$search = $_POST['search'];
$replace =$_POST['replace'];
//To check whether above code work or not
// echo $user_input_box = $_POST['user_input_box'];
// echo $search = $_POST['search'];
// echo $replace = $_POST['replace'];
$search_length = strlen($search);
if (!empty($_POST['user_input_box']) && !empty($_POST['search']) && !empty($_POST['replace'])) {
while ($string_position=strpos($user_input_box, $search ,$offset)) {
// to check while and strpos
// echo $string_position."<br>";
// echo $offset = $string_position + $search_length;
$offset = $string_position + $search_length;
$user_input_box = substr_replace($user_input_box, $replace,$string_position,$search_length );
}
echo $user_input_box;
}
else{
echo "Please fill the complete fields.";
}
}
?>
<form method="POST" action="eleven0.php">
<textarea cols="34" rows="12" name="user_input_box"></textarea>
<br><br>
<label>Search For: </label> <br>
<input type="text" name="search"><br><br>
<label>Replace With: </label><br>
<input type="text" name="replace"><br><br>
<input type="submit" value="Find and Replace">
</form>

Categories