I am facing problem to pass data from one page to another using POST method. I want to pass data from the input field name 'keyword' to another PHP page. But it is not passing the complete data but when i print this data in same page using the 'keyword_div' it is showing the complete data.
Div is showing only 10 words:
আয়,গুর,ছুটে,লগন,জীবন,বয়ে, আজ,যায়,করে,চাঁদের
These lines are for passing the data-
print "<input type='hidden' id='keyword' name='keyword' value=". implode(',', extractKeyWords($file)) .">";
Print "<div class='output' name='keyword_div' id='keyword_div'>Keywords: " . implode(',', extractKeyWords($file)) . "<div>";
These lines are for getting the data after form submission-
if (isset($_POST['keyword'])){
$val = $_POST['keyword'];
} echo $val;
But Input is passing 6 words only:
আয়,গুর,ছুটে,লগন,জীবন,বয়ে
Why I am not getting the whole value? Please help.
You need to put quotes around the value, because there's a space at the beginning of the 7th keyword and that's ending the value attribute.
print "<input type='hidden' id='keyword' name='keyword' value='". implode(',', extractKeyWords($file)) ."'>";
Good HTML style is to put quotes around all attribute values.
If you want to get rid of the whitespaces as well, call trim().
print "<input type='hidden' id='keyword' name='keyword' value='". implode(',', array_map('trim', extractKeyWords($file))) ."'>";
Related
I have two checkbox forms on the same page. When they are both there, the second form will not work. I'm wondering if there is a way to have both on the same page with them both needing the $index of the input.
The input fields are similar to this (the other is unchecked):
echo "<input type='checkbox' name='check[]' value='$index' onChange='this.form.submit()' checked='checked'>";
echo "<input type='hidden' name='itemDELETE$index' value='$bookID'>";
This is the other
echo "<input type='checkbox' name='checkbox[]' value='$index' onChange='this.form.submit()'>";
echo "<input type='hidden' name='itemADD$index' value='$bookID'>";
And the data is stored like:
$value = $_POST["check"][0];
$toDELETE = $_POST["itemDELETE" . $value];
and
$value = $_POST["checkbox"][0];
$toADD = $_POST["itemADD" . $value];
I have tried renaming $value but the issue lies in the $index on the input fields.
They are outputting correctly, but the second one will not store the value properly in it's variable, whereas the first does. The second is the delete one.
When I remove the first forms functionality, the second form works. But when I add it back, the second one stops working. Not picking up on the itemDELETE $value
If I manually put the value in like this "itemDELETE0" instead of ["itemDELETE" . $value]it works. But I need the $value to picked up on since it's a foreach loop.
My solution to this was to put the second form in different shortcode, and include it on the page. It works well and doesn't clash. Same code, just in a different shortcode.
Then on the page created, I used the shortcode for the first form and then put the shortcode for the second form below it.
And instead of defaulting it to checked, I had to use it unchecked. But that works alright.
So within my php code: I'm trying to auto generate and populate a form that will edit a database.
$aName = "Wayne Gretzky";
echo "<form>";
echo "<input type='text' value=".$aName.">";
echo "<input type='Submit'>";
echo "</form>";
But when the code executes it shows a textbox with the value of "Wayne" rather than "Wayne Gretzky"
Because i want the form to be auto generated, so it can adapt to whatever data I may pull form the database; How do i call the variable or the result of a query so that the full string will be displayed in the textbox?
You need to put quotes on the value attribute as well, otherwise the browser does it for you and then you get what you got.
echo "<input type='text' value='" . $aName . "'>";
You are going wrong with the quotes.
If you are using double quotes outside, use single quotes inside for the html tags.
The correct code is
<?php
$aName = "Wayne Gretzky";
echo "<form>";
echo "<input type='text' value='$aName'>";
echo "<input type='Submit'>";
echo "</form>";
?>
I've created a php page which retrieves question and options(as MCQ's) from the database and in that Radio buttons are created dynamically using echo()The name for each radio button group is assigned into an array from which one by one index's value is set to the name attribute.Example:
$result=mysqli_query($db,$sql);
$numrows=mysqli_num_rows($result);//gets the number of questions
$radiogrp_name=array();
for($i=0;$i<$numrows;$i++){ //creating array of names for each set of radio buttons
$radiogrp_name[$i]="q".$i;
}
$i=0;
while(($myrow=mysqli_fetch_array($result)) && ($i<$numrows)){
echo $myrow["q_no"].". ";
echo $myrow["ques"]."<br><br>";
echo "<input type='radio' name='$radiogrp_name[$i]' value='a'/>".$myrow["A"]."<br>";
echo "<input type='radio' name='$radiogrp_name[$i]' value='b'/>".$myrow["B"]."<br>";
echo "<input type='radio' name='$radiogrp_name[$i]' value='c'/>".$myrow["C"]."<br>";
echo "<input type='radio' name='$radiogrp_name[$i]' value='d'/>".$myrow["D"]."<br><br><br>";
$i++;
}
How would I get the selected radio buttons and set them into a SESSION variable? Can anyone help with this? Or any other way to implement this?Thank you in advance!
On the page that the form is POST'ed to you are going to need to look through the $_POST array and strip out the relevant answers. Ideally you would have a list of expected indexes to drive that page, but if not you could just try to pattern match the indexes.
Something like:
$results = array();
foreach($_POST as $key => $value){
if(preg_match("/q(\d{1,3})/", $key, $matches)){
$results[$matches[1]] = $value;
}
}
results would then contain an array of values indexed by the numeral in name="q#" and you would just set it to the session or do with it whatever you need.
EDIT
by the way, you'll need to wrap your string inclusion in curlies since it is an array.
echo "<input type='radio' name='{$radiogrp_name[$i]}' value='d'/>"
echo "<input type='hidden' name='pb1' value='$_POST[pb1]'>";
echo "<input type='hidden' name='pb2' value='$_POST[pb2]'>";
echo "<input type='hidden' name='pc1' value='$_POST[pc1]'>";
echo "<input type='hidden' name='pc2' value='$_POST[pc2]'>";
I want to perform the above task through a function call like below.
function fun1($rm)
{
for ($i=1;$i<=2;$i++)
{
echo "<input type='hidden' name='p.$rm.$i' value='$_POST[p.$rm.$i]'>";
}
}
fun1('b');
fun1('c');
Please suggest how to edit the code inside the function to achieve the goal.
You're mixing two techniques here. In PHP, you can use variabled directly in a double quoted string. In a single quoted string you'll have to terminate and concatenate. However, you're also trying to access an array index dynamically within a double quoted string, and that unfortunately wont work. Here's two way to do what you're trying to:
echo "<input type='hidden' name='p$rm$i' value='".$_POST['p'.$rm.$i]."'>";
echo '<input type="hidden" name="p$rm$i" value="'.$_POST['p'.$rm.$i].'">';
// If you weren't accessing the array index dynamically (with a variable) this would work:
echo "<input type='hidden' name='p$rm$i' value='$_POST[pb1]'>";
// note no quotes for the index when inside a string ^^^
Functionally they are exactly the same. Which one to use is really a case of preference. I'd say pick the one you find more readable.
I have inserted the following TEXT value into mysql..
$_POST['groupname'] = "Linda's Group";
$groupname = $addslashes($_POST['groupname'];
then retrieve it like this..
$groupname = $row['groupname'];
when I echo it it shows up correctly as "Linda's group"
but when I put it into..
echo "<input name='groupname' type='hidden' value='$groupname' />";
it shows up as "Linda", only show text before the apostrophe
What am I doing wrong?
Whenever you want to send a string to the browser, use htmlspecialchars($string).
Replace that with:
echo "<input name='groupname' type='hidden' value='" . htmlspecialchars($groupname, ENT_QUOTES) . "' />";
... and remove the addslashes() call.
Also, make sure you always use mysql_real_escape_string($string) when inserting string values in a MySQL database.
you need to escape content before displaying it on a web page. your markup reads:
try:
htmlspecialchars($dataToEscape, ENT_QUOTES, 'UTF-8' false)
without this you are vulnerable to XSS.
consider Linda'><script src='evil.js'></script>s Group this example:
<input name='groupname' type='hidden' value='Linda'><script src='evil.js'></script>s Group' />
You need to escape your single quote before you use it in your HTML. This is because your attribute values are surrounded by single quotes.
If you didn't escape the quote, the generated HTML is:
<input name='groupname' type='text' value='Linda's group' />
This is not valid. What you need is:
<input name='groupname' type='text' value='Linda's group' />
You can get that by calling htmlspecialchars:
echo "<input name='groupname' type='hidden' value='" . htmlspecialchars($groupname) . "' />";
This also prevents XSS attacks. Otherwise someone could enter '/><script type="text/javascript">alert("omg hax!")</script><br' for example, and you would get an alert on your page.