Exactly what the title says. My PHP written after an the an if statement to check if a text portion of a form has any value is not running. It works fine in one written earlier in my code, but not the second time.
Here is my whole code:
<?php include '../header.php'; include '../navigation.php'; ?>
<div id="pageContent">
<form name="format" method="POST" action="phpFunctions.php">
Please input date:
<input type="date" name="date" value="<?php echo date('Y-m-d'); ?>" />
<input type="submit" name"submit1" value="Format" />
</form>
<?php
if (isset($_POST['date'])) {
$new_date = $_POST['date'];
$date = new DateTime($new_date);
echo 'M-D-Y Format: ' . $date->format('m-d-Y') . '<br>';
echo 'D-M-Y Format: ' . $date->format('d-m-Y');
}
?>
<form name="stringStuff" method="POST" action="phpFunctions.php">
Enter string:
<input type="text" name"yourString">
<input type="submit" name"submit2" value="Parse" />
</form>
<?php
if (isset($_POST['yourString']) && !empty($_POST['yourString'])) {
echo 'ayyyyyy lmao';
$new_string = $_POST['theString'];
$trimmed_string = trim($new_string);
echo 'ayyyyyy lmao';
echo 'String Length: ' . strlen($new_string) . '<br>';
echo 'Trim Whitespace: ' . $trimmed_string . '<br>';
echo 'Lowercased: ' . strtolower($new_string) . '<br>';
if(strpos($new_string, 'DMACC') !== false) {
echo 'String contains DMACC';
}
else {
echo 'String does not contain DMACC';
}
}
?>
</div>
<?php include '../footer.php'; ?>
The portion of the code that isn't working is this portion after what I mentioned
if (isset($_POST['yourString']) && !empty($_POST['yourString'])) {
echo 'ayyyyyy lmao';
$new_string = $_POST['theString'];
$trimmed_string = trim($new_string);
echo 'ayyyyyy lmao';
echo 'String Length: ' . strlen($new_string) . '<br>';
echo 'Trim Whitespace: ' . $trimmed_string . '<br>';
echo 'Lowercased: ' . strtolower($new_string) . '<br>';
if(strpos($new_string, 'DMACC') !== false) {
echo 'String contains DMACC';
} else {
echo 'String does not contain DMACC';
}
}
you missed = , name="yourString"
<form name="stringStuff" method="POST" action="test_index.php">
Enter string:
<input type="text" name="yourString">
<input type="submit" name="submit2" value="Parse" />
</form>
It seems you are missing name="yourString"
<input type="text" name="yourString">
<input type="submit" name="submit2" value="Parse" />
Related
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>  </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).
I am getting all types of errors on the following code. I want to post data from my form and store in a $_SESSION array for future processing
Illegal string offset 'SurveyDate'
Illegal string offset 'Income'
<input class="formFields" type="date" id="txtDateOfSurvey" name="mycensus[0][SurveyDate]"
<input class="formFields" type="numeric" id="txtIncome" name="mycensus[0][Income]"
<?php
session_start();
if( !isset($_SESSION['mycart2']))
{
$_SESSION['mycart2'] = array();
}
$_SESSION['mycart2'] = array_merge($_SESSION['mycart'], $_POST['mycensus']);
foreach($_SESSION['mycart2'] as $v)
{
echo $v['SurveyDate'] . ' was born on ' . $v['Income'] . '<br>';
}
?>
I want the array mycart2 to contain all the entries that I enter on my form.
<?php
session_start();
if(is_array(mycensus)){
$survey_date = mycensus[0]['SurveyDate'];
$Income = mycensus[0]['Income'];
}
?>
<input class="formFields" type="date" id="txtDateOfSurvey" name="survey_date" value="<?php echo $survey_date; ?>">
<input class="formFields" type="numeric" id="txtIncome" name="income" value="<?php echo $Income; ?>">
<?Php
if( !isset($_SESSION['mycart2']))
{
$_SESSION['mycart2'] = array();
}
$_SESSION['mycart2'] = array_merge($_SESSION['mycart'], $_POST['mycensus']);
foreach($_SESSION['mycart2'] as $v)
{
echo $v['SurveyDate'] . ' was born on ' . $v['Income'] . '<br>';
}
?>
I was making a form inside a PHP script and when i tried to access a field
of form in the same script it was giving me error Undefined index
This is my code:
<?php
$query="SELECT * FROM `vendor_list`";
$query_run=mysql_query($query);
if(mysql_num_rows($query_run)>0)
{
echo '<form method="POST">';
echo 'Book name:<input type="text" name="book_name" size="40" maxlength="40"><br><br>';
echo 'Number Of Copies:<input type="text" size="5" name="num_copies" maxlength="2"><br><br>';
echo '<select name="vendor_email">';
while($query_row=mysql_fetch_assoc($query_run))
{
$laser=$query_row['vendor_name'];
$email=$query_row['email'];
echo "<option value='".$email."'>".$laser."</option>";
}
echo '</select><br>';
echo '<input type="submit" value="submit">';
echo '</form>';
}
else
{
echo 'No vendor to display';
}
echo $_POST['book_name'];
USE THIS:-
<?php
$query="SELECT * FROM `vendor_list`";
$query_run=mysql_query($query);
if(mysql_num_rows($query_run)>0)
{
echo '<form method="POST">';
echo 'Book name:<input type="text" name="book_name" size="40" maxlength="40"><br><br>';
echo 'Number Of Copies:<input type="text" size="5" name="num_copies" maxlength="2"><br><br>';
echo '<select name="vendor_email">';
while($query_row=mysql_fetch_assoc($query_run))
{
$laser=$query_row['vendor_name'];
$email=$query_row['email'];
echo "<option value='".$email."'>".$laser."</option>";
}
echo '</select><br>';
echo '<input type="submit" value="submit">';
echo '</form>';
}
else
{
echo 'No vendor to display';
}
if($_POST){
echo $_POST['book_name'];
}
?>
Try this
<?php
$query="SELECT * FROM `vendor_list`";
$query_run=mysql_query($query);
if(mysql_num_rows($query_run)>0)
{
?>
<form method="POST" action="">
Book name:<input type="text" name="book_name" size="40" maxlength="40"><br><br>
Number Of Copies:<input type="text" size="5" name="num_copies" maxlength="2"><br><br>
<select name="vendor_email">
<?php
while($query_row=mysql_fetch_assoc($query_run))
{
$laser=$query_row['vendor_name'];
$email=$query_row['email'];
echo "<option value='".$email."'>".$laser."</option>";
}
?>
</select><br>
<input type="submit" name="submit" value="submit">
</form>
<?php
}
else
{
echo 'No vendor to display';
}
if(isset($_POST["submit"]))){
echo $_POST['book_name'];
}
?>
The Clean Way
All the breaks you have in that code are a hot mess...
Tip When you are echoing too much HTML just wrap it in an if..else.. like this...
<?php
$query = "SELECT * FROM `vendor_list`";
$query_run = mysql_query($query);
?>
<?php if(mysql_num_rows($query_run)>0): ?>
<form method="POST">
<label for="book_name">Book name:</label>
<input type="text" name="book_name" id="book_name" size="40" maxlength="40">
<label for="book_name"Number Of Copies:</label>
<input type="text" size="5" name="num_copies" maxlength="2">
<select name="vendor_email">
<?php while($query_row=mysql_fetch_assoc($query_run)): ?>
<option value="<?=$query_row['email']?>"><?=$query_row['vendor_name']?></option>
<?php endwhile; ?>
</select>
<input type="submit" value="submit">
</form>
<?php else: ?>
<p>No vendor to display</p>
<?php endif; ?>
<?php if (isset($_POST['book_name'])) echo $_POST['book_name']; ?>
Using the below code this will also check if you have a form submission or not, unless this code is a cut/copy paste.. It looks like it will process sections when it should not.. eg the echo post..
Personally i myself will set the action="" to PHP_SELF as many years ago had issues with it, soon this will change once i start using HTML5 when its cross browser enough / older users update their browsers.
Example of processing or showing a post but not both.
echo $_POST['book_name'];
echo "POST Dump: " . print_r($_POST, true);
} else {
// No Form Submission, So we generate the form instead
// QUERY - data
$query = "SELECT * FROM 'vendor_list'";
$query_run = mysql_query($query);
if(mysql_num_rows($query_run)>0) {
// FORM - Name/#Copies
echo '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '">';
echo 'Book name:<input type="text" name="book_name" size="40" maxlength="40"><br><br>';
echo 'Number Of Copies:<input type="text" size="5" name="num_copies" maxlength="2"><br><br>';
// FORM - Select Vendor
echo '<select name="vendor_email">';
while($query_row=mysql_fetch_assoc($query_run))
{
$laser=$query_row['vendor_name'];
$email=$query_row['email'];
echo "<option value='".$email."'>".$laser."</option>";
}
echo '</select><br>';
// FORM - Submit / Close Form
echo '<input type="submit" value="submit">';
echo '</form>';
} else {
echo 'No vendor to display';
}
}
?>
Or using a code that shows the form and processing any $_POST
<?php
// QUERY - data
$query = "SELECT * FROM 'vendor_list'";
$query_run = mysql_query($query);
if(mysql_num_rows($query_run)>0) {
// FORM - Name/#Copies
echo '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '">';
echo 'Book name:<input type="text" name="book_name" size="40" maxlength="40"><br><br>';
echo 'Number Of Copies:<input type="text" size="5" name="num_copies" maxlength="2"><br><br>';
// FORM - Select Vendor
echo '<select name="vendor_email">';
while($query_row=mysql_fetch_assoc($query_run))
{
$laser=$query_row['vendor_name'];
$email=$query_row['email'];
echo "<option value='".$email."'>".$laser."</option>";
}
echo '</select><br>';
// FORM - Submit / Close Form
echo '<input type="submit" value="submit">';
echo '</form>';
} else {
echo 'No vendor to display';
}
if (isset($_POST['book_name'])) {
echo "<b>Displaying post data</b> <br />";
echo "Example book: " . $_POST['book_name'] . "<br />";
echo "POST Dump: <br /><pre>" . print_r($_POST, true) . "</pre>";
} else {
echo "Page has not been subitted yet, please select an item";
}
?>
I found out that whenever I press submit button it does change the current value of the rand() the reason why it doesn't match on my input value.
I want to use this as validation to my form. Before the user can submit the message he/she must answer the question first.
here is my code:
<?php
session_start();
$numa = rand(1,5);
$numb = rand(0,4);
$_SESSION['valid_res'] = $numa + $numb;
echo 'Answer this '.$numa . ' + ' . $numb .' = ';
?>
<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type="text" name="input" /><input type="submit" name="submit" value="Submit" />
</form>
<?php
if(isset($_POST['submit'])){
if(intval($_POST['input']) != $_SESSION['valid_res']){
echo 'You enter have entered '. intval($_POST['input']) .' it is wrong answer';
}else{
echo 'Congratulations you have entered '. intval($_POST['input']) .' it is the correct answer.';
}
}
?>
You are overwriting the session value on each page load. Write it only on first load and use the subsequent value for the check.
You also have to convert the session variable to integer form.
Try this:
if(!isset($_POST['submit']))
{
$numa = rand(1,5);
$numb = rand(0,4);
$_SESSION['valid_res'] = $numa + $numb;
echo 'Answer this '.$numa . ' + ' . $numb .' = ';
}
else
if(intval($_POST['input']) != intval($_SESSION['valid_res']))
{
...
Use !== instead of !=. Use the code below
<?php
session_start();
$numa = rand(1,5);
$numb = rand(0,4);
$_SESSION['valid_res'] = $numa + $numb;
echo 'Answer this '.$numa . ' + ' . $numb .' = ';
?>
<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type="text" name="input" /><input type="submit" name="submit" value="Submit" />
</form>
<?php
if(isset($_POST['submit'])){
if(intval($_POST['input']) !==$_SESSION['valid_res']){
echo 'You enter have entered '. intval($_POST['input']) .' it is wrong answer';
}else{
echo 'Congratulations you have entered '. intval($_POST['input']) .' it is the correct answer.';
}
}
?>
Hope this helps you
I'm trying to integrate CKEditor into my simple CMS. I got it to show up on the page, but it's just above everything. I'm wondering how to get it into the correct spot, below my title textbox? Here is my code:
require_once 'conn.php';
include_once 'ckeditor/ckeditor.php';
$CKEditor = new CKEditor();
$CKEditor->editor('body');
$title= '';
$body= '';
$article= '';
$author_id= '';
if (isset($_GET['a'])
and $_GET['a'] == 'edit'
and isset($_GET['article'])
and $_GET['article']) {
$sql = "SELECT title, body, author_id FROM cms_articles " .
"WHERE article_id=" . $_GET['article'];
$result = mysql_query($sql, $conn) or
die ('Could not retrieve article data: ' . mysql_error());
$row = mysql_fetch_array($result);
$title = $row['title'];
$body = $row['body'];
$article = $_GET['article'];
$author_id = $row['author_id'];
}
require_once 'header.php';
?>
<form method="post" action="transact-article.php">
<h2>Compose Article</h2>
<p>
Title: <br />
<input type="text" class="title" name="title" maxlength="255" value="<?php echo htmlspecialchars($title); ?>" />
</p>
<p>
Body: <br />
<textarea class="body" name="body" id="body" rows="10" cols="60"><?php echo htmlspecialchars($body); ?></textarea>
</p>
<p>
<?php
echo '<input type="hidden" name="article" value="' .
$article . "\" />\n";
if ($_SESSION['access_lvl'] < 2) {
echo '<input type="hidden" name="author_id" value="' .
$author_id . "\" />\n";
}
if ($article) {
echo '<input type="submit" class="submit" name="action" ' .
"value=\"Save Changes\" />";
} else {
echo '<input type="submit" class="submit" name="action" ' .
"value=\"Submit New Article\" />";
}
?>
</p>
</form>
Personally I don't think you need the PHP library. Just add
<div contenteditable="true">
Editable text
</div>
as your editable and then just the script to get it running:
<script type="text/javascript" src="/path/to/ckeditor/ckeditor.js"></script>
That said, you may be able to pass the id of your textarea to the PHP library. To avoid confusion with the body tag, rename the id and name of this control to editable_content or similar. And as I mention above, try using a div instead.