I want to send POST variables from a form to be played around with in a PHP file and then have the PHP file redirect the user back to the original site the form was submitted. How could I reference the form's original URL from the PHP file so that I could use the same PHP file for multiple websites?
You would want to use something like
$_SERVER['HTTP_REFERER']
But this is controlled by the client side / browser.. So should not be trusted as such. A more preferred method for myself is to either include the url as a hidden input or have something like sessions store the last page.
<form method="POST" action="someurl.php">
<input type='hidden' name='sent_from' value='<?php echo $_SERVER['PHP_SELF']; ?>' />
<!-- Normal form data from this point -->
</form>
someurl.php
<?php
echo $_POST['sent_from']; // This will contain the page that posted to this file.
?>
Related
I am trying to pre-fill my form with data that is found in my .php file. Please, what am I doing wrong? The form field displays exactly what is found in between quotes.
<form id='registration' method="POST" action="m.php" >
Username:<br><input type="read-only" name="username" value="<?php echo htmlspecialchars($ss); ?>" /><br>
This is the .php file
<?php
session_start();
if($_SESSION["logged_in"] == true){
$ss=$_SESSION['username'];
}
else $ss="nu";
?>
The file containing the form html code should also be named and executed as php (and have the .php extension alongside the other one). Otherwise php doesn't get executed inside the html code and you end up with php code sent as-is to the browser.
The action attribute is used to inform the browser what page (or script) to call once the "submit" button is pressed, this is why you are not loading the username
You need to add that php script into the page the form is on as opposed to adding it to the forms processing functionality.
Here is a read on the action attribute
https://www.sitepoint.com/action-html-attribute/
let me know if I can be of further assistance but that change should get you up and running.
I have a page containing an iframe and a form targeting the same page (outside the iframe). The form's output seems not to be visible within the iframe. The workaround I use now is that I take the $_POST form output outside the iframe, parse it to variables (with some default values if the form hasn't been sent yet) and then use the variables in the iframe definition. Then in the iframe I parse those values as $_GET. The iframe definition looks like this:
echo '<iframe src="foo.php?a='.$a.'&b='.$b.'" >';
Is this the optimal solution, or is there anything more elegant available through php?
EDIT: I tried to set form target to the iframe and action to the corresponding page, but doesn't seem to work; I'll try to inspect it further. I'm new to JavaScript and AJAX and elsewhere in my website I use very little JavaScript and no AJAX. I will inspect AJAX-using solutions, but I would prefer staying with php.
Does this help? using php sessions. it can be done in JavaScript as well, but since you seem to be affectionate about php, here it is. Inside "index.php"
<?php
session_start();
$_SESSION["name"] = isset($_GET['myName'])?$_GET['myName']:"";
?>
<form name="MyForm" action="" method="GET">
<input type="text" name="myName"/>
<input type="submit" />
</form>
<iframe src="iframe.php" name="myIframe"></iframe>
then within "iframe.php"
<?php
session_start();
echo $_SESSION["name"];
?>
I have this page which contains a form. The values are POSTed over to the next page(check_values.php) which manipulates these values. Now I want to add a captcha. but it requires you to redirect the form to a verify.php which seems to clear out the posted variables when i redirect to check_values.php. I dont want to use session variables in this case. Are there any other method(s) to accomplish this.
You can use query strings too
In your verify.php page, add a query string to the location i.e.,
header('Location: check_values.php?captcha='.$postedValue.'');
Then in check_values.php, you can use $_GET to capture that value.
$value = $_GET['captcha'];
But make sure you sanitize all data coming from the query string
Yep, POST won't persist after a page request. You'll have to resend them somehow.
If you want to stick with POST, inside the validate page you could populate a form with the POSTed variables, and submit it using a Javascript. Something like
<form id='blah' method='post' action='check_values.php'>
<input type='hidden' value='<?php echo $_POST['var1']; ?>' />
<input type='hidden' value='<?php echo $_POST['var2']; ?>' />
</form>
<script type='text/javascript'>
document.getElementById("blah").submit();
</script>
Another option is stream_context_create, where you can send a redirect header with POST data. But this may only be useful for opening a stream, and the redirect could be difficult.
Of course, the easy way is just use a redirect header and send the data using GET, as #asprin explains.
The following works perfectly
http://www.jquery4u.com/forms/setup-user-friendly-captcha-jqueryphp/
My question is should I convert two html pages to php pages, so the called page can access its POSTed parameters, or is there a way for a called html (.html extension) page to access posted parameters?
I've been reading that because posted parameters are server-side there is no way for JavaScript to do this being client side. I've seen nothing about one html page accessing
parameters if that .html page was accessed via a POST.
Here is my calling form. The called form, needs access to TransDesc (below), which is a text field.
<script type="text/javascript" language="JavaScript1.2">
// Check where we came from so that we can go to the right spot when the
// form gets posted from outside of our HTML tree.
var PostURL = '/event.html';
</script>
Enter a Donation Amount
<script type="text/javascript" language="JavaScript1.2">
document.write(
'<form name="InvGenPayDonation"
action="'+PostURL+'"
onsubmit="return validateForm();"
method=POST>');
</script>
<p> $ <input type='text' name='Amount' value="0.00">
</p>
<p>In honor of <span style="color: #FF0000">
<input type='text' name='TransDesc' id='TransDesc' value="" >
</p>
<input type="submit" value="Next"> <br /><br />
A static HTML file cannot access variables that have been POST'ed to it. It can't even know they're there as they're sent to the server in the HTTP request, the server then deals with them and sends the HTML page in the HTTP response. They're 'consumed' before the page is even sent to the client.
You could use GET and access them via JavaScript, or configure Apache to server .html files as PHP files instead though.
In my opinion, php is the easiest way to go, and as far as languages go is pretty easy to learn and pretty intuitive.
You'll have to either convert them to PHP or use GET instead of POST, as GET parameters are accessible through window.location.href
Yes, I would recommend converting the pages to php. If you are set on using HTML files you will have to edit your htaccess file to run HTML pages as php.
You can always use ajax to retrieve and send post and get values.
You can retrieve it with js by creating a php file and access those with ajax from your html files.
I have built a site using php and want to try keep it one page.. The site displays pictures and so far i have it making links from folders in a folder each folder contains images so what i want is to make a post/get tag in the url and when the page loads it uses this to get the images from that folder.
So I want to use the generated links to post to the same page with a value via self_post is this possible and if so how?
my get section is
if(empty($_post['foldername']))
{
$directory = "portfolio/homepage/";
}
else if(isset($_post['foldername']))
{
$foldername = $_post['foldername'];
$directory = "portfolio/".$foldername."/";
}
and my link is like this
echo '<li><a id="" class="" href="'.$_SERVER["PHP_SELF"].'">'.$b.'<input type="hidden" name="foldername" value="'.$b.'" /></a></li>';
Thanks
What's wrong with GET?
Click me
The only way to make a POST request using a <a> tag would be to have it submit a form via javascript:
<form method="post" id="hidden_form" name="hidden_form" action="script.php">
<input type="hidden" name="foldername" value="<?php echo $b ?>" />
</form>
...
post me
You can also update the values of the hidden element(s) from javascript as well so when you click a particular link, it sets one of the values to something specific to the link.
The only way is doing it through JS. You can either send an AJAX request specifying POST, or you can create a hidden form and submit it. Here's an example
document.getElementById('my-link').onclick = function(){
// Code to submit the hidden form or to send an AJAX request specifying POST
return false; // to prevent the default behavior
}
I know of no way to do this with vanilla anchor tags. You could establish click event handlers in javascript to submit an XHR request. However, I have accomplished this in the past by using multiple form tags with a single submit entity (<input type='submit', <button type='submit', etc.).
Your forms would look like so:
<form action="{$_SERVER['PHP_SELF']}" method="post">
<input type="hidden" name="foldername" value="YOURVALUEHERE">
<input type="submit">
</form>
Like drew010 said if you absolutly need the POST method. Otherwise most single-page website uses things like index.php?a=1&b=2 where you can get "a" and "b" with $_GET["a"] ...