Currently I have 100 text input boxes named contact0 through contact101. I am trying to get the Post data and name each string to itself. Meaning $contact0 = $_POST['contact0'];all the way up to $contact101 = $_POST['contact101']; there has to be a simpler way to set them in a loop or something. Overall the end result is I just want the data entered in the textbox to become the value of the textbox when submitted. Any suggestions will help I might be doing this wrong for the results I want.
for ($i = 0; ;$i++){
if($i < 101){
$contact.$i = $_POST['contact'].$i;
echo $contact.$i;
}
else{
break;
}
}
for ($i = 0; $i <= 101; $i++){
${"contact".$i} = $_POST['contact'.$i];
echo ${"contact".$i};
}
You may want to consider amending your HTML form. Rather than create 100 new variables, you can assign all the contacts to an array and then reference them with the array index.
HTML
<input type="text" name="contacts[]" value="Contact 1 name"/>
<input type="text" name="contacts[]" value="Contact 2 name"/>
// etc...
PHP
$contacts = $_POST['contacts'];
var_dump($contacts);
// prints array(0 => 'Contact 1 Name', 1 => 'Contact 2 Name'...
As it now an array, you can reference the contact e.g. $contacts[34] and know it will be a valid entry.
EDIT
A working example:
<?php
if (isset($_POST['contacts'])) {
$contacts = $_POST['contacts'];
echo "<p>The contacts are below:</p>";
print_r($contacts);
} else {
echo "<p>Please enter the contacts</p>";
}
?>
<form method="post" action="">
<?php for($x = 0; $x < 100; $x++): ?>
<input type="text" name="contacts[]" value="<?php echo (isset($contacts[$x])) ? $contacts[0] : ''; ?>"/>
<?php endfor; ?>
<input type="submit" name="submit" value="submit"/>
</form>
Edit 2
I have now made the form a loop, meaning it will create all our contact inputs for us. On top of this, if we have already posted the form each field will have the correct contact values.
If you are not aware of the syntax, echo isset($contacts[$x])) ? $contacts[$x] : ''; is a ternary operator which is a one line if/else statement.
Which can also be tested here: http://phpfiddle.org/api/run/ugb-cta
Related
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.
I've received some c++ code regarding finding the highest number until the input been set as 0 using the form and while statement, but my echo doesn't appear, even after several times trying to key in the highest number
here some reference of C++ code that need to be transform into php format:
http://prntscr.com/plw8mw
<html>
<body>
<form method="POST">
Enter First Number:
<input type="text" name="num1"/><br>
<input type="submit"><br>
<form/>
<?php
if(isset($_POST['submit'])) {
$bignum = 0;
$num = $_POST['num1'];
while($num != 0) {
if ($num > $bignum)
$bignum = $num;
}
print "$bignum";
}
?>
<body/>
<html/>
Use echo insteed of print
echo($bignum);
i want a input field where i can write in a number from 0 to 10.
if i write number 10 the output must be 9,7,5,3,1
or if i would write the number 8 the output should be 7 5 3 1
So only the odd numbers must be outputted when clicked on submit after
putting a number in the input form. html is combined with this php code i think.
please help.
<form action="#" method ="post">
<input type="text" name="number">
<input type="submit" name ="submit" value="submit">
</form>
<?php
if(isset($_POST['submit'])){
$number = $_POST['number'];
for($i=$number;$i>0;$i--){
if($i%2 != 0 ) // modulo prueba por pares o impares
echo $i.'<br>';
}
}
Please check above code
try this one
$inputNumber = intval($_POST[inputNumber"]);
for($i = $inputNumber ; $i >=1 ; $i--){
if($i % 2 == 1){
echo $i." ";
}
}
This title might not describe my question too well but I was unsure how to name this post... Anyways, I have a form that has dynamically generated input boxes that pulls the last 4 years with the following:
<?php
$current_date = new DateTime();
for ($i = 1; $i <= 4; $i++) {
$current_date->modify('-1 year');
$date_string = $current_date->format('Y')
?>
<fieldset name="gross_sales">
<input type="number" name="year_brand_gross[<?php echo $date_string; ?>]" placeholder="Gross Sales for <?php echo $date_string; ?>">
</fieldset>
<?php
} // end while
?>
And once the user clicks submit the form data is processed via my process.php file that contains the following:
$year_brand_gross[1] = $_POST['year_brand_gross'][1];
$year_brand_gross[2] = $_POST['year_brand_gross'][2];
$year_brand_gross[3] = $_POST['year_brand_gross'][3];
$year_brand_gross[4] = $_POST['year_brand_gross'][4];
Now I'm pretty sure the above part is not right. So this is my question... How would I get the info from these inputs into my email that's sent since their created by an array and not "actually" there. Here's a stripped down version of my html email that's sent which I'm pretty sure is also wrong since the above code is incorrect:
<table>
<tr>
<td>Gross Sales:</td>
</tr>
<tr>
<td>{$year_brand_gross[1]}</td>
<td>{$year_brand_gross[2]}</td>
<td>{$year_brand_gross[3]}</td>
<td>{$year_brand_gross[4]}</td>
</tr>
</table>
Any help is greatly appreciated!
Your form would actually look like
<input type="number" name="year_brand_gross[2012]" ... />
<input type="number" name="year_brand_gross[2011]" ... />
<input type="number" name="year_brand_gross[2010]" ... />
etc...
That means you need to use
$_POST['year_brand_gross'][2012]
$_POST['year_brand_gross'][2011]
$_POST['year_brand_gross'][2010]
etc...
on the server.
foreach($_POST['year_brand_gross'] AS $yeah => $value) {
// use $year and $value variables to do whatever
// this code will execute once for each values in $_POST['year_brand_gross'].
// note: print_r($_POST);
// $_POST is an array, same for $_GET and so on
}
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.