preg_match Error When Check HTML many inputs - php

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!';
}
?>

Related

How do I get PHP to stop recognizing NULL values as elements in an array? [duplicate]

This question already has answers here:
How to submit data of a form with repeated field names?
(2 answers)
What is the difference between empty() , isset() and is_null() function in php?
(1 answer)
Closed 1 year ago.
This program is supposed to accept scores for 20 assignments, store them in an array, and then echo two things: the number of assignments for which a score was entered, and the total of all the scores. My issue is that no matter whether or not I enter a value for an assignment, php not only echos a label for the assignment, but it counts the empty field as part of the array and adds it to the variable I'm using to store the number of assignments for which a score has been entered. The below is the page on which the user enters their score(s), and the page below that is the page that is supposed to display their results.
<!DOCTYPE html>
<html>
<head>
<title>Assignments</title>
<link href="main.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="content">
<h1>Enter Your Scores (0-100, 2000 possible)</h1></br>
<p>Press The "Calculate" button when finished.</p></br>
<form action="arraysolo.php" method="post">
<div id="data">
<label>Assignment 1</label>
<input type="text" name="assign1"><br />
<label>Assignment 2</label>
<input type="text" name="assign2"><br />
<label>Assignment 3</label>
<input type="text" name="assign3"><br />
<label>Assignment 4</label>
<input type="text" name="assign4"><br />
<label>Assignment 5</label>
<input type="text" name="assign5"><br />
<label>Assignment 6</label>
<input type="text" name="assign6"><br />
<label>Assignment 7</label>
<input type="text" name="assign7"><br />
<label>Assignment 8</label>
<input type="text" name="assign8"><br />
<label>Assignment 9</label>
<input type="text" name="assign9"><br />
<label>Assignment 10</label>
<input type="text" name="assign10"><br />
<label>Assignment 11</label>
<input type="text" name="assign11"><br />
<label>Assignment 12</label>
<input type="text" name="assign12"><br />
<label>Assignment 13</label>
<input type="text" name="assign13"><br />
<label>Assignment 14</label>
<input type="text" name="assign14"><br />
<label>Assignment 15</label>
<input type="text" name="assign15"><br />
<label>Assignment 16</label>
<input type="text" name="assign16"><br />
<label>Assignment 17</label>
<input type="text" name="assign17"><br />
<label>Assignment 18</label>
<input type="text" name="assign18"><br />
<label>Assignment 19</label>
<input type="text" name="assign19"><br />
<label>Assignment 20</label>
<input type="text" name="assign20"><br />
</div>
<div id="button">
<label>&nbsp </label>
<input type="submit" value="Calculate" /><br />
</div>
</form>
</div>
</body>
</html>
The below is the page which displays the users results.
<!DOCTYPE html>
<html>
<head>
<title>Your Results:</title>
<link href="main.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="content">
<h1>Score Per Assignment:</h1></br>
<?php
$score = array();
if(!isset($_POST['assign1'])) {
$score[0] = null;
}else {
$score[0] = $_POST['assign1'];
echo "<h2>Assignment 1:</h2>" . $score[0] . "</br>";
}
if(!isset($_POST['assign2'])) {
$score[1] = null;
}else {
$score[1] = $_POST['assign2'];
echo "<h2>Assignment 2:</h2>" . $score[1] . "</br>";
}
if(!isset($_POST['assign3'])) {
$score[2] = null;
}else {
$score[2] = $_POST['assign3'];
echo "<h2>Assignment 3:</h2>" . $score[2] . "</br>";
}
if(!isset($_POST['assign4'])) {
$score[3] = null;
}else {
$score[3] = $_POST['assign4'];
echo "<h2>Assignment 4:</h2>" . $score[3] . "</br>";
}
if(!isset($_POST['assign5'])) {
$score[4] = null;
}else {
$score[4] = $_POST['assign5'];
echo "<h2>Assignment 5:</h2>" . $score[4] . "</br>";
}
if(!isset($_POST['assign6'])) {
$score[5] = null;
}else {
$score[5] = $_POST['assign6'];
echo "<h2>Assignment 6:</h2>" . $score[5] . "</br>";
}
if(!isset($_POST['assign7'])) {
$score[6] = null;
}else {
$score[6] = $_POST['assign7'];
echo "<h2>Assignment 7:</h2>" . $score[6] . "</br>";
}
if(!isset($_POST['assign8'])) {
$score[7] = null;
}else {
$score[7] = $_POST['assign8'];
echo "<h2>Assignment 8:</h2>" . $score[7] . "</br>";
}
if(!isset($_POST['assign9'])) {
$score[8] = null;
}else {
$score[8] = $_POST['assign9'];
echo "<h2>Assignment 9:</h2>" . $score[8] . "</br>";
}
if(!isset($_POST['assign10'])) {
$score[9] = null;
}else {
$score[9] = $_POST['assign10'];
echo "<h2>Assignment 10:</h2>" . $score[9] . "</br>";
}
if(!isset($_POST['assign11'])) {
$score[10] = null;
}else {
$score[10] = $_POST['assign11'];
echo "<h2>Assignment 11:</h2>" . $score[10] . "</br>";
}
if(!isset($_POST['assign12'])) {
$score[11] = null;
}else {
$score[11] = $_POST['assign12'];
echo "<h2>Assignment 12:</h2>" . $score[11] . "</br>";
}
if(!isset($_POST['assign13'])) {
$score[12] = null;
}else {
$score[12] = $_POST['assign13'];
echo "<h2>Assignment 13:</h2>" . $score[12] . "</br>";
}
if(!isset($_POST['assign14'])) {
$score[13] = null;
}else {
$score[13] = $_POST['assign14'];
echo "<h2>Assignment 14:</h2>" . $score[13] . "</br>";
}
if(!isset($_POST['assign15'])) {
$score[14] = null;
}else {
$score[14] = $_POST['assign15'];
echo "<h2>Assignment 15:</h2>" . $score[14] . "</br>";
}
if(!isset($_POST['assign16'])) {
$score[15] = null;
}else {
$score[15] = $_POST['assign16'];
echo "<h2>Assignment 16:</h2>" . $score[15] . "</br>";
}
if(!isset($_POST['assign17'])) {
$score[16] = null;
}else {
$score[16] = $_POST['assign17'];
echo "<h2>Assignment 17:</h2>" . $score[16] . "</br>";
}
if(!isset($_POST['assign18'])) {
$score[17] = null;
}else {
$score[17] = $_POST['assign18'];
echo "<h2>Assignment 18:</h2>" . $score[17] . "</br>";
}
if(!isset($_POST['assign19'])) {
$score[18] = null;
}else {
$score[18] = $_POST['assign19'];
echo "<h2>Assignment 19:</h2>" . $score[18] . "</br>";
}
if(!isset($_POST['assign20'])) {
$score[19] = null;
}else {
$score[19] = $_POST['assign20'];
echo "<h2>Assignment 20:</h2>" . $score[19] . "</br>";
}
?>
<?php
$sum = array_sum($score);
$num_elements = count($score);
echo "<h2>Assignments Completed:</h2>" . $num_elements .
"</br>";
echo "<h2>Assignments Score Total:</h2>" . $sum .
"</br>";
?>
</div>
</body>
</html>
To expand on Alex Howansky's answer, you'll need to turn your <input> fields into an array (see How do I create arrays in a HTML form? in PHP's manual).
To do that, just replace all input fields with:
<input type="text" name="assign[]"><br />
Afterwards, you can loop them like so (this would replace your PHP code in arraysolo.php):
$score = [];
foreach ($_POST['assign'] ?? [] as $key => $value) {
$is_valid = is_numeric($value) && $value >= 0 && $value <= 200;
if ($is_valid) {
$score[] = $value;
echo "<h2>Assignment " . ($key + 1) . ":</h2>" . $value . "</br>";
}
}
$sum = array_sum($score);
$num_elements = count($score);
echo "<h2>Assignments Completed:</h2>" . $num_elements . "</br>";
echo "<h2>Assignments Score Total:</h2>" . $sum . "</br>";
This way, you'll also don't have to filter for any null values (because you won't assign them in the first place).

PHP Random Math Quiz Evaluation issue

EDIT: When I print the $answer variable by itself it always returns the correct answer of the current question.
I am currently coding a PHP script that produces a simple, but completely random math quiz. How it works is that is gets two random numbers between 0-9 and a random operator from '-', '+', '*'. The user must enter into the text box the answer of the shown question. From here it's pretty straightforward to understand.
However, the issue I am having is that no matter what the user enters the only questions that are validated as correct are ones where the answer is 0.
Here is my code so far.
<?php
require 'functions.php';
$body = "";
$score = 0;
$count = 0;
if(isset($_POST['submit']))
{
$firstDigit = $_POST['lho'];
$secondDigit = $_POST['rho'];
$operator = $_POST['op'];
$userAnswer = $_POST['answer'];
$count = $_POST['count'];
$score = $_POST['score'];
$answer = evaluate($firstDigit, $secondDigit, $operator);
if($answer == $userAnswer)
{
$count++;
$score++;
$body .= "\n<h1>Congratulations!</h1>\n\n";
$body .= "$score out of $count";
}
else
{
$count++;
$body .= "\n<h1>Sorry!</h1>\n\n";
$body .= "$score out of $count";
}
}
header( 'Content-Type: text/html; charset=utf-8');
print("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
?>
<?php
include("./header.php");
?>
<h1>Math Quiz</h1> <br /> <br />
<?php
print $body;
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<h3><?php echo $firstDigit; ?> <?php echo $operator; ?> <?php echo $secondDigit; ?> = ?
<input type="text" name="answer" size="2" /></h3>
<p><input type="submit" name="submit" value="Try It!" />
<input type="hidden" name="lho" value="<?php echo randdigit(); ?>" />
<input type="hidden" name="rho" value="<?php echo randdigit(); ?>" />
<input type="hidden" name="op" value="<?php echo randop(); ?>" />
<input type="hidden" name="score" value="<?php echo $score++; ?>" />
<input type="hidden" name="count" value="<?php echo $count++; ?>" /></p>
</form>
<?php
include("./footer.php");
?>
My evaluate function is this:
function evaluate($d1, $d2, $op) {
switch($op) {
case '+' : // addition
$result = $d1 + $d2;
break;
case '-' : // subtraction
$result = $d1 - $d2;
break;
case '*' : // multiplication
$result = $d1 * $d2;
break;
default : // Unidentified, return safe value
$result = 0;
}
return $result;
}
Here is the randop() function and the randdigit() function:
/* Return a number in the range 0-9 inclusive
*/
function randdigit() {
return mt_rand(0,9);
} // end functionranddigit()
function randop(){
$ops = array('+', '-', '*');
// pick a random index between zero and highest index in array.
$randnum = mt_rand(0,sizeof($ops)-1);
return $ops[$randnum]; // Use the index to pick the operator
}
First on the 48th line :
<h3><?php echo $firstDigit; ?> <?php echo $operator; ?> <?php echo $secondDigit; ?> = ?
When you first load the form and until it has been submitted once,
$firstDigit, $operator, $secondDigit
Aren't set.
Then, this line wich is the equation to solve is filled with the old equation that needed to be solved AND your hidden fields are filled with new numbers, invisible to the user using randdigit() and randop().
<input type="hidden" name="lho" value="<?php echo randdigit(); ?>" />
<input type="hidden" name="rho" value="<?php echo randdigit(); ?>" />
<input type="hidden" name="op" value="<?php echo randop(); ?>" />
Here is the code that works well for me :
<?php
require 'functions.php';
$body = "";
$score = 0;
$count = 0;
$newFdigit = randdigit();
$newSdigit = randdigit();
$newOperator = randop();
if(isset($_POST['submit']))
{
$firstDigit = $_POST['lho'];
$secondDigit = $_POST['rho'];
$operator = $_POST['op'];
$userAnswer = $_POST['answer'];
$count = $_POST['count'];
$score = $_POST['score'];
$answer = evaluate($firstDigit, $secondDigit, $operator);
if($answer == $userAnswer)
{
$count++;
$score++;
$body .= "\n<h1>Congratulations!</h1>\n\n";
$body .= "$score out of $count";
}
else
{
$count++;
$body .= "\n<h1>Sorry!</h1>\n\n";
$body .= "$score out of $count";
}
}
header( 'Content-Type: text/html; charset=utf-8');
print("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
?>
<h1>Math Quiz</h1> <br /> <br />
<?php
print $body;
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<h3><?php echo $newFdigit; ?> <?php echo $newOperator; ?> <?php echo $newSdigit; ?> = ?
<input type="text" name="answer" size="2" /></h3>
<p><input type="submit" name="submit" value="Try It!" />
<input type="hidden" name="lho" value="<?php echo $newFdigit ?>" />
<input type="hidden" name="rho" value="<?php echo $newSdigit; ?>" />
<input type="hidden" name="op" value="<?php echo $newOperator; ?>" />
<input type="hidden" name="score" value="<?php echo $score++; ?>" />
<input type="hidden" name="count" value="<?php echo $count++; ?>" /></p>
</form>

Group Arrays By Another Array?

I'm trying to sort one array by another array. Both these arrays get their content from a form.
Here's my form code:
<form method="post" action="">
<div class="groupcontainer">
<br/><label>Group One:</label><br/>
<input type="text" name="groupname[]" value="groupone" /><br/>
<br/><label>Variable Group One:</label><br/>
<input type="text" name="variable[]" value="variableone" />
<input type="text" name="variable[]" value="variabletwo" />
</div>
<br/>
<div class="groupcontainer">
<br/><label>Group Two:</label><br/>
<input type="text" name="groupname[]" value="grouptwo" /><br/>
<br/><label>Variable Group Two:</label><br/>
<input type="text" name="variable[]" value="variablethree" />
<input type="text" name="variable[]" value="variablefour" />
</div>
<br/>
<input type="submit" name="submit" value="Submit" />
</form>
Here's the PHP code:
<?php
if (!$_POST['submit'] == "") {
foreach($_POST['groupname'] as $groupname) {
$groupnum = 1;
foreach($_POST['variable'] as $variable) {
print "$".$groupname.$groupnum." = '".$variable."';<br/>";
$groupnum++;
}
print "$".$groupname." = array(";
for ($arrnum = 1; $arrnum <= count($_POST['variable']); $arrnum++) {
print "$".$groupname.$arrnum.", ";
}
print ");<br/><br/>";
}
}
?>
This is the result I get when I submit the form:
$groupone1 = '$variableone';
$groupone2 = '$variabletwo';
$groupone3 = '$variablethree';
$groupone4 = '$variablefour';
$groupone = array($groupone1, $groupone2, $groupone3, $groupone4, )
$grouptwo1 = '$variableone';
$grouptwo2 = '$variabletwo';
$grouptwo3 = '$variablethree';
$grouptwo4 = '$variablefour';
$grouptwo = array($grouptwo1, $grouptwo2, $grouptwo3, $grouptwo4, )
This is the result that I actually want:
$groupone1 = '$variableone';
$groupone2 = '$variabletwo';
$groupone = array($groupone1, $groupone2)
$grouptwo1 = '$variablethree';
$grouptwo2 = '$variablefour';
$grouptwo = array($grouptwo1, $grouptwo2)
The whole thing needs to be dynamic since I want to add as many groups and variables as I want.
I've been searching for an answer for days and already asked two people who didn't know an answer. Maybe you guys can help. Thanks!
Update:
Just to clarify a few points:
So basically I want to be able to add as many input forms as I want (I use jQuery for that) to create as many groups and variables as I want, for example like this:
$groupwuteva1 = 'hello';
$groupwuteva2 = 'bye':
$randomname1 = 'green';
$randomname2 = 'blue';
$randomname3 = 'red';
$blabla1 = 'abc';
$blabla2 = 'xyz';
$blabla3 = '123';
$blabla4 = 'bla';
Whatever I use as groupname will be used in array one, e.g. I call a group "Colors" and the variables I put into the form for that group are "blue", "red" and "green". Then I would get this code:
$colors1 = 'green';
$colors2 = 'blue';
$colors3 = 'red';
I hope this clairfies some questions. And thanks a ton for all responses so far!
You can take the group name as a container to store all associated variable values in it. And later, use variable variables and implode() function to process your html form.
HTML
<form method="post" action="">
<div class="groupcontainer">
<br/><label>Groupe One:</label><br/>
<input type="text" name="groupname[]" value="groupone" /><br/>
<br/><label>Variable Group One:</label><br/>
<input type="text" name="groupone[]" value="variableone" />
<input type="text" name="groupone[]" value="variabletwo" />
</div>
<br/>
<div class="groupcontainer">
<br/><label>Groupe Two:</label><br/>
<input type="text" name="groupname[]" value="grouptwo" /><br/>
<br/><label>Variable Group One:</label><br/>
<input type="text" name="grouptwo[]" value="variablethree" />
<input type="text" name="grouptwo[]" value="variablefour" />
</div>
<br/>
<input type="submit" name="submit" value="Submit" />
</form>
PHP
if(isset($_POST['submit'])){
foreach($_POST['groupname'] as $value){
$arr = array();
$i = 1;
foreach($_POST[$value] as $v){
$var = $value . $i;
$$var = $v;
echo $var . " = " . $$var . "<br />";
$arr[] = $$var;
++$i;
}
$output = $value . " = array(" . implode(",", $arr) . ")";
echo $output . "<br /><br />";
}
}
Output:
groupone1 = variableone
groupone2 = variabletwo
groupone = array(variableone,variabletwo)
grouptwo1 = variablethree
grouptwo2 = variablefour
grouptwo = array(variablethree,variablefour)
try this form:
<form method="post" action="">
<?php foreach(array('groupone', 'grouptwo') as $num):?>
<div class="groupcontainer">
<br/><label>Groupe <?php echo $num;?>:</label><br/>
<input type="text" name="groupname[]" value="<?php echo $num;?>" /><br/>
<br/><label>Variable Group <?php echo $num;?>:</label><br/>
<input type="text" name="variable[<?php echo $num;?>][]" value="<?php echo uniqid();?>" />
<input type="text" name="variable[<?php echo $num;?>][]" value="<?php echo uniqid();?>" />
</div>
<br/>
<?php endforeach;?>
<input type="submit" name="submit" value="Submit" />
</form>
and code:
if ($_POST) {
foreach($_POST['groupname'] as $groupname) {
$$groupname = array();
foreach($_POST['variable'][$groupname] as $variable) {
${$groupname}[] = $variable;
}
}
var_dump($groupone);
var_dump($grouptwo);
}

How to output all checkbox value

hello please help me i have a problem outputting all the values of checkbox it outputs only one i need to show all the checked checkbox and output them please help me here is the code it only shows one whenever i checked them all i need this
<html>
<head>
<title>Cake Form</title>
<link rel="stylesheet" href="cakeform.css">
</head>
<body>
<?php
$nameErr = $addErr = $phoneErr = $scake = $flavorcake = $fill = "";
$name = $address = $phone = $rcake = $fillr = $cakeflavor = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
if (empty($_POST["address"])) {
$addErr = "Email is required";
} else {
$address = test_input($_POST["address"]);
}
if (empty($_POST["phone"])) {
$phoneErr = "Gender is required";
} else {
$phone = test_input($_POST["phone"]);
}
if(isset($_POST['SelectedCake'])){
$x=$_POST['SelectedCake'];
}
if(isset($_POST['CakeFlavor'])){
$y=$_POST['CakeFlavor'];
}
if(isset($_POST['Filling'])){
$z=$_POST['Filling'];
}
if(empty($x)){
$scake='Select one Cake';
}else{
$rcake= $x;
}
if(empty($y) OR $y == 'Flavor'){
$flavorcake='Select one flavor';
}else{
$cakeflavor= $y;
}
if(empty($z)){
$fill='Select at least one Fillings';
}else{
foreach($z as $item){
$fillr=$item;
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<div id="wrap">
<div class="cont_order">
<fieldset>
<legend>Make your own Cake</legend>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<h4>Select size for the Cake:</h4>
<input type="radio" name="SelectedCake" value="Round6">Round cake 6" - serves 8 people</br>
<input type="radio" name="SelectedCake" value="Round8">Round cake 8" - serves 12 people</br>
<input type="radio" name="SelectedCake" value="Round10">Round cake 10" - serves 16 people</br>
<input type="radio" name="SelectedCake" value="Round12">Round cake 12" - serves 30 people</br>
<span class="error">*<?php echo $scake;?></span>
<h4>Select a Cake Flavor: </h4>
<select name="CakeFlavor">
<option value="Flavor" selected="selected">Select Flavor</option>
<option value="Carrot" >Carrot</option>
<option value="Chocolate" >Chocolate</option>
<option value="Banana" >Banana</option>
<option value="Red Velvet" >Red Velvet</option>
<option value="Strawberry" >Strawberry</option>
<option value="Vanilla" >Vanilla</option>
<option value="Combo" >Combo</option>
</select>
<span class="error">*<?php echo $flavorcake;?></span>
<h4>Select Fillings:</h4>
<label><input type="checkbox" name="Filling[]" value="Cream"/>Cream</label><br>
<label><input type="checkbox" name="Filling[]" value="Fudge"/>Fudge</label><br>
<label><input type="checkbox" name="Filling[]" value="Ganache"/>Ganache</label><br>
<label><input type="checkbox" name="Filling[]" value="Hazelnut"/>Hazelnut</label><br>
<label><input type="checkbox" name="Filling[]" value="Mousse"/>Mousse</label><br>
<label><input type="checkbox" name="Filling[]" value="Pudding"/>Pudding</label><br>
<span class="error">*<?php echo $fill;?></span>
</fieldset>
</div>
<div class="cont_order">
<fieldset>
<legend>Contact Details</legend>
<label for="name">Name</label><br>
<input type="text" name="name" id="name"><span class="error">*<?php echo $nameErr;?></span>
<br>
<label for="address">Address</label><br>
<input type="text" name="address" id="address"><span class="error">*<?php echo $addErr;?></span>
<br>
<label for="phonenumber">Phone Number</label><br>
<input type="text" name="phone" id="phone"><span class="error">*<?php echo $phoneErr;?></span><br>
</fieldset>
<input type="submit" name="submitted" id="submit" value="Submit"/>
</div>
</form>
<div class="cont_order">
<?php
echo $name.'<br>';
echo $address.'<br>';
echo $phone.'<br>';
echo $rcake.'<br>';
echo $cakeflavor.'<br>';
echo $fillr.'<br>';
?>
</div>
</div>
</body>
</html>
You can do this:
var_dump($_POST['Filling']);
Or just this:
<?php
if(!empty($_POST['Filling'])) {
foreach($_POST['Filling'] as $check) {
echo $check;
}
}
?>
Tell me if it works =)
Fact is, you accept a list for filling.
In your code you do this :
foreach($z as $item){
$fillr=$item;
}
Replace it by :
$fillr = '';
foreach($z as $item){
$fillr .= '- ' . $item . '<br/>';
}
It should work better.
For comment :
#so let's say $z is an array :
$z = [1,2,3,4];
#then you say, for each element of the array as you will call $item, I do something
foreach($z as $item){
$fillr=$item; #here you say : for each element of $z (1,2,3,4), I put this value into $fillr.
}
#so let's do the loop together.
# First pass, $item == 1, so $fillr == 1.
# Second pass, $item == 2, so $fillr == 2.
# ...
# Last pass, $item == 4, so $fillr == 4.
# so then when you show $fillr it's the last item of the list (here 4).
# using PHP, .= means concatenate.
# for instance :
$a = "hello ";
$a .= "world";
echo $a;
> hello world
So basically, what I'm doing is :
Having an empty string I'll call $fillr.
For every element of the list,
Append "the_element plus a <br/>" to $fillr
so then when you output $fillr it looks sexy.
Do you get it bud?

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>

Categories