while loop doesn't work properly,why? - php

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>

Related

php .how to make make L1 statement ( if ) work?

<?php
$score=0;
$i=0;
function icremm($i) {
$Question1=array( "answer1aa","answer1b","answer1c","answer1d");
$Question2=array( "answer2a","answer2bb","answer2c","answer2d");
$Question3=array( "answer3a","answer3b","answer3c","answer3dd ");
$allquest=(array($Question1,$Question2,$Question3));
$rand1=$allquest[$i];
?>
<form method="POST">
<input type="submit" name="1b" value="<?php echo $rand1[0] ?>"/>
<input type="submit" name="2b" value="<?php echo $rand1[1] ?>" />
<input type="submit" name="3b" value=" <?php echo $rand1[2] ?>" />
<input type="submit" name="4b" value="<?php echo $rand1[3] ?>" />
</form>
<?php
echo "<br>";
global $score;
echo $score;
echo "<br>";
echo "<br>";
global $i;
echo $i;
}
icremm($i);
if (isset($_POST["1b"]) ) {
$score+=5;
$i++;
goto L1;
}
if( $i==0 && isset($_POST["2b"]) || $i==0 && isset($_POST["3b"]) ||$i==0 && isset($_POST["4b"]) ) {
header("Location: lost.php");
}
L1:{
#ob_end_clean();
icremm($i);
if (isset($_POST["2b"]) ) {
$score+=5;
$i++;
#ob_end_clean();
echo"you won";
}
}
?>
i try to make my first game like "who wants to be a millionaire" and when i answer first question and go to L1 the
first statement(//if( $i==0 && isset($_POST["2b"]) || $i==0 && //isset($_POST["3b"]) ||$i==0 && isset($_POST["4b"]) ) {
// header("Location: lost.php");) still works
but i want to L1 statement to work

Is it because of if statement?

this code won't echo out,why??
if (isset( $_post['text']) &&isset( $_post['search for']) && isset($_post['Replace'])){
echo $text= $_post['text'];
echo $replace= $_post['replace'];
echo $search= $_post['searchfor'];
echo $text;
}
?>
<form action='index.php' method ='get'>
<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're trying to combine$_POST with a form element whose method is set as GET.
Either change your form element's method attribute to POST, or change $_POST to $_GET.
Solution 1
<form action='index.php' method='post'>
if (isset( $_POST['text']) && isset( $_POST['search for']) && isset($_POST['replace']))
Solution 2
<form action='index.php' method='get'>
if (isset( $_GET['text']) && isset( $_GET['search for']) && isset($_GET['replace']))
There is a space in $_post['search for'] but you ask later on the code for $_post['searchfor']
Please use $_POST instead of $_post
field name should not have spaces in it. so use search_for instead
of search for
if (isset( $_POST['text']) && isset( $_POST['search_for']) && isset($_POST['replace'])){
echo $text= $_POST['text'];
echo $replace= $_POST['replace'];
echo $search= $_POST['search_for'];
}
<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>
Your name in the input box and in php is different this is solution for you
if (isset( $_POST['text']) && isset( $_POST['search for']) && isset($_POST['replace'])){
echo $text= $_POST['text'];
echo $replace= $_POST['replace'];
echo $search= $_POST['search for'];
}
<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'>
you can also do this by Get method
You should use $_POST instead of $_post . And in form you are specifying method as get, please replace it with post.
The final code is..
if (isset( $_POST['text']) &&isset( $_POST['search for']) && isset($_POST['Replace'])){
echo $text= $_post['text'];
echo $replace= $_post['replace'];
echo $search= $_post['search for'];
echo $text;
}
?>
<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>

checkbox handling php multiple checkbox

Working on a simple php code. When it press on only PH it show hello, and only on chlorine it show yello. When both is pressed it show sello.
<?php
if(isset($_POST['submit'])){
foreach($_POST['verdi'] as $animal){
if(isset($_POST['verdi[]==PH']))
{
echo "hello";
}
}
}
?>
<form name="input" action="" method="POST">
<input type="checkbox" name="verdi[]" value="PH">PH<br>
<input type="checkbox" name="verdi[]" value="Chlorine">Chlorine<br>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
You can do a simple check in PHP:
if( in_array("PH", $_POST["verdi"]) ){
echo "in array!";
}
if(isset($_POST['submit']) && is_array($_POST['verdi'])) {
$checked = array();
foreach($_POST['verdi'] as $animal) {
// you can do extra validation here.
$checked[] = $animal;
}
if(in_array("PH", $checked) && in_array("Chlorine", $checked)) {
echo "sello";
} else {
if(in_array("PH", $checked)) {
echo "hello";
} else if(in_array("Chlorine", $checked)) {
echo "yello";
}
}
}

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>

Dynamic Error Display

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/>' : ''); ?>

Categories