Using php in Form Action - php

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}' />";

Related

Need help to use the following line of code to do a post method in html

my value is as follows , i need the value to do post method . I have tried the following but the post method does not work.
<input type="hidden" name="return_url" value="<?php echo URLROOT; ?>/shop/payment" >
echo '<form method="post" name="myform" action="script to check and manipulate your posts">
<input type="hidden" name="return_url" value="'.URLROOT.'/shop/payment" >
</form>';
you will get the return_url in POST if you submit this form, but you need to write a script which redirects to the url given in the return_url
let's say you have 2 files:
---------- form_file.php
<!DOCTYPE html>
<html>
<body>
<form method='post' name='myform' action='post_read.php'>
<input type='text' name='return_url' value='blablabla.php'>
<input type='submit' name='submit_btn' value='SUBMIT THIS FORM'>
</form>
</body>
</html>
---------- post_read.php
<?php
echo "POST VARIABLE: ".$_POST['return_url'];
?>
so the form_file.php will send the POST variables to post_read.php file. Of course if both files are in the same directory, otherwise give the proper address to the form_read.php in the "action" attribute of the form tag in the form_file.php
In my example the return_url input field is type TEXT to show you the value sent to the post_read.php file, but you can change the type to hidden with the same result in post_read.php

Get back HTML code from json encoded string

We are trying to get some HTML source code using an API written in PHP. The PHP code uses json_encode to encode the response and then echoes it. The response we receive is similar to this.
{"status":true,"message":"Form fetched.","html":"
<!DOCTYPE html>\n\n
<html>\n\n
<head>\n\n<\/head>\n\n
<body onload=\"document.form.submit()\">\n\n\t
<div>\n\t\t\t
<img src='https:\/\/www.example.com\/resource\/images\/loader.gif' width='300px' height='300px'\/>\n\t\t\tPlease wait while we redirect you...\n\t<\/div>\n\n\t
<form name='form' method='POST' action='https:\/\/test.test.com\/_payment'>\n\t\t
<input type='hidden' name='key' value='randomFFx'>\n\t\t
<input type='hidden' name='txnid' value='YHOOCH31DG90HeQpc8h7'>\n\t\t
<input type='hidden' name='productinfo' value='Adding to user.'>\n\t\t
<input type='hidden' name='amount' value='100'>\n\t\t
<input type='hidden' name='firstname' value='test'>\n\t\t
<input type='hidden' name='lastname' value='test'>\n\t\t
<input type='hidden' name='email' value='support#example.com'>\n\t\t
<\/form>\n<\/body>\n<\/html>"}
How do I get a proper HTML code without \n\t which I can show in the webview. Currently due to these, the form isn't getting submitted automatically. We have tried replacing \n, \t, \ with , but is there a better method to retrieve the HTML code. Thanks in advance for all your help and suggestions.

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

PHP Like Button Problems

I am trying to make a like button for posts on my website.
PHP for like query (d_db_update is
function d_db_update($string) {
return mysql_query($string);
}
)
if($_GET['like']) {
$like = d_db_update("UPDATE posts set post_rating = post_rating+1 WHERE post_id = {$_GET['like']}");
}
Button
<form action='{$_SERVER['PHP_SELF']}&like={$posts_row['post_id']}' method='get'>
<p align='left'>{$posts_row['post_rating']}
<input type='submit' name='like' value='Like' /></p>
</form>
What can I do to fix it/make it work?
Use below form with a hidden input it solve your problem.
<form action='{$_SERVER['PHP_SELF']}' method='get'>
<p align='left'>{$posts_row['post_rating']}
<input type='hidden' name='like' value='{$posts_row['post_id']}' />
<input type='submit' value='Like' /></p>
</form>
You are using your form action wrong.. if you are using get method than there is not need to use the form..
try this..
<a href='yourpage.php?like=<?php echo $post_id ?>'>Like</a>
your submit button name and like variable which you have used in action url are the same , and you used get method in method of form.So, you need to change the submit button name.
or
you can do it without using form only on button click try below code
<input type='button' name='like' value='Like' onclick="location.href='yourpage.php?like=<?php echo $post_id ?>'" />
Change your code to this
You can not write PHP variables using {}. You need to echo them out.
<form action='' method='get'>
<p align='left'><?php echo $posts_row['post_rating'] ?>
<input type='hidden' name='like' value='<?php echo $posts_row["post_id"] ?>' />
<input type='submit' value='Like' /></p>
</form>
Edit--
You were not returning the post id correctly, I made the changes, also there is no need to provide any action as it will be self only.

Passing hidden values, html

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'/>";
}

Categories