Need a little help with my coding. I'm working on a project in which it will accept 5 different numbers and insert them into a PHP array. The codes I have tried are written below. Both of them would make all the 5 contents of my array the same number, appreciate a little help please? Never dealt with array in PHP yet.
<form action="activity_1.php" method="post">
<input type="text" name="number">
<input type="submit" value="Submit" name="submit">
</form>
So far these are the ones I've tried:
$no = array();
for($i=1; $i<=5; $i++){
array_push($no, $_POST['number']);
}
Also
$no = array();
for($i=1; $i<=5; $i++){
//$no[$i]= $_POST['number'];
}
You may use one session variable to store the posted data.
So slightly amend your code into the following :
<?php session_start(); ?>
<form action="#" method="post">
<input type="text" name="number">
<input type="submit" value="Submit" name="submit">
</form>
<?php
if ( !isset($_SESSION["no"]) ) {
$_SESSION["no"]=array();
}
if (isset($_POST["number"])){
array_push($_SESSION["no"], $_POST['number']);
}
for($i=0; $i<5; $i++){
if (isset($_SESSION["no"][$i])) {
if ($_SESSION["no"][$i]!="") {
echo " Number you entered was: " .$_SESSION["no"][$i] . "<br>";
}
}
}
?>
In my opinion the code can be further amended so that
a) you should have a button to "clear" all the previous entered number;
b) I don't know whether it is necessary, but you may wish to add a function to check whether a newly entered number is the same as one of the past numbers entered, and if they are the same, do not put it into the array.
Related
I am trying to create a quiz, in order to do this I am using a form which users will input information into, once they submit this form it should add the input to an array/list.
The user should then be able to enter information and the process would repeat.
The finished product would be an array with each element corresponding to the order the answers were given.
-
I have tried so far using both array_push() and declaring elements, eg: $my_array[0] = $input;.
The current problem I am experiencing is that each time I submit the form, the $count variable doesn't seem to increment.
Instead it simply stores the data in the first element and overwrites which was previously there.
I am inclined to believe this is a problem with the posting of the submit button.
-
Here is my code:
<html>
<body>
<form action="" method="POST">
<input type="text" name="INPUT" placeholder="Input something"; required /><br><br>
<input type="submit" name="Submit" /><br><br>
<?PHP
$my_array = array();
$count = 0;
if(isset($_POST['Submit'])){
global $count;
$input = $_POST['INPUT'];
$my_array[$count] = $input;
print_r($my_array);
echo "Count:" . $count;
$count++;
}
?>
</form>
</body>
</html>
The crux of the issue here is that variable values do not persist across PHP requests. Every time you submit the form, you are throwing away your old $count and $my_array variables and initializing new variables with the same names.
Here is a working version of your code snippet, which takes advantage of the PHP $_SESSION variable to have persistent information between requests:
<?php
session_start();
if (!isset($_SESSION["my_array"])) {
$_SESSION["my_array"] = array();
}
?>
<html>
<body>
<form action="" method="POST">
<input type="text" name="INPUT" placeholder="Input something"; required /><br><br>
<input type="submit" name="Submit" /><br><br>
<?php
if(isset($_POST['Submit'])){
array_push($_SESSION["my_array"], $_POST['INPUT']);
print_r($_SESSION["my_array"]);
echo "Count:" . count($_SESSION["my_array"]);
}
?>
</form>
</body>
</html>
I'm doing this task in school where I'm to do something with arrays and loops in PHP.
What I've done so far is make this piece of code, that makes an array with names from a separate text-file, and chooses a random name from that array.
What I'd like it to do now is to display x amount of random names. The amount of names can be chosen in an input field, preferably with a for or while loop (those are the ones I know somewhat).
Here is my code (Don't think the text-file is necessary. If it is, just let me know):
<form method="POST">
How many names do you need?
<input type="number" name="amount" min="1" max="28"><br><br>
<input type="submit" name="proceed" value="Get name(s)">
</form>
<?php
$text = file_get_contents("names2t.txt");
$Array = explode("\n", $text);
$randNameNum = array_rand($Array);
$randPhrase = $Array[$randNameNum];
if (isset($_POST["proceed"])){
echo $randPhrase;
}
?>
Is it possible to do what I'm asking?
Just add another field on your form and loop on it :
<form method="POST">
How many names do you need? <input type="number" name="amount" min="1" max="28"><br>
How many times? <input type="number" name="repeat-count"><br>
<input type="submit" name="proceed" value="Get name(s)">
</form>
<?php
if(isset($_POST['proceed'])) {
for($i = 0; $i < $_POST['repeat-count']; $i++) {
$text = file_get_contents("names2t.txt");
$Array = explode("\n", $text);
$randNameNum = array_rand($Array);
echo $Array[$randNameNum];
}
}
?>
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!
I want to have a form in following way:
[-] [value] [+]
I don't know how to script the logic to get the value 1 higher or 1 lower in PHP.
I assumed something like following would work:
<?php
$i = 0;
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if(isset($_POST['plus']))
{
$i++;
}
if(isset($_POST['min']))
{
$i--;
}
}
?>
and my form is as following:
<form action="" method="post">
<input type="submit" name="min" value="-"> <input type="text" value=<?php echo $i ?> > <input type="submit" name="plus" value="+">
</form>
But I'm only getting either a 1 or a -1. Can someone show me how to do this in a good way?
Try this:
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$i = $_POST['current_value'] || 1;
if(isset($_POST['plus']))
{
$i++;
}
if(isset($_POST['min']))
{
$i--;
}
}
?>
and this:
<form action="" method="post">
<input type="submit" name="min" value="-"> <input name="current_value" type="text" value=<?php echo $i ?> > <input type="submit" name="plus" value="+">
</form>
You need some way to get the current value to persist between requests - depending on your use case, you may need database storage as Rocket mentions, or this simple passing back of the current value may be enough.
PHP will simply load code on the server-side and run when the page is loaded. If you are looking to dynamically increment/decrement the value while remaining on the same page, I suggest you look into learning Javascript and jQuery.
Reference: https://developer.mozilla.org/en/JavaScript/
Reference: http://jQuery.com/
What is happening with your code is that you are incrementing a global variable that you set to 0 in the beginning. That is why the only values you get back or 1 or -1. This can be fixed by passing the variable you increment each time instead of using a global variable. That way it keeps the value between each plus and minus and doesn't keep resetting it.
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
How to loop through dynamic form inputs and insert into an array
I have a php script and a form. The php script makes an xml file but what i need is for someone to enter a number and that would set that amount of textboxes that would be for someone to write data for that xml file.
So i need it to write <input type="text" name="a #"> however many times the user enters. Also the name needs to be a number but it counts by one ex:<input type="text" name="1"> <input type="text" name="2">... Thanks
<?php
session_start();
if(isset($_POST['quantity']){
// code here to check isnum and min/max
$count = $_POST['quantity'];
for ($i=1; $i<=$count; $i++){
#$s.= "<input type=text name=".$i."><br>";
}
?>
now just echo out $s in your html
This?
<form method="get" action="">
<div><input type="text" name="num_inputs" value="1" placeholder="Number of inputs"/></div>
</form>
<?php $num_inputs = isset($_GET['num_inputs']) ? $_GET['num_inputs'] : 1; ?>
<form method="post" action="">
<?php for ($i = 0; $i < $num_inputs; $i++) : ?>
<div><input type="text" name="inputs[]"/></div>
<?php endfor ?>
</form>
Edit: yes, an array is much better than input_x. Updated my answer.
I think what you want is an array of form fields.
You want something like this:
<?php
$number_of_textboxes = 5; // you'd get this from a $_GET parameter
echo str_repeat('<input type="text" name="mybox[]" />', $number_of_textboxes);
?>
This will print five text boxes:
<input type="text" name="mybox[]" />
Then, when you reference these boxes' values, you do so like thus:
<?php
foreach ($_POST['mybox'] as $i) {
echo $i;
}
?>
That is, by using "mybox[]" as the name of each input field, you create an array of textboxes, which you can then iterate through.