How to make a dynamic textbox name in PHP - php

`$row_data .='<input type="textbox" name="left + $i" />'; `
$pst[] = $_POST['left'];
i++;
how to create dynamic textbox and store it in array

Variables don't expand in single-quoted strings:
'<input type="textbox" name="left + $i" />'
So if you really want to do this, you could make it double-quoted and use escape characters for the inner double-quotes, though I prefer concatenation in cases like this:
'<input type="textbox" name="left' . $i . '" />'
Then you'd get the values from the $_POST array with the same technique:
$someVariable = $_POST['name' . $i];
It would be your responsibility to track the values of $i between rendering the form and receiving the form values.
However, consider an alternate approach entirely:
'<input type="textbox" name="left[]" />'
What this does is submit all of the values in the name="left[]" elements as an array to the server. So $_POST['left'] would contain an array instead of a single value.

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"] . '" >';

Php add value to input field?

Is it possible to add a value to a dynamically created input field?
Im trying something like this: echo "<input value="$row['test']">" but that gives me a syntax error. It has to be in my php file since im calling it via ajax and want to keep my html and php seperate.
Im getting content via ajax and I need to set many field names as there are records in the database.
You can do it like this:
echo '<input value="' . $row['test'] . '">';
Alternatively escape the " (not recommended if not needed, because it is hardly readable):
echo "<input value=\"" . $row['test'] . "\">";
Otherwise you’re mixing quotation marks, which is a syntax error. The . is to combine strings with variables (in this case).
You can also Use:
<input value="<?php echo htmlspecialchars($row['test']); ?>" >
OR
echo "<input name='test' value=".$row['test'].">";
OR
echo "<input name='fred' value='{$row['test']}'>";
Reference
When using certain php values within a quoted string, such as the array syntax in the question, you should either escape from the quotes or encapsulate the variable in curly braces. Also, as the string was quoted with double quotes you should use single quotes around attributes and values.
echo "<input name='fred' value='{$row['test']}'>";
<input type="text" name="post_title" class="form-control" value="<?php
if(isset($post_title))echo $post_title;
?>">
If you want to add it into the HTML

How to call a function (stripslashes) within a PHP string?

I have the following code
<input type="text" id="chapter" name="chapter" value="'.$chapter_title.'"/>
I want to add stripslashes to the '.$chapter_title.'
Would I do something like this.
<input type="text" id="chapter" name="chapter" value="stripslashes'.$chapter_title.'"/>
How would I do this - not too sure where to put brackets etc.
Your question is much clearer when you post the whole line. The HTML is part of a string:
$some_string = '<input type="text" id="chapter" name="chapter" value="'.$chapter_title.'"/>';
It doesn't really matter that it's HTML for this question, so let's make the example shorter:
$some_string = 'abc ' . $chapter_title . ' !!!';
That's better. Without HTML's double-quotes, it's much clearer what's going on. The string $some_string is the string 'abc ', concatenated to the PHP variable $chapter_title, concatenated to the string ' !!!'.
In fact, any PHP expression will do, not just an expression. In this case you want to concatenate the value of stripslashes($chapter_title) rather than just $chapter_title itself, so:
$some_string = 'abc ' . stripslashes($chapter_title) . ' !!!';
Putting the HTML back in:
$some_string = '<input type="text" id="chapter" name="chapter" value="' . stripslashes($chapter_title) . '"/>';
There is plenty of great recommended reading on PHP. I suggest picking up a book with good reviews and going through it. Twice.
You have to pass $chapter_title as an argument to stripslashes:
<input type="text" id="chapter" name="chapter" value="'.stripslashes($chapter_title).'"/>
Further readings:
http://en.wikipedia.org/wiki/PHP_syntax_and_semantics#Functions
<input type="text" id="chapter" name="chapter" value="' . stripslashes($chapter_title) .'"/>
You used ' to tell php that the HTML string ends. Than append the value of the function stripslashes, which gets the $chapter_title as input argument. And than append the rest of the HTML string.

Insert value into a text box which is inside an echo statement

I have this code basis:
echo '<input required type = "text" name = "subject1" value="XXX" />';
However, I would like XXX to be a variable, I have searched this online, but all the things that one of the discussions says is to do this:
value=\"$firstName\"
or
value='$firstName'
I have tried both of these, but they don't work, and I was hoping that someone could help me with this problem, basically, what I want is to be able to asign the value of a text edit to a variable in php, but when the text edit itself, is embedded in an echo, nothing seems to work.
Thanks
Concatenation is a pretty easy way to go about this:
echo '<input required type = "text" name = "subject1" value="' . $foo . '" />'
edit: Those spaces around the concatenation .'s (. $foo .) aren't required - I just add them fore readability.
I think the problem is you are using single quotes, so variables are not rendered automatically... you can concat with . like this:
echo 'some text' . $variable . 'some more text';
echo '<input required type="text" name = "subject1 value="<?php echo $value; ?>" />';
acutally you don't need the php echo .... stuff.... /* brain not engaged */

how can I recieve this form in cart.php

$index = 1;
foreach($product['varieties'] as $variety){
echo '<input style="width:10px; margin-left:9px; " name="price_' . $index . '" type="checkbox" value="' . $variety['price']. '" />';
echo '<input name="size_' . $index . '" type="text" value="' . $variety['size']. '" />'; $index++;
}
if you can see This will have an index=1 and it will be incrementing where each iteration will be price_1, price_2, etc.and size_1,size_2. Now with a dynamic name="" input how can I receive in the cart.php when each name will be different?
it would be something like $price= "'..',$_POST['price_']"? well I have not idea how can I receive this name index from the cart.php url.
Thnank you.
Instead of string building the name like size_1, why not make the name like this size[].
Then you can instantly access it like an array via PHP.
Look at alex's answer. It is better suited to the circumstances..
You could, at the end, add a hidden text box with the number of items (the $index value). and read it back
so after the loop add
echo '<input name="counter" type="hidden" value="' . $index. '" />'
and when you try to read it, the first thing would be to read the $POST['counter'] and then loop again from 1 to that value reading the $POST['price_'.$loop_counter]
You could find the values with something like this:
$prices = preg_grep('/^price_\d+$/', $_POST);
foreach($prices as $P) {
$idx = substr($p, 5); // extract the digits, which we know will be at position 5->end
$size = $_POST['size_' . $idx];
etc....
}
This assumes for that for every price_#, there's a corresponding size_#

Categories