So i have these radio buttons in XHTML that I want to put into a PHP function to generate and I can't get it to work.
In XHTML it looks like this and is working;
<p><input type="radio" value="<? echo blabla; ?>" name="radioA" checked="checked" /></p>
<input type="hidden" value="<? echo $value; ?>" name="hiddenA[]" />
In PHP I need to set the "radioA" and "hiddenA" to variables respectively "radioB"/"hiddenB", "radioC"/"hiddenC" and so on for my code to work. This is what I have so far but it is not working. The first radio name is a string, but the second one is array. Thanks in advance.
function radio($Radio, $Array) {
echo '<p><input type="radio" value="$value>" name="$Radio" />', $value, '</p>';
echo '<input type="hidden" value="$value" name="$Array" />';
}
I guess what I'm trying to do is to return the name of the variable as a string. $_POST['hiddenA'] ===> hiddenA[]
Basic php syntax:
$a = 'hello';
echo '$a'; // outputs the literal characters $ and a
echo "$a"; // outputs "hello"
You have a few syntax issues here:
function radio($Radio, $Array) {
echo '<p><input type="radio" value="$Value>" name="$Radio" />', $Value, '</p>';
echo '<input type="hidden" value="$Value" name="$Array" />';
}
You have to use "" not '' when embedding variables:
echo "My name is {$name}.";
In PHP the . character is used to append strings not ,
echo "My name is " . $name . " and I am cool";
If you try to echo an array you will get the word Array not the array itself. Instead you can echo a value in the array:
echo "$array[0]";
You are trying to use a variable that is not defined in your example: $Value. This would give you an VariableUndefined exception.
It is generally bad practice to use capital letters at the start of variable names in PHP. So $Array should be $array.
With these in mind you code should look something like:
function radio($radio, $array) {
$value1 = $array[0];
$value2 = $array[1];
echo "<p><input type=\"radio\" value=\"$value1>\" name=\"$radio\" />$value1</p>";
echo "<input type=\"hidden\" value=\"$value2\" name=\"$value2\" />";
}
From the sounds of your example you need a for loop somewhere.
Related
I'm generating a html table. Each row starts with a Edit-Button.
Site A:
echo "<table border='1' align='center'>";
echo "<tr>"
. "<th style='font-weight:bold'></th>"
. "<th>".$nr.":</th>"
. "<th>Anschrift</th><th>Nachname</th>"
. "<th>Vorname</th>"
. " <th>PLZ</th>"
. "<th>Ort</th>"
. "</tr>";
echo "<form action='lxKundenEdit.php' method='POST'>";
$i=0;
foreach($arr as $key =>$value)
{
echo "<tr><td><input type='submit' name='btn'.$i.'\'' value='Bearbeiten'/></td>";
foreach($value as $subkey=>$subValue)
{
echo "<td>".$subValue."</td>";
}
echo "</tr>";
$i++;
}
echo "</form>";
echo "</table>";
Then I want to know which button has been pushed. When vardump the POST-Array it seems nothing really works. Any hints about this? regards, Ismir
Site B:
var_dump($_POST['btn0']); //f.e.
Simply pass the sender ID in a hidden input
<input type="hidden" name="sender" value="<?= $senderID; ?>">
On Site B you can then get the value from the $_POST variable:
echo $_POST['sender'];
Edit
I see I misread your question. What you can do is pass your subkey on the submit button as follows:
<input type="submit" name="submit[<?= $subkey; ?>]" value="send" />
You should give each button a unique value. When the form is posted you can then check for that value for that button.
echo '<input type="submit" name="btn'.$i.'" value="'.$i.'" />';
Edit:
As Peter pointed out in the comments, if you want to change the text of your button, you can use the button element:
<?php
echo '<form method="post">';
echo '<button type="submit" value="button 1 was used" name="button">Send</button>';
echo '<button type="submit" value="button 2 was used" name="button">Send</button>';
echo '</form>';
var_dump( $_POST[ 'button' ] );
All you need to do is:
Make array of all your submit buttons with ids as keys.
echo '<tr><td><input type="submit" name="btn[<?php echo $i;?>]" value="Bearbeiten"/></td>';
And then in PHP, get posted submit buttons like:
if (! empty($_POST['btn']) {
foreach ($_POST['btn'] as $btnId => $btnVal) {
// $btnId is Id of the button that is $i
// $btnId means button is pressed.
// $btnVal is value of the button
}
}
Just include a hidden input inside your form, eg
<input type='hidden' name='submit'/>
So you can use it to check, if the form has been sent (if(isset($_POST['submit'])) etc). Then check any other $_POST fields looking for any ['btn...'], eg
foreach($_POST as $key => $value){ // check all the $_POST fields
if(substr($key,0,3) == 'btn'){ // if its name begins with 'btn'...
$button_id = substr($key,3,NULL); // then that's some button. Get its ID
}
}
Think this should work
<form method="POST" action="include/crud.php" enctype="multipart/form-data" >
<?php
foreach (LoadAnouncements() as $value){
/*echo "<div id='bb'></div>";*/
echo "<hr/>";
echo $value['searchresultwhat'];
echo "<br/>\n";
echo $value['searchresultwhen'];
echo "<br/>\n";
echo $value['searchresultwhere'];
echo "<hr/>";
/*echo "<div id='bb'></div>";*/
}
?>
</form>
I have this form that show an echo is there a way that I can add the attribute name in this echo so I can use it to query the database? I search for ideas if putting attribute to echo is possible but I haven't found anything yet any suggestion is appreciated
You are looking it?
<input type="text" name="searchresultwhat[]" value="<?php echo $value['searchresultwhat']; ?>" />
<input type="text" name="searchresultwhen[]" value="<?php echo $value['searchresultwhen']; ?>" />
<input type="text" name="searchresultwhere[]" value="<?php echo $value['searchresultwhere']; ?>" />
Edited
<?php
echo '<table>';
echo '<tr><th>What </th><th> When</th><th> Where</th><tr>';
foreach (LoadAnouncements() as $value){
echo "<tr><td>".$value['searchresultwhat']."</td>
<td>"$value['searchresultwhen']."</td>
<td>".$value['searchresultwhere']."</td>
</tr>";
}
echo '</table>';
?>
Do you want to add attribute to certain value in PHP? But you can't do it because $value['searchresultwhat'] and others of the kind are simple strings - as is! They can't send separated data (or attributes) with themself.
EDIT:
You can send seprated data about certain value using array:
$value['searchresultwhat'] = array("name", "value");
$value['searchresultwhen'] = array("name2", "2014-09-01");
$value['searchresultwhere'] = array("name3", "kittens");
echo $value['searchresultwhat'][0] . " is " . $value['searchresultwhat'][1];
echo $value['searchresultwhen'][0] . " = " . $value['searchresultwhen'][1];
echo $value['searchresultwhere'][0] . " has " . $value['searchresultwhere'][1];
I am trying to find out and track what browser and if a proxy is being used when I receive form submissions. I have the following code below:
<?php
$browser = $_SERVER['HTTP_USER_AGENT'];
$ip_address = $_SERVER['REMOTE_ADDR'];
if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) {
$ip_address = array_pop(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']));
}
?>
For example I tried the below but it does not work. It is not submitting any data with my form.
What am I doing wrong?
echo ('<input type="hidden" name="browser" value="' . $browser . '" />' );
echo '<input type="hidden" name="browser" value="'.$browser.'">'
What am I doing wrong?
You are not referencing the variable with a $
You need to correctly escape the variable value from the string
Try printf
printf('<input type="hidden" name="browser" value="%s">', $browser);
You are combining echo and print (which both do the same) into one call. For another thing, you haven't closed the input element HTML (notice the final greater than sign I've added):
echo ('<input type="hidden" name="browser" value="' . $browser . '" />' );
Try this way
echo ("<input type='hidden' name='browser' value='".$browser."'");
echo ('<input type="hidden" name="browser" value="'.$browser.'">');
Try with this -> value="'.$browser.'" something like that
I am creating a form based on a user selection of $node_number ... so the form looks like:
function createForm($node_number) {
echo "<form id=\"form\" name=\"form\" action=\"molecule_display.php\" method=\"post\">";
for ($n = 1; $n <= $node_number; $n++) {
echo "<fieldset class=\"step\">
<input id=\"node_title" . $n . "\" name=\"node_title" . $n . "\" />
<input id=\"node_comment" . $n . "\" name=\"node_comment" . $n . "\" type=\"textarea\" />
</fieldset>";
}
echo "<input type=\"hidden\" name=\"node_number\" value=\"" . $node_number . "\">
<button id=\"submit_node\" type=\"submit\">Submit</button>"
echo "</form>";
}
Which will create $node_number of versions of that form element. My question is how to dynamically name the form elements to be able to manage them easier when I am processing them. The way I'm doing it right now, by adding the $n iterator to the name attribute is not ideal I think.
I understand that I can declare the name="" attribute as an array like name[]="" ... in terms of giving each sub-element of the larger form a unique name.
I'm guessing I want a multi-dimensional array of the individual form segment ... just not sure how to best handle those within a form and within the $_POST variable.
Does anyone have any suggestions?
I think you can do it this way:
function createForm($node_number) {
echo '<form id="form" name="form" action="molecule_display.php" method="post">';
for ($n = 1; $n <= $node_number; $n++) {
echo '<fieldset class="step">
<input id="node_title'.$n.'" name="nodes['.$n.'][node_title]" />
<input id="node_comment'.$n.'" name="nodes['.$n.'][node_comment]" type="textarea" />
<button id="submit_node" type="submit">Submit</button></p>
</fieldset>';
}
echo '</form>';
}
And then get $_POST['nodes'] which will be multidimensional array, which you can iterate with foreach. You will get $_POST['nodes'][1] = array('node_title'=>... , 'node_comment'=>...); and so on.
If you use the array like you were saying in your post you should be able to access them pretty easily.
function createForm($node_number) {
echo "<form id=\"form\" name=\"form\" action=\"molecule_display.php\" method=\"post\">";
for ($n = 1; $n <= $node_number; $n++) {
echo "<fieldset class=\"step\">
<input id=\"node_title_" . $n . "\" name=\"node_title[" . $n . "]\" />
<input id=\"node_comment_" . $n . "\" name=\"node_comment[" . $n . "]\" type=\"textarea\" />
<button name=\"submit_node[" . $n . "]\" type=\"submit\">Submit</button></p>
</fieldset>";
}
echo "</form>";
}
I also changed the submit_node to a name and gave it an array value because an ID must be unique, which will cause errors if you are referencing it somewhere.
You could loop through the results like this:
foreach ($_POST['node_title'] as $key => $response) {
$title = $response;
$comment = (!empty($_POST['node_comment'][$key])) ? $_POST['node_comment'][$key] : "";
// Save title / comment here.
}
Since every form has its own submit button, nothing stops you from using name="node_title" in all of them. If you now add <input type="hidden" name="index" value="$n"> and read this first, your logic becomes very easy.
i'm trying to select values from the database and place each and every one on a separate text input field. my code works properly, however, when i tried to display a string on a text field, the string is cut or trimmed on its very first space character. for example:
value #1: "my-picture.jpg"
value #2: "my name"
if i were to place the two values above and insert them inside a text field, the output would be like this:
value #1: my-picture.jpg
value #2: my
this is the code i'm working on:
<?php
$counter = 0;
while ($newArray = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
echo "
<input type='text' value=".$newArray['my_picture'].">
<input type='text' value=".$newArray['my_name'].">
";
?>
What is wrong? Thanks for any help.
You need to quote your values in your HTML form:
echo '
<input type="text" value="'.$newArray['my_picture'].'">
<input type="text" value="'.$newArray['my_name'].'">
';
Also for good measure it would be a good idea to use htmlentities() or htmlspecialchars() on the values in your variables to encode special characters:
echo '<input type="text" value="' . htmlentities($newArray['my_picture']) . '">
<input type="text" value="' . htmlentities($newArray['my_name']) . '">';
Try viewing the source.. That says:
<input type='text' value=the content of your variable>
Where 'the content of your variable' needs to be surrounded by quotes, naturally. So; change it into:
<?php
$counter = 0;
while ($newArray = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
echo '<input type="text" value="' . $newArray['my_picture'] . '">';
echo '<input type="text" value="' . $newArray['my_name'] . '">';
}
?>
You need to surround your value with quotes. Something like
echo "
<input type='text' value='".$newArray['my_picture']."'>
<input type='text' value='".$newArray['my_name']."'>
";