Passing variable/array through several pages in php - php

my code starts with multiple php arrays with XML data being passed through multiple checkboxes in the following way($i cycles through items in XML doc, values pass specific data in row i that is selected):
$MoneyLine = $event->periods->period[0]->moneyline; //background info
$AwayMoneyLine[] = $MoneyLine->moneyline_visiting; //background info
<input type='checkbox' name='AwayMoneyLine' value='$Date[$i];$AwayRotNum[$i];$AwayParticipantName[$i];$ATotalPoints[$i]'/>
On my next page, I pass the variables in the following manner, but they are not resulting on the next page.:
if(isset($_POST['AwayMoneyLine'])){
foreach($_POST['AwayMoneyLine'] as $value) {
$d = explode(';',$value);
echo '<input type="hidden" name="hidden[]" value="$d[0];$d[1];$d[2];$d[3];$d[4]">';
Here is how I'm attempting to get the data on the next page. Any suggestions on how I can pass the variables through to this page? On the form (also on var_dump of $d), I get $d[0], $d[1], etc. Any help is greatly appreciated!:
foreach($_POST['hidden'] as $value) {
$f = explode(';',$value);
echo 'Here is your following bet:';
echo '<table cellpadding="0" cellspacing="0" border="1" bordercolor="#585858" width=100%>';
echo "<tr><td>$e[0]</td><td>$f[0]</td><td>$f[1]</td><td>$f[2]</td><td>$f[3]</td><td>$f[4]</td></tr>";
}
echo '</table>';

Change the code to:
echo "<input type=\"hidden\" name=\"hidden[]\" value=\"$d[0];$d[1];$d[2];$d[3];$d[4]\">";
$d[0];$d[1];$d[2];$d[3];$d[4] will not be printed as you put inside single quote. You should use double quote and escape attributes like type=\"hidden\", name=\"hidden[]\", etc.,
eg:
<?php
$name = 'foo';
echo "$name";
echo '<br />';
echo '$name';
?>
Output:
foo
$name

use $serial=serialize($array);
echo "<input type=\"hidden\" name=\"hiddenfield\" value=\"<?php echo$serial; ?>">";

Related

form post of values in array and manage them divided by key

I dont know how to manage this situation, I'm a noob coder, I have a page that shows you all available lots where you can unload a specific item from that lot.
This is the foreach that prints out:
$lotto, $totalelotto, $data, and ask for qtyvalue to unload from $lotto (input can be also NULL)
foreach ($dataslotto as $data) {
$totalelotto = totlotto($database, $data['lotto']);
$lotto = $data["lotto"];
$data = $data["data"];
echo "<tr>";
echo "<td>".$lotto."</td>";
echo "<input type=\"hidden\" value=\"".$lotto."\" name=\"array[]\" />";
echo "<td>".$totalelotto."</td>";
echo "<input type=\"hidden\" value=\"".$totalelotto."\" name=\"array[]\" />";
echo "<td>".$data."</td>";
echo "<input type=\"hidden\" value=\"".$data."\" name=\"array[]\" />";
echo "<td><input type=\"text\" class=\"form-control\" placeholder=\"Qta.\" required name=\"qtyvalue\"></td>";
echo "</tr>";
}
I dont know how to set name="" of input fields (because the number of fields can change if there are many lots) and I dont know how to send $_POST data as array, and then foreach group of $lotto, $totalelotto, $data, $qtyvalue where is set $qtyvalue do another query.
I put it in no regular code, I know it looks bad but it's just for giving you an idea.
$_POST[''formarray];
foreach ( /* values recieved in each <tr> inside formarray where $_POST['qtyvalue'] is not empty */ ){
#EXECUTE THIS
}
Thanks for help!!
And sorry for my bad coding skills.
$_POST is an Associative/Key Value pair, it's key is whatever is set as the inputs name.
so if you wanted to send the users input username to the backend PHP script
You set the value and it's name
<input type="text" name="username" value="User123">
then to retrieve the user name you can do
print_r($_POST["username"]);
to print the value.
So you ask how do you loop over each one in $_POST, that's pretty simple you can could a foreach loop over the entire $_POST array.
foreach($_POST as $key => $value)
{
//check something has been entered for the current value we are iterating over
if($value != null)
{
print_r($key . " value is : " . $value);
}
}
this would loop over each item in the $_POST array with key being set to whatever the DOM elements name is.
Don't forget the $_POST array is just that an array, you could do
var_dump($_POST);
and see everything that was sent in the POST request.
You can use array names for your inputs. Say you have a row of your table like this:
<tr>
<td><input ... name="lotto[]"></td>
<td><input ... name="totalelotto[]"></td>
<td><input ... name="data[]"></td>
<td><input ... name="qtyvalue[]"></td>
</tr>
Then you will get arrays $_POST['lotto'], $_POST['totalelotto'] and so on, each with the same number of elements, and elements with same index belonging to one row of the table. You could then process those elements like this
foreach ($_POST['lotto'] as $i=>$lotto) {
if ($_POST['qtyvalue'][$i] > 0) {
...
}
}

PHP Loop Through CSV and Print Selected Row

I'm trying to display a CSV file in a browser and print the selected (via radio button) row.
Here's my code to print the CSV file (with a radio button next to each row):
<?php
echo "<html><body><table border='1'>";
$f = fopen("data.csv", "r");
$row = 1;
echo "<form method='post' action='submit.php'>";
while (($line = fgetcsv($f)) !== false) {
$to_print = implode(",", $line); // added this and changed value below (to use it)
echo "<tr><td><input type='radio' name='env[$row]' value='$to_print' /></td>";
foreach ($line as $cell) {
echo "<td contenteditable='true'>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
++$row;
}
fclose($f);
echo "</table><br><Input type='submit'></form></span></body></html>";
?>
I have a submit button at the bottom that (when clicked) should pass the data in the selected row to submit.php which should print it.
Here's the submit.php:
<?php
echo "<html><body>";
print_r($_POST); // this prints what I need
if (! empty($_POST['env'])) {
$j=0;
foreach ($_POST['env'] as $env) {
// Can't get anything to print in here
++$j;
}
}
echo "</body></html>";
?>
But it prints nothing. I'm guessing it has something to do with the 'value' of my radio button:
value='<?php echo $line;?>]'
That can't be right...
Any suggestions would be great. Thanks!
The problem with your code is this line:
echo "<tr><td><input type='radio' name='env[<?php echo $row;?>]'
You are echoing some HTML from within PHP. Inside the line your are echoing out, you are opening <?php tags again. Debugging what is being POSTed, you are actually just sending the string "<?php echo $row;?>"
To fix this issue, change your echo line to this:
echo "<tr><td><input type='radio' name='env[$row]' ...
As long as you are using double quotes, the $row variable will be interpreted and you'll get the outcome that you want.
Edit:
Once you have the data output to the page correctly, you need to reconsider how you are looping over the data.
The value of $line is set by [fgetcsv](http://php.net/manual/en/function.fgetcsv.php) which returns an array. You are setting this array as the value to the input like this: value=$line, but seeing as $line is an array, PHP is making an array-to-string conversion, and you're not getting what you expect.
Solution: Either pick a single index of the array to set as value (such as $value=$line[0]) or output the td within the inner loop, where you are looping over each field within the row.

Different elements with the same name in PHP

In PHP, I would like to know how to provide the same name to different elements and still echo them out individually. I think the array mechanism works here but I don’t know how.
echo "<input type='checkbox' name='seat[]' id='something'/>";
echo "<input type='checkbox' name='seat[]' id='something1'/>"
Then to echo their values out I use the following:
<?php
if(isset($_POST['seat[]']))
{
echo ' ', $_POST['seat[]'] ;
}
?>
Please guide me!!
It's just an array of data. You can access elements of it just like any other array:
foreach ($_POST['seat'] as $seat) {
echo $seat . "<br>\n";
}
or using a numerical index:
echo $_POST['seat'][0]; // value of the first submitted checkbox

Unsure how to get $_POST[''] value from form when the name is dynamic?

So at the moment I have the following code which is correct but I'm just unsure how I will be able to get the name=".$job_id." as it will be dynamic, applicant-jobs.php below:
<div id="container">
<?php
foreach($jobs as $job){
$job_id = $job['id'];
echo form_open('applications/applicants');
echo "<div class=\"job\">";
echo $job['name'];
echo "</div>";
echo "<input type=\"hidden\" name=".$job_id.">";
echo form_submit('submit', 'View Applicants');
echo form_close();
}
?>
Any help is greatly appreciated, many thanks. P.S. I'm using codeigniter.
You can iterate arrays by key value pairs.
foreach($_POST as $key => $val)
{
}
Read more on the docs here; http://php.net/manual/en/control-structures.foreach.php
Edit, I slightly misunderstood your question. What you're looking for is the value attribute of the <input> element:
echo "<input type=\"hidden\" name=\"job\" value=\"".$job_id."\">";
Then in PHP you just access it like:
$job_id = $_POST['job'];
By the looks of it, the name doesn't need to be dynamic at all. You're only using one per form.
The name can be static, and have the value dynamic.
ex:
echo "<input type=\"hidden\" name=\"job_id\" value=\"".$job_id."\" />";
it can be accessed via $_POST['job_id']
Don't you want to set the value rather than the name?
eg.
echo "<input type=\"hidden\" name=\"job_id\" value =\"".$job_id."\">";
Also you've got the from open and close within the loop, i think you want them outside the loop otherwise you have multiple forms and will only end up submitting one.

PHP pass variable to hidden field

Ok, I am using PHP to parse XML file and to display it;s context to HTML. Here is the code.
Sample text is - Don't Give UP!
$xml = simplexml_load_file('data/quotes.xml');
foreach ($xml as $quote) {
$text = $quote->text;
echo '<div class="itemWrapper">'.
'<div class="quoteHolder">'.
'<p class="quote">'.$text.'</p>'.
'</div>'.
'<form class="selectionButtons">'.
"<input type='hidden' value='$text' name='quote'>".
'<input class="submitButton" type="button" value="create your design">'.
'</form>'.
'</div>';
}
So, when I use $text variable in paragraph it display's correctly, But when I pass it to the form's hidden field I only get: Don (so it stops right before that single quote) It happens with every text that has quotes. Why is that and what is wrong here?
try to use this one...
$this_text = "Don't give up!";
$text = htmlspecialchars($this_text, ENT_QUOTES);
echo "<input type='text' value='$text' />";
I have tested in already..
HTML input fields require that the contents be escaped. Conveniently, PHP has a function that does all the work for you:
$display_text = "Don't give up!";
$input_text = htmlspecialchars($text);
Reference

Categories