This question already has answers here:
HTML PHP form entry problem with space?
(2 answers)
Closed 5 months ago.
I am having trouble with the POST method after submitting a form. It is not passing the complete string after submitting the form, but it shows correctly in the "select" & "option" html.
in the database i have a table called "administradores" with a column called "id" and other called "administrador":
id: 1
administrador: "Alejandro Salgado"
this is the php code:
function listAdministradores(){
global $conn;
$result = $conn->query("SELECT * FROM administradores");
while($row = $result->fetch_assoc()){
echo '<option value='.$row["administrador"].'>'.$row["administrador"].'</option>';
}
}
this is the html code:
<form action="./includes/crearpropuesta.inc.php" method="post">
<label for="administrador" class="form-label pt-3">Administrador:</label>
<select name="administrador" id="administrador" class="form-select">
<?php listAdministradores() ?>
</select>
And it shows the name "Alejandro Salgado" correctly:
html result
but when I submit the form and var_dump($_POST["administrador"]), it returns only the first name "Alejandro":
string(9) "Alejandro"
What is going on?
To include characters separated by spaces, you need to enclose the data with quotation marks (in your case double-quotation)
Hence, please change
echo '<option value='.$row["administrador"].'>'.$row["administrador"].'</option>';
to
echo '<option value="'.$row["administrador"].'">'.$row["administrador"].'</option>';
Related
This question already has answers here:
PHP How to send multiple values within the same name
(2 answers)
I am trying to insert multiple input value with same name
(2 answers)
How to get multiple input values with PHP when name attributes are the same?
(1 answer)
store multiple value with same input name in sql
(1 answer)
PHP Get multiple values from GET with same name
(2 answers)
Closed 4 months ago.
simple php code that works well in terminal:
<?php
$a = array();
for($i=0; $i<3; $i++){
$b = readline('time: ');
$c = readline('money: ');
$d = array('time'=>$b, 'money'=>$c);
array_push($a, $d);
}
print_r($a);
this pushes the values of multiple entries into an array, creating an array of arrays. however, readline() does not work in the browser. i know i can use javascript easily enough but am trying to replicate this simple action using only php and html. and i really like the way readline() works. i have tried variations on the following but am left scratching my head:
<form method="POST">
<?php
for($i=0; $i<3; $i++){
?>
<input name = 'time'>
<input name = 'money'>
<?php
}
?>
<input type="submit">
</form>
<?php
print_r($_POST['time']);
was hoping print_r($_POST['input name']) would return an array, but instead only gives the last input entry. is there a straight forward way to do this with php, or do i HAVE to use a client-side script like javascript?
Just add [] to input names:
<form method="POST">
<?php
for($i=0; $i<3; $i++){
?>
<input name = 'time[]'>
<input name = 'money[]'>
<?php
}
?>
<input type="submit">
</form>
<?php
print_r($_POST['time']);
This question already has answers here:
PHP & mySQL: When exactly to use htmlentities?
(4 answers)
Closed 2 years ago.
I want to do a check if a name already exists in an array.
I have an issue with a name which contains accented chars.
Below is the code is use and when filling in the (french) name Charlène Rodriês and the (german) name Jürgen Günter; it outputs: NOT exists.
How can i catch these names which contain accented characters?
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['bioname'])) {
$bioname = trim(htmlentities($_POST['bioname']));
$array = array('John Doe','Bill Cleve','Charlène Rodriês','мария преснякова','Jürgen Günter');
if (in_array($bioname_raw, $array)) { // if bioname already exists
echo '<div">'.$bioname.' ALREADY exists!</div>';
}
else {
echo '<div">'.$bioname.' NOT exists!</div>';
}
}
}
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST">
<input class="form-control" name="bioname" type="text" placeholder="AUTHORNAME">
<button type="submit" id="cf-submit" name="submit" class="btn btn-primary w-100">POST</button>
</form>
You're comparing apples and oranges.
When you do htmlentities('Charlène Rodriês'), it changes the string and will encode it into: Charlène Rodriês, which obviously won't match Charlène Rodriês in your in_array().
So remove the htmlentities() when you get the value from the $_POST-variable:
$bioname = trim($_POST['bioname']);
and only use that function before you output the data:
echo '<div">'. htmlentities($bioname).' ALREADY exists!</div>';
As a general rule of thumb, don't encode data on input. Only encode data when you use it since different use cases requires different types of encoding.
This question already has answers here:
Can I use white spaces in the name attribute of an HTML element? [duplicate]
(2 answers)
Closed 7 years ago.
Hi I am trying to make a form sticky. It creates a grid, and the user can then select a value for each row.
$subjectListArray has values of subject names like 'english', 'maths','speaking and listening' and $gradeSetArray carries a selection of grades like a,b,c etc
When I press submit I want the values that were selected to stay selected.
It works fine for subjects with NO SPACES in their names. As soon as I introduce an array value like 'Speaking and listening' its not sticky.
SO the space is causing me a problem.
<form>
<?php
foreach ($subjectListArray as $subject){
echo "<tr><td>$subject</td>";
foreach ($gradeSetArray as $grade){
echo '<td><input type="radio" name="'.$subject.'" value="'.$grade.'" ';
if ( isset($_POST[$subject])and $_POST[$subject]==$grade){
echo 'checked="checked"';
}
echo ' /></td>';
}
echo "</tr>";
}
?>
<input type="submit" name="submitPupilData" value="Input data" />
</form>
Here's a little of the html produced:
<td>
<input type="radio" value="p2iic" name="Speaking and Listening">
</td>
So for some reason my if ( isset($_POST[$subject])and $_POST[$subject]==$grade) isn't working if there's a space
What can I do in order to keep the spaces in the names and keep the from sticky?
Thanks
As per Jakar I printed out the $_POST and I noticed that the spaces were being replaced by _ characters thus Array ( [Health_and_well_being] => p2iic
It's not possible to use spaces in HTML name attributes. For more information checkout Can I use white spaces in the name attribute of an HTML element?
This question already has an answer here:
Dynamic variable names in Javascript
(1 answer)
Closed 9 years ago.
Here is my code currently in javascript:
'methv': document.getElementById("methv").value,
'q1': document.getElementById("q1").value,
'q2':document.getElementById("q2").value,
'q3':document.getElementById("q3").value,
'q4':document.getElementById("q4").value,
'comm':document.getElementById("comm").value,
in my php file i have a invisible input field named num which has a value of a unique number in my database.
<input id="num" type="hidden" value="<?php echo $post['unique person code']; ?>" />
is there a way to have this value as a variable name with this. For example it will have these values :
'q1' + num : document.getElementById("q1").value
Yes. Use bracket notation:
containerObject['q1' + num] = document.getElementById("q1").value;
Where container object is whatever contains all of your data. num would have to be defined first although.
I've got a form like so:
Question: <input type="text" name="question[][text]" id="question" />
Type:
<select name="question[][type]" id="type">
<option value="1to5scale">1 to 5 scale</option>
<option value="freetext">Open text</option>
</select>
This gets repeated depending on how many items the user wants (cloned through jquery). And my php to action the form contains:
foreach ( $_POST['question'] as $key=>$question )
{
if ($key >= 1) {
$question_text = $question['text'];
$type = $question['type'];
$query = " INSERT INTO questions (question_text, survey_id, type) ".
" VALUES ('$question_text','$survey_id', '$type')";
mysql_query($query) or die('Error ,query failed');
}
}
Each question has a text item and a type item. However in the DB it's adding one row per question item (if that makes sense...), rather than one row per complete question. Any ideas where I'm going wrong, it's quite hard to debug forms I'm finding...
Change the form element name from question[][type] to question[type][]. What this form does is add to the question array for each form element ([] means add a new array element).
As the comment implies, make sure you sanitize the input.