How to get json value of input form in codeigniter - php

I have a json variable and I want to post it as a json format.
$json_value = json_encode($my_array);
...
echo '<input name="myvalue" type="hidden" value="'.$json_value.'" />';
...
in my function :
$posts = $this->input->post();
echo $posts['myvalue'];
but it is empty .
my form works correctly as I can fetch other values of inputs .

Change Your input to use single quotes inside
echo "<input name='myvalue' type='hidden' value='$json_value' />";
since you are using double quotes inside and $json_value also has double quotes so there is a conflict.

json_encode encoded values have quotes. So your hidden element value will break when first quote appears. There are some other methods to send php array using forms. I am adding one method here.
//On html side
foreach($my_array as $value) {
echo '<input name="myvalue[]" type="hidden" value="'.$value.'" />';
}
//On posted server side
$posts = $this->input->post();
$my_array = $posts['myvalue']; //<-- you can get your array here

Related

php use hidden post to pass dynamic data to another php file

I'm trying to build a comment system, each comment has a unique id, many comments can be associated with a post, and each post has a unique id. I want to pass the post id to a submit.php file (where comments are update to the database), but no matter what I tried I just can't pass the data. Currently I have something like this:
$sql="SELECT postid,post,pdate FROM posts";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo '$row["post"]';
echo '<form action="submit.php" method="POST">';
echo '<input name="comment" type="text" id="comments"></input>';
echo '<input type="hidden" name="id" value="$row["postid"]" />';
echo '<input type="submit" value="enter comments" />';
}
?>
for testing purpose I have submit.php as follows,
<?php
$ha=$_POST['id'];
echo $ha;
?>
data of postid is not passed, and I just got "$row[" as output.
inside the while loop if I say $haha=$row["postid"]; echo "$haha"; then each individual post id will be printed correctly, but I just cannot pass the data to submit.php file.
update: I just changed my code to :
echo '<input type="hidden" name="id" value="' . $row["postid"] . '" />';
Now a number is succesfully passed to submit.php, the problem is ,it's always "3". My post id ranges from 3 to 13, post with id=3 is at the bottom of the page and post with id=13 is at the top.However,if I write a comment at the post with id=13(same issue occur to other posts as well), after clicking submit, the data passed to submit.php is always 3. Is there something wrong with the while loop?
Another update: it's always 3 because i forget to close the form tag, now everything worked perfectly
you're using single quote, so you cannot insert variables inside of string, use
echo '<input type="hidden" name="id" value="' . $row["postid"] . '" />';
In PHP, you can wrap a string in single-quotes ('), or double quotes (").
When you use single quotes, the string is not interpreted - this means that all the characters are left intact, and no variables are parsed.
When you use double quotes, any variables in the string will be replaced with their value.
In your case, you're using single quotes, so your variable is not being interpreted and converted. Instead, use double quotes:
$sql="SELECT postid,post,pdate FROM posts";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo $row["post"];
echo '<form action="submit.php" method="POST">';
echo '<input name="comment" type="text" id="comments"></input>';
echo "<input type=\"hidden\" name=\"id\" value=\"{$row["postid"]}\" />";
//Alternatively, keep the single quotes and use the concatenation method:
//echo '<input type="hidden" name="id" value="' . $row["postid"] . '" />';
echo '<input type="submit" value="enter comments" />';
//Also, if you're opening a form tag in this loop, be sure to close it
echo '</form>';
}
Some other things to note:
When you use double quotes to wrap your string, and you have double quotes inside your string, you must escape them (using a \). Notice name="id" became name=\"id\"; and
When referencing an item in an array within a string, you can either use string concatenation to ensure the full variable is interpreted correctly (value=\"" . $row["postid"] . "\"), or you can leave the variable in place and wrap it in curly brackets - which is my preference and is what is used above. If you're going to use the concatenation method, then you can keep the single quotes wrapping everything else - there are no variables to parse.
When echoing a variable value, you don't need to wrap it in anything - notice I removed the quotes from the first echo.
Here is PHP's documentation on strings, including single and double quoted strings: http://php.net/manual/en/language.types.string.php.
And here is PHP's documentation on string operators: http://php.net/manual/en/language.operators.string.php.
Try adding a conditional at the start of your file like this just to be sure the form is actually submitted properly:
<?php
if(isset($_POST['submit_form'])) {
$ha=$_POST['id'];
echo $ha;
}
with your button like this
<input type="submit" value="enter comments" name="submit_form"/>
and please close your form tag.
You can pass value as follow,
echo '<input type="hidden" name="id" value="' . $row["postid"] . '" >';

POST the value of an array index stored in hidden fields

I am creating a wordsearch, I'm trying to post the value in another page, The problem is that I cannot display all the value of $thisChar to another page, all I get is the last letter. So the question is how can you post a variable that is something like this e.g., $rc[$r][$c]...
This is my form. I have hidden fields that named thisChar.
echo '<form method="post" action="#path" target="blank">';
echo '<table>';
#--Display the random letters and the words
for ($r=0;$r<=$row;$r++) {
echo '<tr>';
for ($c=0;$c<=$col;$c++) {
$thisChar=strtoupper($rc[$r][$c]);
echo '<input type="hidden" name="thisChar" value="'. $thisChar.'">';
echo '<td style="background:#fff">' . $thisChar . '</td>';
}
echo '</tr>';
}
echo '</table>';
echo '<input type="submit" name="submit">';
echo '</form>';
This is how I fetch the post data. All I get is Uninitialized offset error.
for ($r=0;$r<=$row;$r++) {
for ($c=0;$c<=$col;$c++) {
$thisChar=$_POST['thisChar'];
$pdf->Cell(10,10, strtoupper($thisChar) ,1,0,'C', );
}
}
I already tested other approach like foreach loop and adding square brackets on the name of hidden fields and it works, now I want to make it work using this approach. This method creates a loop that can display in a table. Any idea how can I make it works?
You need to post it as array, so your name attribute should look like this:
echo '<input type="hidden" name="thisChar[]" value="'. $thisChar.'">';
haven't check the rest of your code, but this will send all the values as an array
Use html input name as an array!
echo '<input type="hidden" name="thisChar[]" value="'.$thisChar.'">';
Then retrieve this variable in action PHP file as array:
$thisChars=$_POST['thisChar'];
foreach($thisChars as $thisChar)
{
...
}

Echo an input field with an echo POSTBACK value

So I'm iterating through a set value via a for loop, which will echo html input fields, every other echo'd html input field behaves as expected (including the name and id fields of the one below), however I keep getting syntax errors when trying to set the value as a postback to retain them on page submit.
Here is my code:
$type = "number".$i;
echo '<input type="text" name="'.$type.'" id="'.$type.'" value="'.<?php if (isset($_POST[$type])) { echo $_POST[$type]; } else { echo NULL;}.'" />';
Thanks in advance.
You have an extra <?php statement in the row. Since the line is echo '...'. you don't need to declare that more php code is coming. You can do something like this instead:
echo '<input type="text" name="'.$type.'" id="'.$type.'" value="';
if (isset($_POST[$type])) echo $_POST[$type];
echo '" />';
Although personally, I prefer to do something like <input [...] id="{$type}" [...]" outside of the php code, less messy.

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

Is there a way I can send a variable to a database from a form's action attribute value?

So I have a php file that retrieves some variables with the $_GET method and then outputs the result. I would want to send one of these retrieved variables to another php file (if it was possible to the same file would be fine also) through a form along with other variables which then are stored in a database. I tried putting the variable in the "action" attribute of the form like this but it didn't appear in the url when I submitted the form values:
Here is all my code:
<?php
$nome=$_GET[nome];
$cognome=$_GET[cognome];
echo "<form action='salva_citazione.php"."?autore=".$nome."+".$cognome."&' method='GET'>"
....
You can use hidden input fields, also you forgot to use ' in your $_GET variables:
<?php
$nome = htmlspecialchars($_GET['nome']);
$cognome = htmlspecialchars($_GET['cognome']);
$fullname = $nome.' '.$cognome;
echo '<form action="salva_citazione.php" method="GET">';
echo '<input type="hidden" name="autore" value="'.$fullname.'" />';
// ...
?>
Note that this way of using $_GET results in XSS vulnerabilities, so I've used htmlspecialchars function to convert special characters to HTML entities.
Put this inside the form:
echo "<input type='hidden' name='autore' value='$nome+$cognome'/>";

Categories