How to print values of array using print_r php? - php

I have an array of values stored in variable "do"
It's something like this but not exactly
<html>
<input type='text' name='do' id='do'>
<input type='text' name='do' id='do'>
<input type='text' name='do' id='do'>
<input type='text' name='do' id='do'>
<input type='text' name='do' id='do'>
</html>
and I print it all with the use of [print_r] then it gives a result
<?php
print_r($_POST['do']);
//and i try this also
foreach($_POST as $key => $val){
echo $key . ' : ' . htmlentities($val,ENT_QUOTES) . "<br>\n";?>
}
[do=1&do=2&do=3]
how could I modify or just print the values like this:
1
2
3

Do not assign multiple elements the same ID (that's why it's out) but change the name in order to send an array of elements as POST
<input type='text' name='do[]'>
<input type='text' name='do[]'>
<input type='text' name='do[]'>
<input type='text' name='do[]'>
<input type='text' name='do[]'>

foreach($_POST as $key => $val)
echo $val;
^^ would do. However you cannot edit like $val=something. To change value, use $POST[$key]=something;

<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>

this won't work properly because you're overriding the same variable with different values and end up having one variable with the last value assigned.
Change this to
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
So you can loop through it like this:
foreach($_POST['do'] as $key => $val)

PHP overwrites the values with same plain names when parsing POST data into $_POST.
You may instead write the following form code:
<html>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
<input type='text' name='do[]' id='do'>
</html>
Now, $_POST['do'] is the array containing five do strings.

I think it can be done this way:
<input type="text" name="do[]" id="do_1" />
<input type="text" name="do[]" id="do_2" />
<input type="text" name="do[]" id="do_3" />
<input type="text" name="do[]" id="do_4" />
<input type="text" name="do[]" id="do_5" />
Then on POSTing, just look inside $_POST['do'] to get an array.
Edit: note that the apostrophes have been switched to quotes, the IDs have been made unique, and each tag has been self-closed. In general it will not validate otherwise.

Related

Why is my PHP string variable breaking where there is a space? [duplicate]

This question already has answers here:
How to set HTML value attribute (with spaces)
(6 answers)
Closed 1 year ago.
My PHP variable 'contractorcompany' usually has spaces in its value but when I try to use the value in a form it breaks at the space.
PHP
<?php
include "msbkeys.php";
if(isset($_POST['editcontractor']))
{
$contractorid = $_POST['contractorid'];
$contractorfirstname = $_POST['contractorfirstname'];
$contractorlastname = $_POST['contractorlastname'];
$contractorcompany = $_POST['contractorcompany'];
$contractoremail = $_POST['contractoremail'];
$contractorphone = $_POST['contractorphone'];
echo "$contractorcompany";
echo "<form method='POST'>
<input type='hidden' name='contractorid' value=" .$contractorid. ">
First name: <input type='text' name='firstname' value=" .$contractorfirstname. ">
<br/>
Last name: <input type='text' name='lastname' value=" .$contractorlastname. ">
<br/>
Company: <input type='text' name='company' value='".$contractorcompany."'>
<br/>
Email: <input type='text' name='email' value=" .$contractoremail. ">
<br/>
Phone: <input type='text' name='phone' value=" .$contractorphone. ">
<br/>
<input type='submit' name='updatecontractor' value='Submit'>
</form>";
}
mysqli_close($db); // Close connection
?>
When this value is for example 'Grant Grouting', it breaks as per line 6 below.
<form method="post">
<input type="hidden" name="contractorid" value="3">
<input type="hidden" name="contractorfirstname" value="Tom">
<input type="hidden" name="contractorlastname" value="Grant">
<input type="hidden" name="contractoremail" value="tomgrant#grouting.com">
<input type="hidden" name="contractorcompany" value="Grant" grouting>
<input type="hidden" name="contractorphone" value="">
<input name="editcontractor" type="submit" value="Edit this contractor">
</form>
I've tried every combination of quotation marks around the variable but am still not getting the full string passed through the form.
You just missed the quotes for the values:
<?php
include "msbkeys.php";
if(isset($_POST['editcontractor']))
{
$contractorid = $_POST['contractorid'];
$contractorfirstname = $_POST['contractorfirstname'];
$contractorlastname = $_POST['contractorlastname'];
$contractorcompany = $_POST['contractorcompany'];
$contractoremail = $_POST['contractoremail'];
$contractorphone = $_POST['contractorphone'];
echo "$contractorcompany";
echo "<form method='POST'>
<input type='hidden' name='contractorid' value='" .$contractorid. "'>
First name: <input type='text' name='firstname' value='" .$contractorfirstname. "'>
<br/>
Last name: <input type='text' name='lastname' value='" .$contractorlastname. "'>
<br/>
Company: <input type='text' name='company' value='".$contractorcompany."'>
<br/>
Email: <input type='text' name='email' value='" .$contractoremail. "'>
<br/>
Phone: <input type='text' name='phone' value='" .$contractorphone. "'>
<br/>
<input type='submit' name='updatecontractor' value='Submit'>
</form>";
}
mysqli_close($db); // Close connection
?>
Try to sanitize the output with addslashes for example.

How can I post more than 20 items?

I want to post multiple values:
index.php:
<form action="update.php" method="post">
<input name='name[]' value='1'/>
<input name='name[]' value='2'/>
<input name='name[]' value='3'/>
<input name='name[]' value='4'/>
<input name='name[]' value='5'/>
<input name='name[]' value='6'/>
<input name='name[]' value='7'/>
<input name='name[]' value='8'/>
<input name='name[]' value='9'/>
<input name='name[]' value='10'/>
<input name='name[]' value='11'/>
<input name='name[]' value='12'/>
<input name='name[]' value='13'/>
<input name='name[]' value='14'/>
<input name='name[]' value='15'/>
<input name='name[]' value='16'/>
<input name='name[]' value='17'/>
<input name='name[]' value='18'/>
<input name='name[]' value='19'/>
<input name='name[]' value='20'/>
</form>
update.php
if ($_POST['name']) {
foreach ( $_POST['name'] as $key=>$value ) {
echo $_POST['name'][$key];
}
}
My problem is that somehow the posting is limited to 20. When I add another input field <input name='name[]' value='21'/> then the output is still only 20 items. I have no idea why this is happening.
In your server there can be the max_input_vars value setted at 20.
Change that value by creating a .htaccess file in the dir where the script run and insert into:
php_value max_input_vars 100
Read more at http://php.net/manual/en/info.configuration.php#ini.max-input-vars.

PHP: How to use method GET, if action already contains GET value

I have a form, something like this:
<form action='index.php?page=search&filter=1'>
<input type='text' name='ID'>
<input type='text' name='number'>
<input type='submit'>
</form>
If I submit form, it will go to page:
index.php?ID=value1&number=value2
I would like to have
index.php?page=search&filter=1&ID=value1&number=value2
How can I append extra fields to url ?
Thank you for help.
you should put these values in hidden inputs:
<form action='index.php'>
<input type='hidden' name='page' value='search'>
<input type='hidden' name='filter' value='1'>
<input type='text' name='ID'>
<input type='text' name='number'>
<input type='submit'>
</form>
You can submit additional GET value as a hidden field value in the form which will be submit along with form submission.
<form action="index.php" method="get">
<input type='text' name='ID'>
<input type='text' name='number'>
<input type='hidden' name='page' value='search'>
<input type='hidden' name='filter' value='1'>
<input type='submit'>
</form>
Use hidden fields with your form and you will get exact url you want.
Try below code :
<form action="index.php" method="get">
<input type='hidden' name='page' value='search'>
<input type='hidden' name='filter' value='1'>
<input type='text' name='ID'>
<input type='text' name='number'>
<input type='submit'>
</form>
Try using some hidden input fields and use get method in form submitting.
Try this:
<form action="index.php" method="GET">
<input type='hidden' name='page' value='search' />
<input type='hidden' name='filter' value='1' />
<input type='text' name='ID' value='value1' />
<input type='text' name='number' value='value2' />
<input type='submit' value='Submit' />
</form>

PHP, select a string between two strings, then get one value from the selected string

I have a file, with many parts of code.
Each part starts with <form> and ends with </form>.
Between each form, there is something like this: <input type='text' name='date' value='2010-01-01'><input type='text' name='username' value='test'>
I want to parse each part of the code with php and get the value of input username, where the value for input date is '2010-01-01'
Any solutions please?
example:
<form>
<input type='text' name='date' value='2010-01-01'>
<input type='text' name='username' value='test'> <!-- How can I get this value ('test')? -->
</form>
<form>
<input type='text' name='date' value='2010-01-02'>
<input type='text' name='username' value='test2'>
</form>
.
.
.
preg_match('/form.*name=\'date\' value=\'2010-01-01\'.*name=\'username\' value=\'(.*)\'/Usim', $f, $r);
printf($r[1]);

Multiple post data to Mysql DB

I have problem with: I want to create admin for add questions to quiz system. Structure is:
<label>Question 1</label>
<input type='text' name='question' value=''/>
<label>Possible reply</label>
<input type='text' name='1' />
<input type='text' name='2' />
...
<input type='text' name='6' />
<label>**Correct reply</label>
<input type='text' name='correct' />
<label>Question 2 </label>
<input type='text' name='question' value=''/>
<label>Possible reply </label>
<input type='text' name='1' />
<input type='text' name='2' />
...
<input type='text' name='6' />
<label>Correct reply </label>
<input type='text' name='correct' />
<label>Question 3 </label>
...
<input type='submit' name='submit' value='submit'>
And I need multiple questions post to Mysql db tables: question, 1,2,3,4,5,6, correct.
I was create this:
<?php
if(isset($_POST['submit']))
{
$question $_POST['question '];
$a = $_POST['1'];
$b = $_POST['2'];
$c = $_POST['3'];
$d = $_POST['4'];
$e = $_POST['5'];
$f = $_POST['6'];
correct = $_POST['correct '];
$result=mysql_query("insert into test (question, 1, 2, 3, 4, 5, 6, correct) values ('$result', '$a', '$b', '$c', '$d', '$e', '$f', '$correct' )");
}
else
{
?>
<label>Question 1 </label>
<input type='text' name='question' value=''/>
<label>Possible reply </label>
<input type='text' name='1' />
<input type='text' name='2' />
...
<input type='text' name='6' />
<label>Correct reply </label>
<input type='text' name='correct' />
<label>Question 2 </label>
<input type='text' name='question' value=''/>
<label>Possible reply </label>
<input type='text' name='1' />
<input type='text' name='2' />
...
<input type='text' name='6' />
<label>Correct reply </label>
<input type='text' name='correct' />
<label>Question 3 </label>
...
<input type='submit' name='submit' value='submit'>
<?
}
But this send only 1 question to DB.
If you can't use
<input type='text' name='question_1' value=''/>
<input type='text' name='question_1' value=''/>
<input type='text' name='question_1' value=''/>
Write in HTML:
<input type='text' name='question[]' value=''/>
<input type='text' name='reply1[]' />
<input type='text' name='reply2[]' />
<input type='text' name='reply3[]' />
<input type='text' name='reply4[]' />
<input type='text' name='reply5[]' />
<input type='text' name='reply6[]' />
<input type='text' name='correct[]' />
In PHP:
<?php
if (isset($_POST['submit'])){
$questions=$_POST['question'];
$reply1=$_POST['reply1'];
$reply2=$_POST['reply2'];
$reply3=$_POST['reply3'];
$reply4=$_POST['reply4'];
$reply5=$_POST['reply5'];
$reply6=$_POST['reply6'];
$correct=$_POST['correct'];
foreach($questions as $key=>$value){
$result=mysql_query("insert into test (question, 1, 2, 3, 4, 5, 6, correct) values ('$value', '".$reply1[$key]."', '".$reply2[$key]."', '".$reply3[$key]."', '".$reply4[$key]."', '".$reply5[$key]."', '".$reply5[$key]."', '".$correct[$key]."' )");
}
}
It would be nice if you can state your database structure and expected result, because it may not work the way you think and we'll never know until we see it.
If you want 1 answer per row, you need to use a multiple row insertion - How to insert multiple rows in single insert statement?.
try that:
<input type='text' name='question1' value=''/>
<input type='text' name='question2' value=''/>
instead of
<input type='text' name='question1' value=''/>
<input type='text' name='question1' value=''/>

Categories