I'd like that the variable $order is increase by 1 every time I push the submit button.
(The name of the page is study.php so every time I push the button the page is refreshed):
<?php
$order = $_GET['number'];
echo "<form action='study.php' method='GET'>
<input type='hidden' name='number' value='$order++' />
<input class='big_b' type='submit' value='next' />
</form>";
echo "$order";
?>
The first time 1 push the button $order is 1, the second 2, the third is 3 ... etc
Thanks for your help!
EDIT: SOLVED
<?php
session_start();
if(empty($_SESSION['count'])) $_SESSION['count'] = 0;
$order = $_SESSION['count']+1;
$_SESSION['count'] = $order;
echo "<form action='study.php' method='GET'>
<input class='big_b' type='submit' value='next' />
</form>";
echo "$order";
?>
How you currently have it, is that it will increment on each page refresh regardless whether button was clicked or not, do you need it only to increment if the button is pressed?
<?php
session_start();
// Reset to 1
if(isset($_POST['reset'])){unset($_SESSION['number']);}
// Set or increment session number only if button is clicked.
if(empty($_SESSION['number'])){
$_SESSION['number']=1;
}elseif(isset($_POST['next'])){
$_SESSION['number']++;
}
echo '
<form action="" method="POST">
<input class="big_b" type="submit" name="next" value="Next" />
<input type="submit" name="reset" value="Reset" />
</form>';
echo $_SESSION['number'];
?>
<?php
session_start();
if(empty($_SESSION['order'])){
$_SESSION['order'] = 1;
}
echo "<form action='study.php' method='GET'>
<input type='hidden' name='number' value='".$_SESSION['order']++."' />
<input class='big_b' type='submit' value='next' />
</form>";
echo $_SESSION['order'];
?>
You need to put $order++ outside of the quotation marks to make an operation (incrementing by 1). Here's the code:
<?php
$order = $_GET['number'];
echo "<form action='study.php' method='GET'>
<input type='hidden' name='number' value='".$order++."' />
<input class='big_b' type='submit' value='next' />
</form>";
echo "$order";
?>
Related
In my first page I have this code:
$number="1234567891";
$str="456";
echo "<form action='edit.php' method='POST'><input type='hidden' name='msg' value='$message' />
<input type='hidden' name='text' value='$number' />
<input type='hidden' name='edit' value='$str' />
<input type='submit' name='chedit' value='Go' style='position:relative; top:25px; left: 50%;'>
</form>";
In my edit.php I have this code:
<form action="#" method="POST">
Edit Number
<input type="text" name="change" value="$mumu"/>
<input type="submit" name="pch" value="Change"/>
</form>
<?php
if (isset($_POST["chedit"]))
{
$suj = $_POST["msg"];
$text = $_POST["text"];
$mumu =$_POST["edit"];
if(isset($_POST["pch"]))
{
$change = $_POST["change"];
$obinna = str_replace("$change","$mumu","$text");
echo $obinna;
}
}
?>
My problem is that whenever I put a new text in new form and click submit to edit a character in the old string submitted line the page refreshes and no result is output. Please can anybody sort this out?
// try this ..
if (isset($_POST["chedit"]))
{
$suj = $_POST["msg"];
$text = $_POST["text"];
$mumu =$_POST["edit"];
if(isset($_POST["pch"]))
{
$change = $_POST["change"];
//456 , //555(post value) , //(your text)12345678910
// $obinna = str_replace("Set old value you change in text","set new value you want to set in text ","your orignal text ");
$obinna = str_replace("$mumu","$change","$text");
echo $obinna;
}
}
echo '<form action="#" method="POST">
Edit Number
<input type="text" name="change" value="" placeholder="Change"/>
<input type="text" name="edit" value="" placeholder="Edit"/>
<input type="submit" name="pch" value="Submit"/>
</form>'
Check Demo Url :- https://eval.in/931366
So I'm currently working on a little project that allows a user to input a number into a textbox, after clicking a button that says "add" it should store that value into an array and then allow the user to input another value into that array. There is also a button on the page when the user is finished and wants to sum the values called "Submit". The problem I'm running into is everytime the form posts back, it recreates a new blank array. Any tips?
See the code below:
<html>
<head>
</head>
<body>
<h2>Please Select your title and name:</h2>
<form action='<?php echo $_SERVER["PHP_SELF"]; ?>' method='post'>
<p>
<label for="strFirstname">Type number to add: </label>
<input type='text' name='strNumber' id='strNumber'/>
</p>
<p>
<input type='submit' name='submit' value='Add' />
</p>
<p>
<input type='submit' name='calculate' value='Compute' />
</p>
<?php
$array = array();
if (isset($_POST['submit']))
$num = $_POST['strNumber'];
$array[] = $num;
foreach($array as $num)
echo $num . ' + ';
if(isset($_POST['calculate']))
foreach($array as $num)
echo $num . ' + ';
?>
</form>
</body>
</html>
<?php
session_start();
?>
<html>
<head>
</head>
<body>
<h2>Please Select your title and name:</h2>
<form action='' method='post'>
<p>
<label for="strFirstname">Type number to add: </label>
<input type='text' name='strNumber' id='strNumber'/>
</p>
<p>
<input type='submit' name='submit' value='Add' />
</p>
<p>
<input type='submit' name='calculate' value='Compute' />
<input type='submit' name='clear' value='clear' />
</p>
<?php
if (isset($_POST['submit'])) {
if(!array_key_exists("numbers", $_SESSION)) {
$_SESSION["numbers"] = array();
}
array_push($_SESSION["numbers"], $_POST["strNumber"]);
}
if(isset($_POST['clear'])) {
$_SESSION["numbers"] = array();
}
if(array_key_exists("numbers", $_SESSION)) {
echo implode("+", $_SESSION["numbers"]);
}
if(isset($_POST['calculate'])) {
if(array_key_exists("numbers", $_SESSION)) {
$expression = implode("+", $_SESSION["numbers"]);
eval( '$result = (' . $expression . ');' );
echo "=" . $result;
}
}
?>
</form>
</body>
</html>
Start a session
When the action is "submit"
Check if the session which will store the numbers is initialized
If it's not initialize it as an array
Finally push the number into the array
Check if there is a session initialized if there is print all the numbers ( you can use implode to do that)
if the action is calculate .. just make the calculation ( check eval function )
I'm quite new to php so this might seem very easy. I have a table called 'products' and for each product in that table I want to create a button with the id of that product. I have no problem creating the buttons but I can not see which of the buttons has been pressed.
How can I solve this?
This is my code:
$sql = "SELECT id FROM `products` WHERE subcategory = 'laptop' ORDER BY id desc limit 1";
$query = mysql_query($sql);
$id = mysql_result($query,0);
for($i=1; $i<= $id; $i++){
$product2 = R::load('products', $i);
echo "<input type='submit' name='$product2->id' value='Add to cart'/>";
}
Thank you !
assign a value to the button/input
<input type="submit" name="btn" value="button1" />
<input type="submit" name="btn" value="button2" />
<input type="submit" name="btn" value="button3" />
<?php echo filter_input(INPUT_POST, 'btn'); ?>
or
<input type="submit" name="btn1" value="button1" />
<input type="submit" name="btn2" value="button2" />
<input type="submit" name="btn3" value="button3" />
<?php if (filter_has_var(INPUT_POST, 'btn1')) { echo "button 1 clicked"; } ?>
I have a basic sum example with some PHP and HTML, inside this PHP page the reset button only resetting first two input field, and it is not resetting the third answer field. I dont know what is the error with this cause i am new to PHP. someone please help me to fix this.
code
<html>
<head>
<title>Title goes here</title>
</head>
<body>
<form action="" method="post">
<label>Enter Num1:</label>
<input type="text" name="num1" id="num1" /><br>
<label>Enter Num2:</label>
<input type="text" name="num2" id="num2" /><br><br>
<input type="radio" name="rad" value="add"/>addition
<input type="radio" name="rad" value="sub"/>sub
<input type="submit" name="btn_submit" value="fire">
<?php
if(isset($_POST['btn_submit']))
{
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$rad_val = $_POST['rad'];
if($rad_val=="add"){
$total = $num1+$num2;
}
else if($rad_val=="sub"){
$total = $num1-$num2;
}
echo "<SCRIPT TYPE=\"text/javascript\">
document.getElementById(\"num1\").value= $num1;
document.getElementById(\"num2\").value= $num2;
</SCRIPT>";
echo "<label>Answer is:</label> <input type=\"text\" name=\"rad\" value = $total />";
echo"<input type=\"reset\" name=\"reset\" value=\"reset\" />";
}
else if(isset($_POST['reset'])){
echo "<script>window.location = 'your_page.php';</script>";
}
?>
</form>
</body>
</html>
The reset function loads the your_page.php. So the initial value on that page might be needed to be changed to 0. Could you give an example of your_page.php?
You can trigger a page refresh by changing the line
echo"<input type=\"reset\" name=\"reset\" value=\"reset\" />";
to
echo"<input type=\"reset\" name=\"reset\" value=\"reset\" onclick= \"window.location = 'your_page.php';\"/>";
OR
Even better. Just Reset the value in the Answer to 0, This will save on the page reload.
Add an ID to the result field :
<input type=\"text\" name=\"rad\" id=\"rad\" value = $total />";
And change te value on the reset click/
echo"<input type=\"reset\" name=\"reset\" value=\"reset\" onclick= \" document.getElementById(\"rad\").value= 0;\"/>";
With Jquery you can reset to any value but you net to prevent the default action:
Example:
$('#Reset').click(function(){
event.preventDefault();
$('#element').val('default');
$('#element2').val('default');
$('#element3').val('default');
});
I have started writing a PHP script for a game about creatures, there are 4 yes/no questions and what I am trying to do is write a function that will display 2 buttons that say yes and no and give then different names each time I run the function, for example yes1 and no1, then the next time the function is run the names of the buttons will be yes2 and no2.
I have attempted to do this already but it is not working correctly, below is the code I have done so far, any help would be much appreciated.
<?php
session_set_cookie_params(2592000);
session_start();
?>
<html>
<head>
<title>Creature Guessing Game</title>
</head>
<body>
<h1>Creature Guessing Game</h1>
<p> Welcome to the creature guessing game! </p>
<p>Click the button below to start or restart the game </p>
<form method="post" action="Creatures.php">
<input type="submit" name="start" value="Start Game" />
</form>
<?php
$questions = array('Does the creature live on land?', 'Does it have wings?', 'Does it live near water?', 'Can it fly?');
function repeat()
{
$number = 0;
$number++;
echo "<form method ='post' action='Creatures.php'>
<input type='submit' name='yes'.$number value='Yes' />
<input type='submit' name='no'.$number value='No' />
</form>";
}
//If form not submitted, display form.
if (!isset($_POST['start'])){
?>
<?php
} //If form is submitted, process input
else{
switch($_POST)
{
case yes1:
echo $questions[0];
repeat();
break;
case no1:
echo $questions[1];
repeat();
break;
case yes2:
echo $questions[2];
repeat();
break;
case no2:
$questions[3];
repeat();
}
}
?>
</body>
</html>
To do this you need to maintain state between requests.
function repeat() {
$number = $_SESSION['number'];
$number++;
echo "<form method ='post' action='Creatures.php'>
<input type='submit' name='answer' value='Yes' />
<input type='submit' name='answer' value='No' />
</form>";
$_SESSION['number'] = $number;
}
switch
if($_POST['answer']=='Yes') {
switch($_SESSION['number']) {
case 1:
break;
case 2:
break;
}
} else {
switch($_SESSION['number']) {
case 1:
break;
case 2:
break;
}
}
I am not in agreement with the accepted answer:
Here is an easier way to go about it without session:
function repeat() {
static $number = 0;
$number++;
echo "<form method ='post' action='Creatures.php'>
<input type='submit' name='answer' value='Yes' />
<input type='submit' name='answer' value='No' />
</form>";
}
You have to store the 'number' in session or cookie as it will loose the value every time page submitted (reloaded).