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

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>

Related

how to use the single input received multiple times in php

for example, I receive two inputs value1 and value2 and I want this input for different functions. Like addition, subtraction and multiplication.
my code
<?php
$x = $_POST['fnum'];
$y = $_POST['snum'];
if (isset($_POST['add'])) {
$sum = $x + $y;
echo "Result:<input type='text' value='$sum'/>";
}
if (isset($_POST['sub'])) {
$sub = $x - $y;
echo "Result:<input type='text' value='$sub'/>";
}
if (isset($_POST['mul'])) {
$mul = $x * $y;
echo "Result:<input type='text' value='$mul'/>";
}
<body>
<form method="post">
Enter first number <input type="text" name="fnum" />
<hr />
Enter second number <input type="text" name="snum" />
<hr />
<input type="submit" name="add" value="ADD" />
<input type="submit" name="sub" value="Subtract" />
<input type="submit" name="mul" value="Multiply" />
</form>
</body>
In this it is asking me to feed input for each operation separately
Good, Just use the posted value in your form input like -
Enter first number <input type="text" name="fnum" value="<?php echo #$_POST['fnum'];?>"/><hr/>
Enter second number <input type="text" name="snum" value="<?php echo #$_POST['snum'];?>"/><hr/>
So that user don't need to put the same value again and when press the other buttons the form will automatically submit with the previous values.
Note: Remember, you need to check all the posted value is set or not and use proper conditions of POST method. Have a look at the given example of your problem as solution, I give it here to give you a proper guide.
Example:
<?php
$x = 0;
$y = 0;
if(isset($_POST['submit'])) {
$x = $_POST['fnum'];
$y = $_POST['snum'];
$operator = "";
if($_POST['submit'] == 'ADD') {
$operator = "+";
} elseif($_POST['submit'] == 'Subtract') {
$operator = "-";
} elseif($_POST['submit'] == 'Multiply') {
$operator = "*";
}
$result = $x . $operator . $y;
}?>
Your form will be-
<form method="post">
Enter first number <input type="text" name="fnum" value="<?php echo $x;?>"/><hr/>
Enter second number <input type="text" name="snum" value="<?php echo $y;?>"/><hr/>
<input type="submit" name="submit" value="ADD"/>
<input type="submit" name="submit" value="Subtract"/>
<input type="submit" name="submit" value="Multiply"/>
</form>
Result:
<input type='text' value='<?php echo (isset($result) ? $result : "-";)?>'/>

How can I echo a statement from a form?

I'm a student doing a challenge where I would have to make a Calculator-type form where you can input two values and click an operation. It's a simple concept using only 4 operations.
I was able to make the format of the form: title, input text and buttons. But I can't find a way to take the input information and manipulating the information. My initial goal was to take the two values and adding/subtracting/multiplying/dividing the values and printing the statement above.
For example:
1 + 2 = 3 //statement printed above
First Value: 1
Second Value: 2 //insert values
+ - * / //click operation buttons
Any suggestions?
<!DOCTYPE HTML>
<?php
print_r($_POST);
if ( isset($_POST["+"]) ) {
$sum = $var1 + $var2;
echo "$var1 + $var2 = $sum";
}
elseif( isset($_POST["-"]) ) {
$sum = $var1 - $var2;
echo "$var1 - $var2 = $sum";
}
elseif ( isset($_POST["*"]) ) {
$sum = $var1 * $var2;
echo "$var1 * $var2 = $sum";
}
elseif ( isset($_POST["/"]) ) {
$sum = $var1 / $var2;
echo "$var1 / $var2 = $sum";
}
?>
<html>
<body>
<form action="" method="POST">
First Value: <input type="text" name="First Value"><br><br>
Second Value: <input type="text" name="Second Value"><br><br>
Operations: <button type="submit" name "+">+</button>
<button type="submit" name "-">-</button>
<button type="submit" name "*">*</button>
<button type="submit" name "/">/</button>
</form>
</body>
</html>
This seems to fix the errors I mentioned in comments
<!DOCTYPE HTML>
<html>
<body>
<?php
if ( $_SERVER['REQUEST_METHOD'] == 'POST'){
if ( isset($_POST["+"]) ) {
$sum = $_POST['var1'] + $_POST['var2'];
echo "$_POST[var1] + $_POST[var2] = $sum";
}
elseif( isset($_POST["-"]) ) {
$sum = $_POST['var1'] - $_POST['var2'];
echo "$_POST[var1] - $_POST[var2] = $sum";
}
elseif ( isset($_POST["*"]) ) {
$sum = $_POST['var1'] * $_POST['var2'];
echo "$_POST[var1] * $_POST[var2] = $sum";
}
elseif ( isset($_POST["/"]) ) {
$sum = $_POST['var1'] / $_POST['var2'];
echo "$_POST[var1] / $_POST[var2] = $sum";
}
}
?>
<form action="" method="POST">
First Value: <input type="text" name="var1"><br><br>
Second Value: <input type="text" name="var2"><br><br>
Operations: <button type="submit" name="+">+</button>
<button type="submit" name="-">-</button>
<button type="submit" name="*">*</button>
<button type="submit" name="/">/</button>
</form>
</body>
</html>
I don't know if it's the right way to do things but you can use jquery in the script section to do so, let's say you have this form
<form id="target" action="destination.html">
<input type="text" value="Hello there">
<input type="submit" value="Go">
</form>
so on the submit you do the math
$( "#target" ).submit(function( event ) {
//do the math//
document.write(//your answer//);
event.preventDefault();
});
<?php
$first_num = $_POST['first_num'];
$second_num = $_POST['second_num'];
$operator = $_POST['operator'];
$result = '';
if (is_numeric($first_num) && is_numeric($second_num)) {
switch ($operator) {
case "Add":
$result = $first_num + $second_num;
break;
case "Subtract":
$result = $first_num - $second_num;
break;
case "Multiply":
$result = $first_num * $second_num;
break;
case "Divide":
$result = $first_num / $second_num;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div id="page-wrap">
<h1>Simple Calculator Program</h1>
<form action="" method="post" id="quiz-form">
<p>
<label>First Number</label>
<input type="number" name="first_num" id="first_num" required="required" value="<?php echo $first_num; ?>" />
</p>
<p>
<label>Second Number</label>
<input type="number" name="second_num" id="second_num" required="required" value="<?php echo $second_num; ?>" />
</p>
<p>
<label>Result</label>
<input readonly="readonly" name="result" value="<?php echo $result; ?>">
</p>
<input type="submit" name="operator" value="Add" />
<input type="submit" name="operator" value="Subtract" />
<input type="submit" name="operator" value="Multiply" />
<input type="submit" name="operator" value="Divide" />
</form>
</div>
</body>
</html>

preg_match Error When Check HTML many inputs

Hello I want to verify that all the fields in the array $key in which the first letter is M, but there is a problem which is:
Warning: preg_match() expects parameter 2 to be string, array given in C:\AppServ\www\regx1.php on line 15
<?php
if (isset($_POST['zr'])){
$pattern = "/^m/";
$key = array($_POST['text'],$_POST['text1'],$_POST['text2'],$_POST['text3']);
if (preg_match($pattern,$key))
echo 'is Mathcing M is First!';
}else {
echo "M it's Not First!'";
}
?>
<form action="regx1.php" method="post">
<input type="submit" name="zr" />
<br />
<hr />
<input type="text" name="text" /><br />
<input type="text" name="text1" /><br />
<input type="text" name="text2" /><br />
<input type="text" name="text3" /><br />
</form>
.
<?php
if (isset($_POST['zr']))
{
$pattern = "/^m/";
$key = array($_POST['text'],$_POST['text1'],$_POST['text2'],$_POST['text3']);
foreach($key as $val)
{
if (preg_match($pattern,$val))
{
echo 'is Mathcing M is First!';
}
else
{
echo "M it's Not First!'";
}
}
or
<?php
if (isset($_POST['zr']))
{
$pattern = "/^m/";
$key = array($_POST['text'],$_POST['text1'],$_POST['text2'],$_POST['text3']);
$failed = false;
foreach($key as $val)
{
if (!preg_match($pattern,$val))
$failed = true;
}
}
if ($failed)
{
echo "M it's Not First!'";
}
else
{
echo 'is Mathcing M is First!';
}
?>

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

Post variables not being sent

This is really bugging me and I can't figure it out. I have a form with a few options being sent by POST:
<form method="POST" action="scripts/submit.php"><strong>
To User: <input type="text" name="ID" size="21" /><br />
Short Description: <input type="text" name="Item" size="21" /><br />
Link: <input type="text" name="Link" size="21" /><br />
Points: <select name="Points">
<option value="1" selected="selected">1</option>
<option value="0">0</option><option value="-1">-1</option>
</select> (1 = Positive, 0 = Neutral, -1 = Negative)<br />
Text: <br /><textarea name="Text" rows="5" cols="50"/></textarea></strong><br />
<input type="submit" value="Send" />
</form>
And here is the portion of submit.php that is giving me trouble:
<?php
include('functions.php');
Connect();
if(!isset($_SESSION))
{
session_start();
}
$id_from = $_SESSION['SESS_MEMBER_ID'];
$id_to = Sanitize($_POST['ID']);
$item = Sanitize($_POST['Item']);
$link = Sanitize($_POST['Link']);
$points= Sanitize($_POST['points']);
$text = Sanitize($_POST['Text']);
Does anyone see an issue here? I am getting undefined index's from all of the variables except the session one.
Thanks in advance.
edit: If i just have this:
<?php
include('functions.php');
Connect();
if(!isset($_SESSION))
{
session_start();
}
$id_from = $_SESSION['SESS_MEMBER_ID'];
$id_to = Sanitize($_POST['ID']);
$item = Sanitize($_POST['Item']);
$link = Sanitize($_POST['Link']);
$points = Sanitize($_POST['points']);
$text = Sanitize($_POST['Text']);
?>
The variables populate just fine. If I add:
$id_query=mysql_query("SELECT ID FROM tbl_users WHERE Username = '$id_to'");
$count=mysql_num_rows($id_query);
$id_row=mysql_fetch_array($id_query);
$id_to=$id_row['ID'];
if ($points> 1 || $points< -1) {
echo "Nice try";
exit();
} else {
if(!($id_to == $id_from))
{
if($count==1)
{
mysql_query("INSERT INTO tbl_data (Item, Link, Points, Text, ID_To, ID_From) VALUES ('$item', '$link', '$points', '$text', '$id_to','$id_from')");
header('Location:?id=submit');
}
else
{
echo "Nice try1";
}
}
else
{
echo "Nice try2";
}
}
I just took your code over at my dev server at tried to test run it. Since I don't know what your Sanitize() do, I can't be sure what is going on inside this function.
If you try to remove the Sanitize(), I'm pretty sure it would work and you will have to look inside this to find the bug.
I'm guessing you might be missing something like ($var, str) for sanitize a string. Can you please tell a little more about this function ?
edit: some minor spelling errors.
Edit: Did some more test and made the error happen and the two codes shows it. The 1st works, while the 2nd gives me a empty var_dump.
This one gives me a full var_dump();
<?
function Sanitize($String) {
$output = mysql_real_escape_string(stripslashes($String));
return $output;
}
if(!isset($_SESSION))
{
session_start();
}
?>
<form method="post" action=""><strong>
To User: <input type="text" name="ID" size="21" /><br />
Short Description: <input type="text" name="Item" size="21" /><br />
Link: <input type="text" name="Link" size="21" /><br />
Points: <select name="Points"><option value="1" selected="selected">1</option><option value="0">0</option><option value="-1">-1</option></select> (1 = Positive, 0 = Neutral, -1 = Negative)<br />
Text: <br /><textarea name="Text" rows="5" cols="50"/></textarea></strong><br />
<input type="submit" value="Send" />
</form>
<?
$id_from = $_SESSION['SESS_MEMBER_ID'];
$id_to = Sanitize($_POST['ID']);
$item = Sanitize($_POST['Item']);
$link = Sanitize($_POST['Link']);
$points= Sanitize($_POST['points']);
$text = Sanitize($_POST['Text']);
var_dump($_POST);
echo $text;
?>
This one gives me an empty var_dump
<?
if(!isset($_SESSION))
{
session_start();
}
?>
<form method="post" action=""><strong>
To User: <input type="text" name="ID" size="21" /><br />
Short Description: <input type="text" name="Item" size="21" /><br />
Link: <input type="text" name="Link" size="21" /><br />
Points: <select name="Points"><option value="1" selected="selected">1</option><option value="0">0</option><option value="-1">-1</option></select> (1 = Positive, 0 = Neutral, -1 = Negative)<br />
Text: <br /><textarea name="Text" rows="5" cols="50"/></textarea></strong><br />
<input type="submit" value="Send" />
</form>
<?
$id_from = $_SESSION['SESS_MEMBER_ID'];
$id_to = Sanitize($_POST['ID']);
$item = Sanitize($_POST['Item']);
$link = Sanitize($_POST['Link']);
$points= Sanitize($_POST['points']);
$text = Sanitize($_POST['Text']);
var_dump($_POST);
echo $text;
?>
based on your comment that you have this code:
$id_query = mysql_query("SELECT ID FROM tbl_users WHERE Username = '$id_to'");
$count = mysql_num_rows($id_query);
$id_row = mysql_fetch_array($id_query);
$id_to = $id_row['ID'];
if ($points > 1 || $points < -1) {
} else {
if (! ($id_to == $id_from)) {
if ($count == 1) {
mysql_query("INSERT INTO tbl_data (Item, Link) VALUES ('$item', '$link')");
header('Location:?id=submit');
} else {}
} else {}
}
I think the problem is on the line that says:
header('Location:?id=submit');
Perhaps you're testing with something that somehow make $points either greater than 1 or less than -1, $count is 1, and $id_to is different from $id_from, which then make the else block executed and (especially the line) header() executed, and user get redirect immediately.
To check if this is true, try var_dump($_GET) to see if you got something like:
array (size=1)
'id' => string 'submit' (length=6)
If you do, and perhaps the database isn't updated, then it's the mysql_query that sits right before the header is the one that you need to check.
Hope this helps.

Categories