Need Transfer Value PHP - php

I am creating a new website. For that website I will need to transfer a quantity and amount value from one if condition statement into another if condition statement. Both if statements are accessed by separate submit buttons named "checkamt" & "buy".
I need to transfer the "quantity", "value", and "net" values to and from the checkamt if statement to the buy if statement.
Here's my code:
<form action="index.php" method="post">
<input type="submit" name="checkamt" value="Check Amount"/>
<input type="submit" name="buy" value="Buy"/>
</form>
<?php
if(isset($_POST[checkamt]))
{
$qun=1;
$val=5000;
$total=$qun*$val;
}
if(isset($_POST[buy]))
{
echo $qun;
echo $val;
echo $total;
}
?>

I think that the problem you're having is that variables don't persist on page change. If you want that, you'll need to use a session. First, you must call session_start before anything, including HTML, is sent to the user. Then, you can use the $_SESSION variable.
<?php
session_start();
?>
<form action="index.php" method="post">
<input type="submit" name="checkamt" value="Check Amount"/>
<input type="submit" name="buy" value="Buy"/>
</form>
<?php
if(isset($_POST[checkamt]))
{
$_SESSION['qun']=1;
$_SESSION['val']=5000;
$_SESSION['total']=$qun*$val;
}
if(isset($_POST[buy]))
{
echo $_SESSION['qun'];
echo $_SESSION['val'];
echo $_SESSION['total'];
}
?>

Improve your English! Not sure if this is what you want, but if you want to share the values of your variables between the two ifs? You have to declare them at a higher scope than your if:
<?php
$qun = 0;
$val = 0;
$total = 0;
if(isset($_POST[checkamt]))
{
$qun=1;
$val=5000;
$total=$qun*$val;
}
if(isset($_POST[buy]))
{
echo $qun;
echo $val;
echo $total;
}
?>

Not sure if I'm getting the question right, but why not do something like this :
if(isset($_POST[checkamt]) || isset($_POST[buy]))
{
$qun=1;
$val=5000;
$total=$qun*$val;
echo $qun;
echo $val;
echo $total;
}

You need to track your variables between the different forms. You can use SESSION like Xeon06 suggested, or do the following. I'm only showing for $qun:
<?php
if(isset($_POST['checkamt'])) {
$qun=1;
}
if(isset($_POST['buy'])) {
echo $qun;
}
?>
<form action="index.php" method="post">
<input type="hidden" name="qun" value="<?php echo $qun; ?>" />
<input type="submit" name="checkamt" value="Check Amount"/>
<input type="submit" name="buy" value="Buy"/>
</form>

Related

Each time I press submit I want input text box change text

So basically I have a simple form and I want to change the input boxes text different every time you press submit by using PHP. I know my code doesn't work but I don't understand how to solve this problem. Maybe the array is not the best way to do this?
<body>
<?php
if(isset($_GET['submit'])) exit();
$msg= ['One', 'Two', 'Three', '']
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>">
<input type="submit" name="submit" value="Paina nappi">
<input type="text" name="msg" value="<?php echo (isset($msg)) ? $viesti : ''; ?>">
</form>
</body>
You could write something like this:
<?php
session_start();
$msgArray= array('One', 'Two', 'Three', '');
// if $_SESSION['msgIndex'] is not set, we initialize it, or it will take 0 value every loading page
if (!isset($_SESSION['msgIndex'])) { $_SESSION['msgIndex'] = 0; }
if (isset($_GET['submit'])) {
getMessage();
}
// We save msgIndex in a $_SESSION variable cause even if the user reload the page, we keep the value
function getMessage() {
if ($_SESSION['msgIndex'] >= 0) {
$_SESSION['msgIndex'] += 1;
} if ($_SESSION['msgIndex'] > 3) {
$_SESSION['msgIndex'] = 0;
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>">
<input type="submit" name="submit" value="Paina nappi">
<input type="text" name="msg" value="<?= $msgArray[$_SESSION['msgIndex']] ?>">
</form>

Trying PHP IF Else Statements for the first time

I'm learning PHP and trying to understand the if .. else statements a little better, so I'm creating a little quiz. However, I have come across an issue and I don't seem to know what the issue is. My problem is that whenever I type in the age in the input area, it will give me the $yes variable every time even if I enter the wrong age.
Here is my code so far:
My html file:
<form action="questions.php" method="post">
<p>How old is Kenny?<input></input>
<input type="submit" name="age" value="Submit"/>
</p></form>
My php file:
<?php
$age = 25;
$yes = "Awesome! Congrats!";
$no = "haha try again";
if ($age == 25){
echo "$yes";
}else{
echo "$no";
}
?>
You catch the user input inside the $_POST superglobal var (because the method of your form is POST.
So
<?php
$age = 25;
should be
<?php
$age = $_POST['age'];
There is an error in HTML too. This
<input type="submit" name="age" value="Submit"/>
should be
<input type="text" name="age" value=""/>
<input type="submit" value="Click to submit"/>
Because you want one input and one button. So one html element for each element.
and <input></input> must be cleared because it's not valid syntax :-)
<form action="questions.php" method="post">
<p>How old is Kenny?</p><input type="text" name="age"></input>
<input type="submit" value="Submit"/>
</form>
$age = (int) $_POST["age"];
$yes = "Awesome! Congrats!";
$no = "haha try again";
if ($age == 25) {
echo $yes;
} else {
echo $no;
}
<?php
/* Test that the request is made via POST and that the age has been submitted too */
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['age'] ) ){
/*
ensure the age is an integer rather than a string ..
though for this not overly important
*/
$age=intval( $_POST['age'] );
if( $age==25 ) echo "Congratulations";
else echo "Bad luck!";
}
?>
<form action="questions.php" method="post">
<p>How old is Kenny?
<input type='text' name='age' placeholder='eg: 16' />
<input type="submit" value="Submit" />
</p>
</form>
A simple html form, note that the submit button does not carry the values you want to process, they are supplied via the input text element.
First of all, you need to echo the variable; echoing "$no" will keep it as a string. Remove the quotes from "$no" and "$yes" in your if then statement. Otherwise, your code seems sound!

Session variable not working

I am using one session variable in my php page. As per my infomation, it is accessible throughout the program and it is, but problem is that it is showing different value for the same variable at different place in php page?
the code is as follows
<html><body>
<?php session_start();
if(!isset($_SESSION['x']))
$_SESSION['x']=1;
echo "X=". $_SESSION['x'];
?>
<form>
<input type="submit" name="save" value="save" />
</form>
<?php
if (isset($_GET['save']))
{
if(isset($_SESSION['x']))
$_SESSION['x'] = $_SESSION['x']+1;
echo $_SESSION['x']."<br>";
}
else
echo "no submit";
?>
</body></html>
value becomes different before and after submit button click? Please tell me why it is so?
thanks in advavnce.
You are redeclaring the value of session variable 'x' here
$_SESSION['x'] = $_SESSION['x']+1;
This is why its appearing 1 greater than its initial value.
it is due to the code itself
if(isset($_SESSION['x'])) //It is set
$_SESSION['x'] = $_SESSION['x']+1; //Add 1 to the value
echo $_SESSION['x']."<br>"; return value with +1
Solution
The reason the output is different is the order you echo and update
//Echo
//Update Value
//Echo again
Simple solution would be to move this
if (isset($_GET['save']))
{
if(isset($_SESSION['x']))
$_SESSION['x'] = $_SESSION['x']+1;
echo $_SESSION['x']."<br>";
}
else
echo "no submit";
to above this
if(!isset($_SESSION['x']))
$_SESSION['x']=1;
echo "X=". $_SESSION['x'];
Also note set the method and the action in the form to make sure it calls itself
<form method="GET" action="[url to itself]">
<input type="submit" name="save" value="save" />
</form>
Do it like this :
<html><body>
<?php session_start();
if(!isset($_SESSION['x']))
$_SESSION['x']=1;
echo "X=". $_SESSION['x'];
?>
<form method="GET" action="">
<input type="submit" name="save" value="save" />
</form>
<?php
if (isset($_GET['save']))
{
if(isset($_SESSION['x']))
echo $_SESSION['x']."<br>";
}
else
echo "no submit";
?>
</body></html>
this way the code prints out the same value after submit as it did before.
Either way you try if you print value and change after or change value and print after, when page reloads it will change value. you could add another button called increment and add the following code inside the php :
if (isset($_GET['inc']))
{
if(isset($_SESSION['x']))
$_SESSION['x'] = $_SESSION['x']+1;
}
and this one inside the form:
<input type="submit" name="inc" value="inc" />
this way youre variable increment when you press the inc button

incrementing inside a php function

I was wondering if you can help me with my php function
I have created a quiz which uses radio buttons and a submit button. I want the function to check if a specific radio button is selected and if so add 1 onto the user score. however nothing is being added the function for this is
function Score()
{
if(isset($_POST['correctAnswer'] ))
{
$answer=$_POST['correctAnswer'];
$_SESSION['score']=$userScore+1;
}
else
{
$_SESSION['score']=$_SESSION['score'];
}
}
and the form in which it is submitted is
echo '<strong>'."$theQuestion".'</strong><br>';
?> <form name="correctAnswer" form method="post" action="quiz.php" onSubmit="Score()">
<?php
echo "$theAnswer1";?> <input type="radio" id="correct_answer" name="correctAnswer">
<?php
echo "<br>$theAnswer2"; ?> <input type="radio" id="wrong_answer1" name="wrongAnswer1">
<?php
echo "<br>$theAnswer3"; ?> <input type="radio" id="wrong_answer2" name="wrongAnswer2">
<?php
echo "<br>$theAnswer4"; ?> <input type="radio" id="wrong_answer3" name="wrongAnswer3">
<input type="hidden" name="score" value="userScore">
<br><input type="submit" value="Submit Answer">
</form>
Hope you can help
You have to pass the counter as an argument of the function like this
function Score($userScore){
//Do the rest
}
Looks like the $_SESSION['score'] is undefined
function Score() {
if (!isset($_SESSION['score'])) {
$_SESSION['score'] = 0;
}
if (isset($_POST['correctAnswer'])) {
$_SESSION['score']++;
}
}

Multiple Submits on a page

I have this PHP code:
$result = mysql_query("SELECT distinct om_quote_no from `porders` order by om_quote_no desc") or die(mysql_error());
echo '<select name="project_no">';
while ($row = mysql_fetch_array($result)) {
echo "<option value=".$row['om_quote_no'].">".$row['om_quote_no']."</option>";
}
echo '</select>';
?>
<input type="submit" value="Submit" />
</form>
<div id="table">
<?php
if($_GET){
So as you can see the PHP is forming a dropdown input and once submitted its going to execute some code, but what i need to do is have two dropdown inputs and therefore two submit buttons, however i'm not sure how to form the PHP if statement to distinguish which submit was pressed, so i'll have(pseudo):
if (submit1){
}
if (submit2){
}
Is that possible?
If you give your <input type="submit"> elements names, the one that is clicked will have its name and value sent to the server.
<input type="submit" value="Submit" name="submit1">
if clicked will send submit1=Submit to the server. You could therefore check with if ($_GET['submit1']) to see if it was pressed.
This isn't the best way but can do something like this too:
<select name="test" onchange="document.location ='test.php?submit=dropdown1'">
<option>test</option>
<option>test1</option>
</select>
<br><br>
<select name="test1" onchange="document.location ='test.php?submit=dropdown2'">
<option>test2</option>
<option>test3</option>
</select>
within test.php file:
if($_GET['submit'] == 'dropdown1')
{
print "One";
//statements to execute
}elseif($_GET['submit'] == 'dropdown2'){
//statements here to execute
print "Two";
}
<input type="submit" value="Submit 1" name="submit1"/>
<input type="submit" value="Submit 2" name="submit2"/>
<?php
if(isset($_POST['submit1'])){
//do stuff
//grab select option
$select_option=$_POST['project_no'];
}else if(isset($_POST['submit2'])){
//do stuff
}else{
//do stuff
}
?>
Or if your method is GET than change $_POST to $_GET :)

Categories