Passing hidden values, html - php

I have a the following php file. displayitems.php
<?php
*
*
*
echo "<form action='http://retailthree.nn4m.co.uk/alex/add_data.html'>";
echo "<input type='hidden' name='value' value='$value'/>";
echo "<button type='submit'>Add</button>";
echo "</form>";
?>
Then the html file. add_data.html:
<form method="post" name="form">
<table id="mytable" border="1">
<tr>
<td>Trend <input type="text" name="trend" size="20"></td>
//many other fields
</tr>
</table>
</forn>
Then the aforementioned html will perform an action on a php file.
However that I want to achieve is to pass the hidden data ->$value from the first php file, to the Trend input box(to print the $value content to the input box). Is this possible?

You would simple use the posted variable and place it in the value attribute of your <input> like so:
<input type="text" value="<?php echo $_GET['value'] ?>" name="trend" size="20">
Of course you should do some validation before echoing it to the <input>
EDIT:
Quite rightly mentioned by #ocanal - GET is the default method for forms. You will not be able to process these forms with PHP if your file is *.html it must be a *.php file.

change the name of add_data.html file to add_data.php use following code in add_data.php file
<?php
// your php code
?>
<form method="post" name="form">
<table id="mytable" border="1">
<tr>
<td>
Trend <input type="text" name="trend" size="20"
value="<?php echo $_POST['trend'] ?>">
</td>
//many other fields
</tr>
</table>
</forn>

I'm a little lost but assuming you mean that you want the hidden value to appear in a text input field on another page I would suggest this:
HTML page
<form name='form' action='yourPhpFile.php' method='POST'>
<input name='hiddenGuy' type='hidden' value='hello from the hidden guy'/>
<input type='submit' value='Send'/>
</from>
Now for your php file called yourPhpFile.php
<?php
//your value from the hidden field will be held in the array $_POST from the previous document.
//the key depends on your field's name.
$val = $_POST['hiddenGuy'];
echo "<form name='form' action='yourPhpFile.php' method='POST'>
<input name='showingInText' type='text' value='".$val."'/>
</from>";
?>
This can be achieved on the same page too by removing the form action attribute. and echoing the a different input type and value depending on whether $_POST is set using the isset method.
if(isset($_POST['hiddenGuy'])){
echo "<input name='showingInText' type='text' value='".$_POST['hiddenGuy']."'/>";
}
else{
echo "<input name='hiddenGuy' type='hidden' value='hello from the hidden guy'/>
<input type='submit' value='Send'/>";
}

Related

Get input textbox value into a PHP variable in WordPress

I am trying to get a input textbox value in a PHP variable and then update a table based on this variable value in my custom WordPress page. I have written following code.
It is working but not updating the text input value. What is wrong with my code?
<form method="post" enctype="multipart/form-data">
<td>
<table> <tr><td> </td><input type="text" name="offeramt" style="height:15px"/> <td>
<input type="hidden" name="pid" value= "<?php echo $retrieved_data->id; ?>">
<input type="hidden" name="textamount" value="<?php echo htmlspecialchars($_POST['offeramt']);?>">
<input type="submit" name="update" value="update" /></td> </table></td>
</form>
<?php
if (isset($_POST['update'])) {
$sellamt=$_POST['textamount'];
if (empty($sellamt)){
echo "pls input a value in text box";
}
else{
$myid = $_POST['pid'];
?> <?php
$wpdb->update('wp_share', array('status'=>'onsell','offervalue'=>$sellamt),array('id'=>$myid));
}}
}?>
I was updating the text input value to a hidden field ["textamount"]then trying to get that hidden field ["textamount"] value to $sellamt. However I have removed the hidden field and took the value directly from textbox like this
$sellamt=$_POST['offeramt'];
where ['offeramt'] is the textbox itself
and its working.Thanks

How to post table with a form in php?

I have a form with a table (values in it can be edited) in it, like this:
<form action="file.php" id="myform" method="POST">
<input name="inp">
<div class="divclass">
<table id="mytable" name="mytable">
<tr><th>col1</th><th>col2</th></tr>
<tr><td contenteditable='true'>val11</td>
<td contenteditable='true'>val12</td></tr>
<tr><td contenteditable='true'>val21</td>
<td contenteditable='true'>val21</td></tr>
</table>
</div>
<input type='submit' name='submit' value='Submit'>
</form>
<?php
print_r ($_POST)
?>
So, when I click on submit, the entire form should be submitted right?
But just the inputs - inp & submit get submitted to the form, which I know when I print the post parameter array in php.
Can someone please explain how I can get the table data too in my post parameter array $_POST?
Also how do I trigger an onchange event on the table? I tried this, but it didn't seem to work :
$("#mytable").change(function (event) {
console.log("changed")
console.log($("#keno_config_table").val())
})
When you submit a form only the values inside selector elements gets submitted not all html elements.
However, if you can add an input tag inside <td> tag you'll be able to submit the values.The following code should work for you.
<form action="file.php" id="myform" method="POST">
<input name="inp">
<div class="divclass">
<table id="mytable" name="mytable">
<tr><th>col1</th><th>col2</th></tr>
<tr><td contenteditable='true'><input type="hidden" value="val11" name="value1"> val11</td>
<td contenteditable='true'><input type="hidden" value="val12" name="value2"> val12</td></tr>
<tr><td contenteditable='true'><input type="hidden" value="val21" name="value3"> val21</td>
<td contenteditable='true'><input type="hidden" value="val21" name="value4"> val21</td></tr>
</table>
</div>
<input type='submit' name='submit' value='Submit'>
</form>
jQuery
$("#mytable input[type='text']").change(function (event) {
console.log("changed")
console.log($(this).val())
});
PHP
<?php
if (!empty($_POST)){
echo "Post data received";
echo "Value 1 :".$_POST["value1"];
echo "Value 2 :".$_POST["value2"];
echo "Value 3 :".$_POST["value3"];
echo "Value 4 :".$_POST["value4"];
}else{
echo "Post data is empty. No value is passed from HTML to the server";
}
?>
So, when I click on submit, the entire form should be submitted right?
No. It isn't a form control.
But just the inputs - inp & submit get submitted to the form
inputs are form controls (as are selects, textareas and buttons).
Can someone please explain how I can get the table data too in my post parameter array $_POST?
Using JavaScript, listen for a submit event on the form element and copy the HTML of the table into the value property of a hidden input.
Alternatively: Use input elements in your table cells instead of using contentEditable.
I offer you to do this
<form action="file.php" id="myform" method="POST">
<input name="inp">
<div class="divclass">
<table id="mytable" name="mytable">
<tr>
<th>col1</th>
<th>col2</th>
</tr>
<tr>
<td><input name='col1[]' style='border:0;outline:0;display:inline-block' value='value'></td>
<td><input name='col2[]' style='border:0;outline:0;display:inline-block' value='value'></td>
</tr>
<tr>
<td><input name='col1[]' style='border:0;outline:0;display:inline-block' value='value'></td>
<td><input name='col2[]' style='border:0;outline:0;display:inline-block' value='value'></td>
</tr>
</table>
</div>
<input type='submit' name='submit' value='Submit'>
</form>
(of course, make a class for styles, I just wanted to do it simpler). Inputs will look like contenteditable tags without borders and you will be able to get an array of all col1 and col2 like $_POST['col1'] and $_POST['col2'] Also, you may change values to placeholders if you don't want default data to be sent.

HTML form's "action" doesnt carry over my $_GET data

I have a HTML form that acts as a "confirm deletion?" page and my "Back" button is playing up.
$string = "foo.php?id=" . $_POST['fooid'] . "&id2=" . $_POST['barid'];
<!-- ^^ this ends up being "foo.php?id=1&id2=2" -->
<form action='<?php echo $string; ?>'>
<input type='submit' value='Back'>
</form>
the problem is, when the button is pressed it links to foo.php without any of the $_GET data in my string, even though the string contains "id=1&id2=2"
P.S I changed the code above so people could better understand it, here is the raw code:
<?php
$string = "xrays.php?id=" . $_POST['visitid'] . "&id2=" . $_POST['patientid'];
?>
<form action='delete.php' method='post'>
<input type='hidden' name='xrayid' value='<?php $_POST['xrayid']?>'>
<input type='submit' name='submit' value='Confirm Delete?'>
</form>
<form action='<?php echo $string; ?>'>
<input type='submit' value='Back'>
</form>
Maybe it's not the best answer but I would like do something like that:
$string = "foo.php?id=" . $_POST['fooid'] . "&id2=" . $_POST['barid'];
<!-- ^^ this ends up being "foo.php?id=1&id2=2" -->
<form action='foo.php'>
<input type="hidden" name="fooid" value ="<php echo $_POST['fooid']; ?>" />
<input type="hidden" name="barid" value ="<php echo $_POST['barid']; ?>" />
<input type='submit' value='Back'>
</form>
This should work properly.
EDIT: change $_GET na $_POST
You need to put the get variable in the form hidden inputs, like so:
<form action='foo.php'>
<input type="hidden" name="id" value="1" />
<input type="hidden" name="id2" value="2" />
<input type='submit' value='Back'>
</form>
Or you could use a link:
Back
Let's start with form submission in general. W3C: Form Submission
Next, let's review $_GET and $_POST. PHP Manual: $_GET | PHP Manual: $_POST
In summary, inside of your <form> tag, use either method="get" or method="post". Only one of the superglobal arrays will be populated by successful controls, based upon your method of sending the data. I believe the query string must result from a GET request url (which may be the default), not just a plain string slapped into the action="" attribute. I could be wrong about the query string, but you have another problem. You are using two forms on one page. Presently, I think only one form's controls can be submitted successful at a time.
<form action='delete.php' method='post'> <!-- FORM 1 -->
<input type='hidden' name='xrayid' value='<?php $_POST['xrayid']?>'>
<input type='submit' name='submit' value='Confirm Delete?'>
</form>
<form action='<?php echo $string; ?>'> <!-- FORM 2, add a method="" attribute -->
<input type='submit' value='Back'>
</form>
Upon adding a method="get" to form two, it should become clear that a composite $_POST + $_GET request is not possible in the two form approach, and that you need to start with making a single, monolithic form instead of two modular ones. Using the type="hidden" attribute of an <input /> tag, inside of one form, as in #machineaddict's answer, will help. However, what will really help is if you explicitly use all the correct attributes of each tag so that you can spot errors like this in the future.
In a situation like this, it is helpful to know that the $_SERVER['QUERY_STRING'] element would hold the complete query string if your web server received one.
PHP Manual: $_SERVER

Using php in Form Action

here is my html code:
form action="add_answer.php?id=<?php echo $id;?>" form method="post" class="login">
The issue is that it directs me to the link: xxxxxx/add_answer.php?id=%3C?echo%20$id?%3E
The problem with that link is that it inserts the php code instead of echoing the variables value. Why is this happening?
Note: It does not actually have xxxxx in the link, I replaced the beginning part of the link sine it disallowed me to insert localserver into the link.
Don't do:
<form action="add_answer.php?id=<?php echo $id;?>" form method="post" class="login">
Do do:
<form action="add_answer.php" method="post" class="login">
<input type='hidden' name='id' value='<?php echo $id;?>' />
You should not include parameters in the action itself, but in the form. If you are echoing the line (a possible reason your PHP tag was not being parsed), do it like:
echo "<form action='add_answer.php' method='post' class='login'>";
echo "<input type='hidden' name='id' value='{$id}' />";

How to print a form for every row of a loop in Symfony2.

i want to have a Form in every row with the values: "accept" and "decline".
But now, I recognize it is not as easy as in a plain php project, where I can write something like this:
while($row = mysqli_fetch_assoc($result)) {
<tr>
<td>$row['name']</td>
<td>
<form method='POST' action='action.php'>
<input type='hidden' value='$row[id]'>
<input type='submit' value='accept'>
</td>
<td>
<form method='POST' action='action_decline.php'>
<input type='hidden' value='$row[id]'>
<input type='submit' value='decline'>
</td>
</tr>
}
I even know, that in Symfony2 it is possible to react with the method isClicked() on the various buttons, so I have not to create two different forms in each row.
But my problem is I can only send one form with the controller to the twig-template.
How is it possible in Symfony to generate dynamic forms in list (loops)?
Greetings Michael

Categories