I found a few similar examples, but none specifically applicable to my case.
On my form page, I have multiple checkbox inputs in the following format:
<input type='checkbox' name='AwayMoneyLine[] value='$Date[$i];$VisitingRotNum[$i];$VisitingParticipantName[$i];$AwayMoneyLine[$i]'/>
The checkboxes loop through each XML data set for each $i. When submitted, I would like to display the values in a form. Here is some code I am using:
if(isset($_POST['AwayMoneyLine'])){
$bet_type1="Moneyline";
$NewDate1 = $_POST['$AwayMoneyLine[0]'];
$NewRotation1 = $_POST['AwayRotNum'];
$NewTeamParticipant1 = $_POST['AwayParticipantName'];
$NewBet1 = $_POST['AwayMoneyLine'];
$NewSpread1 = "";
var_dump($NewBet1);
echo "<tr><td>$bet_type1</td><td>$NewDate1</td><td>$NewRotation1</td><td>$NewTeamParticipant</td><td>$NewBet1</td></tr>";
}
However, none of the values display. I know the values are being passed as
var_dump($NewBet1);
// gives array(1) { [0]=> string(39) "10/31/2012 20:10;703;Denver Nuggets;100" }
Any help breaking down these values, assigning them to variables and displaying them would be greatly appreciated.
Use following way,
if(isset($_POST['AwayMoneyLine'])) {
foreach($_POST['AwayMoneyLine'] as $value) {
$d = explode(';',$value);
echo $d[0]."<br>"; //this gives date
echo $d[1]."<br>"; // this gives rotation
echo $d[2]."<br>"; // participant name
echo $d[3]."<br>"; // bet
}
}
Related
I'm using a form with checkboxes. Here is part of the code:
if(isset($_POST['send'])){
$grandezas = array('tempCheckbox', "umiCheckbox", "uvCheckbox", "ventoCheckbox", "direcaoventoCheckbox", "precipitacaoCheckbox");
$grandezasCount = 0;
$tamanhoArray = count($grandezas);
for($i = 1; $i <= $tamanhoArray; $i++){
if($_POST[$grandezas[i]])
$grandezasCount++;
echo $grandezas[i]."."; // This gives me null values.
}
echo "<br>".count($grandezas)."<br>";
echo $grandezasCount;
printf("%s", $grandezas[1]);
I have the $grandezas array with the names (already checked and they are right) of the checkboxes i used in my form. They return value 1 when checked. All the rest of the form works perfectly fine with a similar logic.
When i use:
echo "<br>".count($grandezas)."<br>";
printf("%s", $grandezas[1]);
It works right, but the echo inside the for loop keeps giving me null values.
Am i using the $_POST[$grandezas[i]] in the wrong way?
You need to use $i rather than i:
if($_POST[$grandezas[$i]])
$grandezasCount++;
echo $grandezas[$i].".";
Additionally, checkboxes that are not ticked on the form will not show up in your POST results. This means that you merely need to check that the variable exists IF you know the key is going to be a checkbox:
if( isset($_POST[$grandezas[$i]]) ){
//checkbox was ticked
}
The title is so general mainly because I don't know what should be the appropriate title for it. Let me just explain the situation:
Say that I have two textboxes named LastName0 and FirstName0 and a button called addMore. When I click addMore, another two textboxes will be created through JavaScript. These textboxes will be named LastName1 and FirstName1. When I click the addMore button again, another two textboxes button will be created and named LastName2 and FirstName2 respectively. This will go on as long as the addMore button is clicked. Also, a button named deleteThis will be created alongside the textboxes. This simply deletes the created textboxes when clicked.
I also initialized a variable called counter. Every time the addMore button is clicked, the counter goes up by 1, and whenever the deleteThis button is clicked, the counter decreases by 1. The value of the counter is stored in a hidden input type.
When the user submits the form, I get the value of the counter and create a For loop to get all the values of the textboxes in the form. Here is the sample code:
//Suppose that the user decides to add 2 more textboxes. Now we have the following:
// LastName0 FirstName0
// LastName1 FirstName1
// LastName2 FirstName2
$ctr = $_POST['counter']; //the counter == 3
for ($x = 0; $x < $ctr; $ctr++)
{
$lastname = $_POST["LastName$x"];
$firstname = $_POST["FirstName$x"];
//This will get the values of LastName0,1,2 and FirstName0,1,2
//code to save to database…
}
On the code above, if the value of counter is equal to 3, then the values of textboxes LastName0,1,2 and FirstName0,1,2 will be saved. Now here is the problem: If the user decided to delete LastName1 and FirstName1, the For loop will not be able to iterate properly:
$ctr = $_POST['counter']; //the counter == 2
for ($x = 0; $x < $ctr; $ctr++)
{
//Only LastName0 and FirstName0 will be saved.
$lastname = $_POST["LastName$x"];
$firstname = $_POST["FirstName$x"];
//code to save to database…
}
Someone told me to use the "push and pop" concept to solve this problem, but I am not really sure on how to apply it here. So if anyone can tell me how to apply it, it'll be grand.
Add your input text boxes with name as array ie, <input type="text" name="FirstName[]" />
In php you can fetch them as a array. ie,
foreach($_POST["FirstName"] as $k=>$val){
echo $val; // give you first name
echo $_POST["LastName"][$k]; // will give you last ame
}
In this case even if one set of field is removed in HTML will not affect the php code.
One solution would be to use the isset function like this:
$ctr = $_POST['counter'];
for ($x = 0; $x < $ctr; $ctr++)
{
isset($_POST["LastName$x"])?$lastname = $_POST["LastName$x"]:;
isset($_POST["FirstName$x"])?$firstname = $_POST["FirstName$x"]:;
}
If it is possible, instead of using LastNameN and FirstNameN names try using LastName[N] and FirstName[N], this way the result is an array and you can iterate through it with a foreach, meaning you will not need the counter and the index of the value will not be important:
foreach ($_POST["LastName"] as $i=>$lastname) {
if (!isset($_POST["FirstName"][$i])) {
// This should only happen if someone messes with the client side before posting
throw new Exception("Last name input does not have a related First name input");
}
$firstname = $_POST["FirstName"][$i];
}
If not, then you may have to use your $counter in a different way
$current = 0;
while ($counter) { // Stop only when i found all
if (isset($_POST["LastName$current"]) {
$counter--; // Found one
$lastname = $_POST["LastName$current"];
$firstname = $_POST["FirstName$current"];
}
$current++;
}
A better way to solve this would be to use arrays for Firstname and Lastname. Instead of calling them Lastname0 and Firstname0, then Lastname1 and Firstname1, call them all Lastname[] and Firstname[]. Give them ID's of Lastname0 and Firstname0 and so on for the delete function, but keep the names as arrays.
When the form is submitted use the following:
foreach($_POST['Lastname'] as $i => $lastname) {
$firstname = $_POST['Firstname'][$i]
//... code to save into the database here
}
Be warned though that in IE if you have an empty field it will not be submitted, so if Lastname0 has a value, but Firstname0 does not, then $_POST['Firstname'][0] will in fact contain the value of Firstname1 (assuming it has a value in it). To get around this you can use javascript to check if a field is empty when submitting the form, and if so put the word EMPTY in it.
Do not use counter if not required
A much easier way is to add array name when admore clicked.
Give a name like first_name[] in textbox
if you create form like that you can use foreach through $_POST['first_name']
try var_dump($_POST) in you php code to see how things goes on.
Inside your for loop, maybe you could try...
if ((isset($_POST["LastName$x"])) && (isset($_POST["FirstName$x"]))){
$lastname = $_POST["LastName$x"];
$firstname = $_POST["FirstName$x"];
//code to save to database…
}
This will check if the variables exists before you try to do anything with them.
I got a html form which i loop like this:
for($i=0;$i<10;$i++){
echo '<input type="text" name="field'.$i.'">';
}
then i make and hidden input with a count var which says that there are 10 such input fields. but now i hav $field0 to $field9 and i do not know how i can get the input in a for loop again.
thanks for your help!
Use names like this in your input fields: ...'field['.$i.']'...
This way in your $_POST these will show up in an array for you, and you can loop over them like:
foreach ($_POST['field'] as $key => $value)
{
}
First of all you should use $_POST to get your form data. then you can make this by doing
for($i=0;$i<$_POST["count"];$i++) {
$var = $_GET["field".$i];
//do something
}
I assume that you have a count variable in $_POST["count"]
2nd you could better use Arrays in your looped form
<input type="text" name="field[0]">
then you have an array in $_POST["field"] with $_POST["field"][0] and $_POST["field"][1] etc...
but to answer just you wanted you can also use variable variables:
here a sample which should make this clear
$a1 = "What";
$a2 = " are";
$a3 = " you";
$a4 = " doing?";
for($i=1;$i<=4;$i++){
$txt .= ${"a".$i};
echo $txt;
}
takes as output
"What are you doing?"
:)
in your loop:
echo '<input type="text" name="field['.$i.']">';
then when you process this form when it's submitted:
$fields = $_POST['field'];
the "field" variable will be sent as an array to PHP
I'm trying to make a BASIC roulette script.
Is there anyway to get the submitted results of a form using PHP? In fact I know theres a way, but i can't find out how to do it.
So say if my form had several fields I want the result to loop through and show me which fields were filled and the numbers in each.
UPDATE: And say the form has about 40 fields, would I have to name each one in the loop? Any easier way?
$_GET or $_POST depending on the form method.
if(isset($_REQUEST['formInputName'])){
echo $_REQUEST['formInputName'];
}
$_REQUEST looks for GET, POST, and COOKIE.
You can also use $_GET to get a variable from the url (asdf.php?var=2).
If your form looks like this:
<form method="post" action="result.php">
<input type="text" name="foo">
</form>
In result.php you can use the global variable $_POST and loop through it if you want:
foreach($_POST as $name => $value) {
echo $name . ' = ' . $value;
}
If your form has 40 fields, you still need to name them all, but you can automate the process of naming and retrieving them with a loop. For example, if you wanted to create a sum with the value of all the fields, you could name them number1, number2, etc and do:
$sum = 0;
for($i = 1; $i <= 40; $i++)
$sum += $_POST['number' . $i];
You need to define the name of each field using name="something" in the input element, and than in the PHP you're getting it using $_POST['something'] in case you sent the form as method="post" or $_GET['something'] in case of get method
You can see what's sent using var_dump() or print_r(), just write something like that:
echo '<pre>';
print_r($_POST);
Or you can go all over the array using foreach statement:
<?php
foreach($_POST AS $key=>$val)
{
echo $key.': '.$val."<br />\n";
}
?>
I had multiple checkbox which is generated using loop. After submitting the form I want to get the names of the selected individual checkbox to store it in database as id. Please help me.. Thanks in advance
Code i used for generating checkbox in loop:
while($arrayRow = mysql_fetch_assoc($rsrcResult))
{
$strA = $arrayRow["area_id"];
$strB = $arrayRow["area_name"];
echo "<div class=\"area_check\"><input type=\"checkbox\" id=\"covarea[] \" />$strB</input></div>";
}
This code I used for getting names for it didnt worked. It only returned state of check box as ON
while (list ($key,$val) = #each ($box))
{
$aid=$val;
echo $aid;
}
If the set of checkboxes is marked up as so
<input type="checkbox" name="food[]" value="Cheese">
<input type="checkbox" name="food[]" value="Ham">
Then any checked values are accessed as an array from $_POST['food']
Obviously with a code example, as #rajasekar points out, it would be easier to recommend an approach
The id for each element is optional, but should/must be unique.
Form elements must have a name attribute, or they will not be submitted (use "covarea[]" for the name for all the chexboxes.
Checkboxes must have a value attribute, or they will be submitted with value "on".
You must ensure that $strA and $strB do not have HTML meta characters or your generated HTML will be invalid.
Your example had a space after the "[]" in the id, btw.
Perhaps like this.
$n = 0;
while($arrayRow = mysql_fetch_assoc($rsrcResult))
{
$strA = $arrayRow["area_id"];
$strB = $arrayRow["area_name"];
$n++;
echo "<div class=\"area_check\"><input type=\"checkbox\" id=\"covarea${n}\" name=\"covarea[]\" value=\"${strA}\"/>$strB</input></div>";
}