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
Related
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.
I am trying to do a find and replace applicaiton the problem is that after cliked submit button all the text fields gets clean nothing displays on the screen What am i doing wrong
<?php
$offset=0;
if(isset($_POST['text'] ) && isset($_POST['searchfor']) && isset($_POST['replacewith'])){
$text=$_POST['text'];
$search=$_POST['searchfor'];
$replace=$_POST['replacewith'];
$searchLength=strlen($search);
if(!empty($text) && !empty($search) &&!empty($replace)){
while ($strpos= strpos($text,$search,$offset)){
echo $offset=$strpos+$searchLength;
}
} else {
echo "<script>alert('errorrrr')</script>";
}
}
?>
<form action="#" method="post">
<textarea name="text" id="" cols="30" rows="10"></textarea><br>
Search For:<br>
<input type="text" name="searchfor"><br>
ReplaceWith<br>
<input type="text"name="replacewith"><br>
<input type="submit" value="Fr..."></>
</form>
Regarding your form, you decided to submit to the same page.
Doing this, the page is obviously fully reloaded when submitted. Hence it is normal that what you typed in has disappeared.
If you want to see it again, you have to display you variables in the HTML code.
For example:
<?php
$myVar = "";
if(isset($_POST['myVar']){
$myVar = $_POST['myVar'];
}
?>
<form>
<input type="text" value="<?php echo $myVar;?>"/>
</form>
NB: I encourage you to filter the user entry.
Regards
there is problems in your code :
1 - echo $offset=$strpos+$searchLength; the echo can't be used in this format. insted use echo $offset; in next line for seeing offset values.
2 - if the text be like 'amir love persepolis' and search for 'amir' to replace it with 'all men's' you will have another issue, because you will have while ( 0 ) situation. think about this too!
i have a form in html that when the certain fields are clicked i want it to add up the values in the php part, so i thought that if i put the php part in the value area of the html then it would add it up, but it adds them all up but not the specific ones when clicked, Please help:
<p>
<input type="checkbox" name="one" value= <?php $number = 2.39; ?>
<label for="one">Four 100-watt light bulbs for $2.39</label>
<p>
$total = $number + $numberone + $numbertwo + $numberthree;
echo "Total cost is " .$total;
echo $card;
?>
</form>
First, you need to echo out the number value in your form, not just assign the variable, nothing will be rendered there. Also, I find that if you're adding numbers for totals, it's easier to name them in a fashion that you can pull the values back out in an array
name="one"
would become
name="price[]"
or something of the like.
<p>
<input type="checkbox" name="price[]" value= <?php echo('2.39'); ?>
<label for="one">Four 100-watt light bulbs for $2.39</label>
</p>
Then on the server side, just sum the totals
$total = 0;
foreach($_POST['price'] AS $value){
$total += $value;
}
You'll want to do some input sanitization, but this example should get you going.
This example will only work on the server-side after a form submission, if you want to add them up in realtime on the page, you'd have to use ajax to add the values up.
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
I am just trying to learn PHP and want to get the value of the textbox using $_post function, but its not working. I am using wamp 2.1 and the code is simple as below
<form method="POST" action="c:/wamp/www/test/try.php">
<input type="text" name="nco" size="1" maxlength="1" tabindex="1" value="2">
<input
tabindex="2" name="submitnoofcompanies" value="GO"
type="submit">
</form>
<?php
if (!isset($_POST['nco']))
{
$_POST['nco'] = "undefine";
}
$no=$_POST['nco'];
print($no);
However in no way I get the value of the textbox printed, it just prints undefined, please help me out.
You first assigned the word "undefine" to the variable $_POST['nco'].
You then assigned the value of the variable $_POST['nco'] (still "undefine" as you stored there) to the variable $no.
You then printed the value stored in the variable $no.
It should be clear that this will always print the word "undefine".
If you want to print the value of the textbox with the name nco, fill out the form with that textbox, and in the page that process the form,
echo $_POST['nco'];
...is all you do.
You need to setup a form or something similar in order to set the $_POST variables. See this short tutorial to see how it works. If you click the submit button, your $_POST variables will be set.
what for you are using this line $_POST['nco'] = "undefine"; } ..?
and please cross check whether you are using form method as post and make sure that your text name is nco ... or else use the below code it will work.
<?php
$no = $_POST['nco'];
echo $no;
?>
<form name='na' method='post' action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type='text' name='nco'>
</form>
thanks
Your action is wrong.
Change it to
action="try.php"