I'm trying to create a simple PHP page that will Display a number starting with 0. It will then ask for a number input, then add the inputted number to the beginning number. and keep adding the number that is input in the text box. Here's what I have so far... I can get the first variable initialized and get the input from a form box. I just can't seem to add them together and keep a running total. Thanks in advance.
<?php
$_POST['number1'];
echo "Current number is ".$_POST['number1'];
echo "<br>";
echo "Enter your next number.<br>";
echo "<form action='' method='POST'>";
echo "<input type='number' name='number2'>";
echo "<input type='submit' name='submit' value='Submit'>";
echo "<br>";
echo "Your entered number is ".$_POST['number2']."<br>";
$sumtotal = $_POST['number1'] + $_POST['$number2'];
echo "Your new total is ".$sumtotal;
$_POST['number1'] == $_POST['number2'];
?>
Using input type HIDDEN:
<?php
if(isset($_POST['submit'])){
$total=$_POST['number1']+$_POST['number2'];
echo "Your entered number is ".$_POST['number2']."<br>";
}
else {
$total=0;
}
echo "Current total number is ".$total;
echo "<br>";
echo "Enter your number.<br>";
echo "<form action='' method='POST'>";
echo "<input type='hidden' name='number1' value='$total'>";
echo "<input type='number' name='number2'>";
echo "<input type='submit' name='submit' value='Submit'>";
echo "</form><br>";
echo "Your new total is ".$total;
?>
Using SESSION:
<?php
session_start();
if(empty($_SESSION["total"])){
$_SESSION["total"]=0;
}
if(isset($_POST['submit'])){
$total=$_SESSION["total"];
$total=$total+$_POST['number2'];
$_SESSION["total"]=$total;
echo "Your entered number is ".$_POST['number2']."<br>";
}
echo "Current total number is ".$_SESSION['total'];
echo "<br>";
echo "Enter your number.<br>";
echo "<form action='' method='POST'>";
echo "<input type='number' name='number2'>";
echo "<input type='submit' name='submit' value='Submit'>";
echo "</form><br>";
echo "Your new total is ".$_SESSION['total'];
?>
Both works and have the same output, but with different approach from one another.
You could add another hidden input field that keeps the last number that was posted by you.
<input style="display: none" name="tmp_number" value="<?php echo $tmp_number; ?>">
Try (edited for all details and errors)
<?php
$input = $sum = 0;
if (isset($_POST)) {
$input = $_POST['number1'];
$prev_sum = $_POST['prev'];
$sum = $input + $prev_sum;
}
echo "Current number is ".$input;
echo "<br>";
echo "Enter your next number.<br>";
echo "<form action='' method='POST'>";
echo "<input type='number' name='number1'>";
echo "<input type='hidden' name='prev' value='$sum' >";
echo "<input type='submit' name='submit' value='Submit'>";
echo "<br>";
echo "Your entered number is ".$_POST['number1']."<br>";
echo "Your new total is ".$sum;
?>
Related
I am using 2 pages. Page 1, the user enters name and age. I have given form action as the same page and user selects either one or both occupation and the data gets pushed to page 2 which displays all the results. Below is the code for the same:
Page1:
<?php
echo "<form action=\"page1.php\" method=\"post\">";
echo "<h2>Your Name. *</h2><input type='text' name='user_name'>";
echo "<br><br>";
echo "<h2>Your Age. *</h2><input type='text' name='age'>";
echo "<br><br>";
echo "<div><input type='submit' value='Review'></div>";
?>
<?php
if((empty($_POST['user_name'])) || (empty($_POST['age'])) ) {
echo "<h2>Please enter your user name and age</h2>";
} else {
$user_name = $_POST['user_name'];
$age = $_POST['age'];
echo "<h2>Below are the details entered:</h2><br>";
echo "<h2>Name: </h2>$user_name";
echo "<h2>Age: </h2>$age";
echo "<form action=\"page2.php\" method=\"post\">";
echo '<td bgcolor="#EAEAEA" style="color:#003399"><input type="checkbox"
name="occupation[]" value="QA">QA</td>';
echo '<td bgcolor="#EAEAEA" style="color:#003399"><input type="checkbox"
name="occupation[]" value="Tester">Tester</td>';
echo "<br>";
echo "<input type=\"submit\" value='Add to DB' >";
echo "</form>";
}
?>
Page2:
<?php
$user_name = $_GET['user_name'];
$age = $_GET['junos_version'];
$occupation = $_GET['occupation'];
echo "<h2>Below are the details entered:</h2><br>";
echo "<h2>Name: </h2>$user_name";
echo "<h2>Age: </h2>$age";
echo "<h2>Occupation selected: </h2>";
for ($i=0;$i<sizeof($occupation);$i++) {
echo " $occupation[$i] ";
}
?>
Please help!!!
Put all of that fields in just one form and validate the action or put validation into php before frontend start, I prefer the first one to your case, it's easier:
FILTERING IN THE ACTION
<?php
if (empty($_POST['user_name'])){
$action = 'page1.php';
$structure = "<h2>Please enter your user name and age</h2>";
$submit = 'Review';
} else {
$action = 'page2.php';
$phrase = '';
$user_name = $_POST['user_name'];
$age = $_POST['age'];
$structure = '<h2>Below are the details entered:</h2><br>
<h2>Name: </h2>$user_name
<h2>Age: </h2>$age
<td bgcolor="#EAEAEA" style="color:#003399"><input type="checkbox" name="occupation[]" value="QA">QA</td>
<td bgcolor="#EAEAEA" style="color:#003399"><input type="checkbox" name="occupation[]" value="Tester">Tester</td>';
echo "<br>";
$submit = 'Add to DB';
}
echo "<form action='{$action}' method=\"post\">";
echo "<h2>Your Name. *</h2><input type='text' name='user_name'>";
echo "<br><br>";
echo "<h2>Your Age. *</h2><input type='text' name='age'>";
echo "<br><br>";
echo $structure;
echo "<div><input type='submit' value='{$submit}'></div>";
echo "</form>";
?>
PS: Avoid all those echoes, put just plain html and use php where it needed, it's better. TD's outside tables are utterly wrong, remove then, use divs instead and organize them with CSS.
I am writing a program that is supposed to add a studio. This is my addstudio.php
<?php
include ("db.php");
$pagename="Add a New Location";
echo "<title>".$pagename."</title>";
echo "<h2>".$pagename."</h2>";
echo "<body>";
echo "<hr><b>".date('d F Y H:i:s')."</b>";
echo "<hr>Fill the form below to add a new studio";
//create a html form to capture user input
echo "<form method=post action=getstudio.php>" ;
echo "<table border=0 cellpadding=10>";
echo "<tr><td>*Studio Id </td>";
echo "<td><input type=text name=form_studioID size=35></td></tr>";
echo "<tr><td>*Studio Name </td>";
echo "<td><input type=text name=form_studioName size=35></td></tr>";
echo "<tr><td>*Address </td>";
echo "<td><input type=text name=form_studioAddress size=35></td></tr>";
echo "<tr><td>*Post Code </td>";
echo "<td><input type=text name=form_postcode size=35></td></tr>";
echo "<tr><td>Telephone Number </td>";
echo "<td><input type=text name=form_tel size=35></td></tr>";
echo "<tr><td><input type=submit value='Add Studio'></td>";
echo "<td><input type=reset value='Clear Form'></td></tr>";
echo "</table>";
echo "</form>" ;
echo "</body>";
?>
And then the getstudio.php file that is supposed to add the entered studio to the database and display all of them(the one that has been just entered and these that already were on the server)
<?php
include ("db.php");
$pagename="View Studio";
echo "<title>".$pagename."</title>";
echo "<h2>".$pagename."</h2>";
echo "<body>";
echo "<hr><b>".date('d F Y H:i:s')."</b>";
echo"<hr>";
//capture the values inserted in the form's fields and store them in local variables
$studid=$_POST['form_studioID'];
$studname=$_POST['form_studioName'];
$studaddress=$_POST['form_studioAddress'];
$studpostcode=$_POST['form_postoce'];
$studtel=$_POST['form_tel'];
//check if any if the mandatory fields were not filled in
if(empty($studid) or empty($studname) or empty($studaddress) or empty($studpostoce)
or empty($studtel)){
echo "<p> Please ensure all fields are filled in !";
}
else{
$addstudioSQL=
"insert into
studio(studio_id, studio_name, studio_address, studio_postcode, studio_tel)
values(".$studid.", '".$studname."', '".$studaddress."', '".$studpostcode."', '".$studtel."')";
}
$exeaddstudioSQL=mysql_query($addstudioSQL);
//write SQL query
$viewstudioSQL="select studio_id, studio_name, studio_address, studio_postcode, studio_tel
from studio
order by studio_id";
//Run SQL query or exit if any errors are retrieved
$exeviewstudioSQL=mysql_query($viewstudioSQL) or die (mysql_error());
//Create an array of records and fecth the results of the execution of the SQL query
//Loop through the array of records and display details in specific format
while ($arraystudio=mysql_fetch_array($exeviewstudioSQL))
{
echo "<p>Location Id: ".$arraystudio['studio_id'];
echo "<br>".$arraystudio['studio_name']."<br> ".$arraystudio['studio_address'].", ".$arraystudio['studio_postcode'];
echo "<br>".$arraystudio['studio_tel'];
echo "<hr>";
}
echo "</body>";
?>
What this does is to print the studios that are on the server but not the one that has been just entered. It also has some problems with printing the postcode.
Anyone who can help? Much appreciated.
Please refer to the image below:
http://i.stack.imgur.com/6hBPC.png
For instance, if a user clicks the button on the row which says "You have a quiz for math", the "Quiz ID" value of THAT row would then be passed to another PHP file.
Here's my current code:
<?php
$con=mysqli_connect("127.0.0.1", "root", "", "quizmaker");
if (mysqli_connect_errno($con))
{
echo "MySqli Error: " . mysqli_connect_error();
}
$now=date("m/d/Y");
$sql=mysqli_query($con,"SELECT * FROM quiz_query WHERE quiz_date='$now'");
$count=mysqli_num_rows($sql);
if($count>=1)
{
echo "<table border='1' width='50%'>";
echo "<form action='answer_quiz.php' method='post'>";
echo "<tr>
<td>You have a pending quiz!</td><td> </td><td> </td>
</tr>";
$number=1;
while($result=mysqli_fetch_array($sql))
{
echo "<tr>";
echo "<td>You have a quiz for " . $result['subject'] . "</td>";
echo "<td>Quiz ID: " .$result['quiz_ID']. "</td>";
echo "<td><input type='submit' name='button' id='button' value='Take Quiz'>";
echo "<input type='hidden' name='quiz[$number]' value='$result[quiz_ID]'>";
echo "</td>";
echo "</tr>";
$number++;
}
echo "</form>";
echo "</table>";
}
else
{
"You have no quiz! :D";
}
mysqli_close($con);
?>
Move this line:
echo "<form action='answer_quiz.php' method='post'>";
Inside of the while loop.
Also, change
echo "<input type='hidden' name='quiz[$number]' value='$result[quiz_ID]'>"
with
echo "<input type='hidden' name='quizId' value='$result[quiz_ID]'>"
Now, in answer_quiz.php you'll receive $_POST['quizId'] with the value you need.
Change your while to :
while( $row = $result->fetch_array(MYSQLI_ASSOC)){
echo $row['subject'];
}
You are forgetting quotes around your variable:
Instead of
echo "<input type='hidden' name='quiz[$number]' value='$result[quiz_ID]'>";
It should be
echo "<input type='hidden' name='quiz[$number]' value='$result[\"quiz_ID\"]'>";
For some reason when I call a function to create a form, the drop down menus aren't sticky and the browser forces users to click into the first text field and tab through the rest. It won't let them mouse through the fields. This is only happening in FF, not IE or Chrome. The forms I'm including are just basic html and only php pages I include are doing this.
Here is one function:
function addNoteUI($keyword) {
echo "<div id='search_result_right'>";
echo "<center><div id='enter_note_header'>Assign a Salesperson</div></center><p>";
echo "<form id='response' action='notes_add.php' method='post'>";
echo "<label for='mod_num'>MOD Initials: <label>";
echo "<input type='text' name='mod_num' size='2' maxlength='4'><p>";
echo "<label for='sales_num'>Assigned to Sales Person: <label>";
echo "<input type='text' name='sales_num' size='2' maxlength='4'><p>";
echo "<input type='hidden' name='question_num' value='$keyword'>";
echo "<label for='response'>Note</label><br>";
echo "<textarea name='response' cols='30' rows='7 maxlength='510'></textarea><p>";
echo "<input type='submit' value='Assign'>";
echo "</form>";
echo "</div>";
Here is the other:
function changeDept() {
include 'ask_search.php';
echo "<div id='search_result'>";
echo "<form action='change_dept.php' method='post'>";
echo "<label for='current_num'>Enter the Question Number to be Changed: <label>";
echo "<input type='text' name='current_num' size='4'><p>";
echo "<label for='store'>Select New Store/Department: <label>";
echo "<select name='store'>";
echo "<option>Please Select</option>";
echo "<option value='Albany'>Sales (Albany Store)</option>";
echo "<option value='Saratoga'>Sales (Saratoga Store)</option>";
echo "<option value='Web Sales'>Sales (TaftFurniture.com)</option>";
echo "<option value='Financing'>Financing</option>";
echo "<option value='Customer Service'>Customer Service</option>";
echo "<option value='Delivery'>Delivery</option>";
echo "<option value='HR'>Human Resources</option>";
echo "<option value='Web Contact'>Website Comment</option>";
echo "<input type='submit' value='Change' id='dropdown'>";
echo "</select></form></div>";
}
Thanks in advance.
Your labels are not closing properly:
echo "<label for='mod_num'>MOD Initials: <label>";
Should be:
echo "<label for='mod_num'>MOD Initials: </label>";
Also, in the second example, you have an input inside the select. The input must be outside:
echo "<option value='Web Contact'>Website Comment</option>";
echo "<input type='submit' value='Change' id='dropdown'>";
echo "</select></form></div>";
Should be:
echo "<option value='Web Contact'>Website Comment</option>";
echo "</select>";
echo "<input type='submit' value='Change' id='dropdown'></form></div>";
And another one, you're not closing your P tags:
echo "<input type='text' name='mod_num' size='2' maxlength='4'><p>";
Should be:
echo "<p><input type='text' name='mod_num' size='2' maxlength='4'></p>";
Try to be more careful with your tags. Some browsers are more forgiving about malformed HTML, but others are not.
I have been using variable in the name part of the input tag. Now while access answers as selected by users using $_post,It gives error as undefined index.Tell me how to get answers of all questions as selected .
echo "<form method=\"post\" action=\"\">";
$query=mysql_query("select q_detail,q_id from question where category=\"$value2\"",$connection);
if(!$query)
{
echo mysql_error().'query failed';
}
$ans=1;
while($value1=mysql_fetch_array($query))
{
echo "Q-$i"." ";
echo $value1['q_detail']."<br />";
$i++;
$qno=$value1['q_id'];
$query1=mysql_query("select * from answer where ans_id=$qno");
if(!$query1)
{
echo mysql_error().'query failed';
}
while($value2=mysql_fetch_array($query1))
{
$opt=$value2['option1'];
$opt1=$value2['option2'];
$opt2=$value2['option3'];
$opt3=$value2['correct'];
echo "<input type=\"radio\" value=\"$opt\" name=\"$ans\">";
echo "<span class=\"margin\">$opt</span>";
echo "<input type=\"radio\" value=\"$opt1\" name=\"$ans\">";
echo $opt1." ";
echo "<input type=\"radio\" value=\"$opt2\" name=\"$ans\">";
echo $opt2." ";
echo "<input type=\"radio\" value=\"$opt3\" name=\"$ans\">";
echo $opt3." <br /><br />";
$ans++;
}
}
echo"<input type=\"submit\" name=\"submit\" value=\"submit\">";
echo "</form>";
You can make the name of input as array, e.g.:
<input type="radio" value="Blah" name="answers[]" />
and in php code you can access this using the following code:
foreach($_POST['answers[]'] as $answer)
{
echo $answer;
}
Enclose the non-numeric value with single quotes.
$query=mysql_query("select q_detail,q_id
from question where category='$value2'");
Try this:
$ans=1;
if(isset($_POST))
{
echo $_POST['radio_' . $ans];
}
echo "<form method=\"post\" action=\"\">";
$query=mysql_query("select q_detail,q_id from question where category=\"$value2\"",$connection);
if(!$query)
{
echo mysql_error().'query failed';
}
while($value1=mysql_fetch_array($query))
{
echo "Q-$i"." ";
echo $value1['q_detail']."<br />";
$i++;
$qno=$value1['q_id'];
$query1=mysql_query("select * from answer where ans_id=$qno");
if(!$query1)
{
echo mysql_error().'query failed';
}
while($value2=mysql_fetch_array($query1))
{
$opt=$value2['option1'];
$opt1=$value2['option2'];
$opt2=$value2['option3'];
$opt3=$value2['correct'];
echo "<input type=\"radio\" value=\"$opt\" name=\"radio_$ans\">";
echo "<span class=\"margin\">$opt</span>";
echo "<input type=\"radio\" value=\"$opt1\" name=\"radio_$ans\">";
echo $opt1." ";
echo "<input type=\"radio\" value=\"$opt2\" name=\"radio_$ans\">";
echo $opt2." ";
echo "<input type=\"radio\" value=\"$opt3\" name=\"radio_$ans\">";
echo $opt3." <br /><br />";
$ans++;
}
}
echo"<input type=\"submit\" name=\"submit\" value=\"submit\">";
echo "</form>";
I've added the if(isset($_POST)) and before the name I've added some text, because a name as a integer, can be used as a integer index in PHP. Array's can be called like
$ar = array("Name" => "VALUE");
echo $ar[0]; // Outputs VALUE
echo $ar["Name"]; // Outputs VALUE
Will both output "VALUE". So what you're trying to do. Is getting the value of index 1 instead of key 1.
You have to use $_POST['actual radiobutton name'], i. e. whatever the content of $ans is at the time of the echoing.
You can also debug your form after submitting by using this code:
echo "<pre>";
print_r($_POST);
echo "</pre>";
This will show you the contents of the $_POST data so you can see if values are being passed correctly.