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

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>

Related

How to submit the same form multiple times on same page and also process them on that page in php

I am trying to display a form and process data on same page multiple times.
This is my code:
<?php
function display($q) {
$que=mysqli_fetch_row($q);
$_SESSION['qid']=$que[0];
ob_start ();
?>
<form method="post" action="">
<h3> <?php echo $que[0]." . ".$que[2]; ?> </h3>
<input type="radio" name="ans" value="<?php echo $que[3]; ?>"> <?php echo $que[3]; ?> <br/>
<input type="radio" name="ans" value="<?php echo $que[4]; ?>"> <?php echo $que[4]; ?> <br/>
<input type="submit" name="next" value="next" >
</form>
<?php
$_SESSION['qid']=$que[0];
echo $_SESSION['qid'];
if(isset($_POST['next'])) {
if(array_key_exists('next',$_POST)){
next_que();
}
}
}
function next_que() {
//some code
// calling display function
}
?>
When I run this code, the display and next_que functions are working properly the first time, but on the next call only the display function is called displaying the form, and clicking the button doesn't turn the isset($_POST['next']) condition to true.
How can I get this to work?
Yes, prevent the form from submit via prevent default in your javascript. Then you can call your javascript functions / code on any button click and use the data in the same page.

Grabbing another form element and store as variable

Sorry for what is probably quite an easy question, but I'm trying to pick up some info from my php form:
This is my current code which works for the post name, but what if I want to also grab the colours boxes that were selected too?
main.php
<?php
if (isset($_POST['submit'])) {
$data = $_POST['name']; // the data from the form input.
}
$colour_array = [
"red" => "#9E2A2B",
"blue" => "#3E5C76",
"green" => "#335C67",
];
?>
...
<form action="/" method="post">
<input type="text" name="name" placeholder="Acme Corp"/>
<input name="colour" type="checkbox" value="red">Red<br>
<input name="colour" type="checkbox" value="blue">Blue<br>
<input name="colour" type="checkbox" value="green">Green<br>
<input type="submit" name="submit" value="Submit">
</form>
<img src="pngfile.php?data=<?php print urlencode($data);?>"
alt="png php file">
I guess I confused because currently it is calling this:
pngfile.php
<?php
require_once 'functions.php'; // Requires and includes do not need brackets.
$inputData = urldecode($_GET['data']);
process($inputData);
exit;
?>
Which calls functions.php
<?php
function process($inputdata)
{
...
The issue is I want to be able to separate out the values from the form, so I get both the input box and the colour selection in separate variables.
EDIT: What I have tried:
main.php
$data = $_POST['name'] && $_GET['colour']
functions.php
process($inputdata, $colours)
But I'm not really sure where to go from there.
In HTML, change name="colour" to name="colour[]"
To access the value of colour in php do something like below
<?php
if(!empty($_POST['colour'])) {
foreach($_POST['colour'] as $check) {
echo $check; //echoes the value set in the HTML form for each checked checkbox.
}
}
?>

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!

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']++;
}
}

Need Transfer Value 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>

Categories