I want to have 2 input fields and a button. Once you press the button it adds the two numbers together and shows the result below. The input numbers have to be between 0 and 10. If one of the inputs is 0, the sum has to be 0 no matter what.
Here's what I have so far:
<form action="" method="post">
<label>Enter Num1:</label>
<input type="text" name="num1" /><br>
<label>Enter Num2:</label>
<input type="text" name="num2" /><br><br>
<input type="submit" name="btn_submit" value="Add">
</form>
<?php
if(isset($_POST['btn_submit']))
{
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$total = $num1+$num2;
echo "The total value is: ".$total;
}
?>
The first problem is that when I press the 'Add' button, my input fields are cleared and nothing happens. The second problem is that I have no clue on how to add the 2 mentioned conditions. Can anyone help me out?
The first problem is that when I press the 'Add' button, my input fields are cleared and nothing happens
This how it works. If you want your <input>s to be pre-filled with any values, you must do that yourself. There's value argument for any <input> you can use.
The second problem is that I have no clue on how to add the 2 mentioned conditions.
there's no additional conditions clear in your question. You just added two values in proper way, so it's unclear what you are having problem with here.
Related
** Here are the Codes that i use**
** I use this for recording of the scores, there 5 zeros cause there are more 3 students
$Students_C1= array(0,0,0,0,0);
$Students_C2= array(0,0,0,0,0);
** These are for inputting numbers
<td>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" id="inputs" name="SampleUP" value="<?php $Students_C1[0]; ?>" placeholder="<?php echo $Students_C1[0] ?>">
<input type="hidden" name="UpdateC1" >
</form>
</td>
<td>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" id="inputs" name="SampleUPs" value="<?php $Students_C2[0]; ?>" placeholder="<?php echo $Students_C1[1] ?>">
<input type="hidden" name="UpdateC2" >
</form>
** And these are for updating the php array after i input those numbers
<?php
if( isset($_GET['UpdateC1']) )
{
$val1 = htmlentities($_GET['SampleUP']);
$Students_C1[0]=$val1;
}
else{}
if( isset($_GET['UpdateC2']) )
{
$val2 = htmlentities($_GET['SampleUPs']);
$Students_C2[0]=$val2;
}
else{}
?>
The flow should be if i input a number in C1 and C2 next to the student's name. that number should stay as i input. The problems is that after i input C2 number (After C1), both of the numbers reset to zero, but the C1 works fine
Any Potential solution is heartly welcomed. A complete beginner in PHP
Student Grading System
Okay, I can't see your output but firstly both your input IDs are the same name. Then, you do not have an input type as submit instead it is hidden. So how do you submit your form then? If you do not submit it, then there is no value passing through the GET request. Your form does not have a method as well. Next is you're having two forms on one page so I'm guessing that if you have two forms then u need to enter one form and submit it first before submitting the other? I guess that's the reason why only one of it is updated. You should include your webpage output as well to make it easier to debug.
I've been trying to build a program using PHP cookies. The user interface basically looks like this.
My basic program
The logic is this. When the user enters the product name, price and quantity and clicks on the "Add" button, the total value which is price*quantity will fall on the "Total bill value" textbox. The user can go on adding more and more values for "Price" and "Quantity" and each time he clicks on "Add", all the values get added. So this should be done by using cookies. The "Clear" button will reset the cookie, and the "Print" button will also be using the same cookie to print out everything the user has entered upto that point.
Anyways I just started coding, and I'm already stuck. I'm not sure what my mistake is. When I click on the "Add" button, the value doesn't get added every time I enter a price and quantity. Instead the page refreshes to give only the product of the price and quantity entered at one time. This is my code.
`
<form method="POST" >
<ul style="list-style-type:none;">
<li>
Product Name: <input type="text" name="ProductName"> <br>
Price: <input type="number" name="Price" value="price"> <br>
Quantity: <input type="number" name="Quantity" value="qty"><br>
<input type="submit" name="btnClick" value="Add">
<input type="submit" name="btnReset" value="Clear">
<input type="submit" name="btnPrint" value="Print"><br>
Total Bill Value: <input type="text" name="Bill">
</li>
</ul>
</form>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$result=0;
if(isset($_POST["btnReset"]))
{
setCookie("result", $result);
}
elseif(isset($_POST["btnClick"]))
{
if(isset($_COOKIE["result"]))
{
$result=$_COOKIE["result"];
}
$result= $_POST["Price"]*$_POST["Quantity"];
echo $result;
setCookie("result", $result);
}
else
{
echo "Product: ".$_POST["ProductName"];
echo "Price: ".$_POST["Price"];
echo "Quantity: ".$_POST["Quantity"];
}
}
?>
</body>
`
I'm sure that this is a stupid mistake I'm struggling on. However I am an absolute beginner and would so appreciate if somebody could point out my mistake and explain it to me.
So there are two things I want to know mainly:
Why is my cookie not being set when the Add button is clicked?
How to put the value calculated in PHP back to the html form, so that the "Total Bill Value" textbox will have the total value?
Thanks in advance!
Edit: I did figure out my mistake. I should have put $result=$result + $_POST["Price"]*$_POST["Quantity"];
Then the values get added up.
However would like to know how to put the total bill value calculated in PHP back to the HTML form
You should call setcookie() before you output any HTML, so move the PHP code above the rest.
To set the form input:
<?php
if (isset($result)) {
$bill = $result;
} else {
$bill = '';
}
?>
Total Bill Value: <input type="text" name="Bill" value="<?php echo $bill; ?>">
please use setcookie instead of setCookie
This will work
Happy Coding
I have a form with few input fields and a update option ,suppose if i have 10 fields and i update only first two fields and not the rest who can i display a message that only the first to field A and B are updated using PHP and Mysqli
in the screenshot if i update the value of bill id and origin i should display message out in some other page that says that bill id and origin has been updated with "xyz " value
One way is to fetch fields with real and old name i.e.
Select name,name as old_name from table
In html
<input type="text" name="name" value="name from db">
<input type="hidden" name="old_name" value="old_name from db">
After submit form you can check it by.
And check after post.
If(post['name']==post['old_name'])
Something like this.
Another way is from js by checking onchange event
well it'll be something like this ..
html:
<html>
<body>
//action is defining the php file's name which the form will be sent to ;
//method is defining the method which the form will be sent with
<form action="updateCheck.php" method="post">
<input type="text" name="input-filed-1"/>
<input type="text" name="input-filed-2"/>
<input type="text" name="input-filed-3"/>
<input type="text" name="input-filed-4"/>
<input type"text" name="input-filed-5"/>
<input type="submit" value="submit/>
</form>
</body>
</html>
create a file named updateCheck.php in the same directory and enter the following code
<?php $updateMessage = "you have updated the following fields";
for($x = 0 ; $x<=5 ; $x++)
{
// trim for erasing the whitespaces
// that if clause checks if there was data entered in the update input fields
if(isset($_POST["input-field-".$x]) && trim($_POST["input-field-".$x]))
{
$updateMessage.= "input-field-".$x ;
}
}
echo "<script type='text/javascript'>alert('".$updateMessage."')</script>"
?>
I haven't tested the code .. but study it carefully and you'll see how it's done
I want to do an equation using the values I get from the input boxes called ampMin, voltMin and hastMin...
I'm not sure if it's a syntax problem or my method of approach is plain wrong.. Here's is an example of how the equation should look and work like with Excel.
What am I doing wrong? Thank you for your time!
EDIT: worth mentioning is that the whole block of code is within "strckenergi.php".
<html>
<title>Sträckenergi</title>
<body>
<h3>Svetsmetod: 111</h3>
<h4><i>Med K=0.8</i></h4>
<pre>
<form method="post" action="strckenergi.php">
Amp. Min <input type="text" name="ampMin"> Volt. Min <input type ="text" name="voltMin"> Hast. Min <input type="text" name="hastMin"> </pre>
<?php
echo "kJ/mm (minimum) = " . $qMin
$qMin = ( $ampMin * $voltMin ) / (( $hastMin * 1000 ) / $hastMin * 0.8));
?>
</body>
</html>
This works.
<html>
<title>Sträckenergi</title>
<body>
<h3>Svetsmetod: 111</h3>
<h4><i>Med K=0.8</i></h4>
<pre>
<form method="post" action="form.php">
Amp. Min <input type="text" name="ampMin"> Volt. Min <input type ="text" name="voltMin"> Hast. Min <input type="text" name="hastMin">
<input type="submit" value="submit"> </pre>
</form>
<?php
if($_POST){
$voltMin = $_POST['voltMin'];
$ampMin = $_POST['ampMin'];
$hastMin = $_POST['hastMin'];
$qMin = ( $ampMin * $voltMin ) / ( $hastMin * 1000 ) / $hastMin * 0.8;
echo "kJ/mm (minimum) = " . $qMin;
}
?>
You don't appear to be using forms correctly. If you want the browser do be able to display it right away, you should use JavaScript instead. PHP will require an additional pageload, or an AJAX request.
And in order to post the form, you need a submit button. Otherwise, the browser won't know what to do with it.
Further, your first PHP line needs a semi-colon, and your second one needs to be above the first - otherwise, the interpreter won't know what your value is when printing it, because it hasn't been calculated yet.
To be honest, I think you need to start by googling for how to construct an HTML form, then you can look up simple JavaScripts. Lycka till!
First off, as Joel Hinz says above, you need a submit button so the page knows when the user is done inputting and wants to send the form to the server for processing.
Second, you need to close the form with a tag.
Third, you're probably better off sticking with php at this stage; JavaScript can be a bit tricky and mighty frustrating for beginners.
This is a rough approximation of how your form should look.
<form method="post" action="strckenergi.php">
Amp. Min <input type="text" name="ampMin">
Volt. Min <input type ="text" name="voltMin">
Hast. Min <input type="text" name="hastMin">
<input type="submit" value="submit">
</form>
See http://www.tizag.com/phpT/forms.php for a clear explanation of forms.
OK first thing first. When the form is submitted, the values contained in the $_POST array aren't immediately available to the script on the server which processes the input.
For that you will need something like the following:
<?php
if($_POST){// this checks for the existence of the $_POST array, i.e. was something submitted
//now we're assuming a form was submitted
$voltMin = $_POST['voltMin'];
$ampMin = $_POST['ampMin'];
$hastMin = $_POST['hastMin'];
$qMin = ( ($ampMin*$voltMin)/($hastMin*1000)/($hastMin*0.8) );
echo "kJ/mm (minimum) = " . $qMin;
}// if($_POST)...
?>
Then you can process the elements, and print out the results of your calculations.
Oh, and drop the pre tags unless you really need them.
I have a site in which an Admin User looks at rows of invoices which have been submitted by users, when they click on an 'Approve Invoice' button from one of these rows it will take them on the page below.
Once the Admin User approves this invoice, they hit the 'yes' radio button and submit at the bottom of the page which enters the value 'AUDITED' under the 'npc_active' column in that row. It then multiplies the quantity and points and inserts the total onto a new row in 'tally_points' (along with their user id and sales id). This is all working fine, but...
What I am trying to do, however, is make a condition in which once the sale is audited, that it can't be re-audited. ie the 'This invoice has been audited' print should show once the submission has taken place, but it isn't working.
I'm close but can't seem to figure out what the problem is. The code in which I think I am having the problem is below, the full page code is at the bottom of this post.
$str ='<form method="post" action="audit_invoice.php">
<font style="font-size:11px;">
<em>Is this invoice approved?<br />';
if($approved == "AUDITED") {
$str .='Please select carefully as this action cannot be undone.</em>
<em>Yes:</em><input type="radio" value="AUDITED" name="npc_active"> <em>No:</em>
<input type="radio" value=" " name="npc_active">
<input type="submit" name="submit" value="Submit" />
<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name="id" value="' . $id . '" />
</font>
</form></tr>';
}
else {
$str .='This invoice has been audited';
}
echo $str;
If I put the '==' before "AUDITED" it will show up with the echo 'The invoice has been audited' in each instance, if I put '=' in front of "AUDITED" it will show the yes button and submit button in each instance.
#AdamMC the = operator is only used when assigning data.
You are comparing a data, therefore you are correct when using ==
if($approved == "AUDITED")
I would like to request what exactly your $approve variable contains.
As of right now I can only make an assumption that this code implies
if invoice is approved it would equal audited which then would trigger it to echo "this invoice has been audited"
To stackoverflow users: please do not downvote, I cannot comment because my reputation does not permit it. Just trying to help