I have got an issue to ask related with GET/POST.
I am trying to make a simple blog with posts and comments of them.
From each post I have got on main page, I would like to add a comment form in a new page that enables to save the post´s index to have a control of the commentaries.
I get this index value through GET in the new page but when I submit the form via POST I lose the reference to the index.
I read that is not possible to use both methods at the same time and I would like to know how can I keep a parameter from the main page and store it with the rest of the values in the new form.
Thanks a lot,
BR
http://localhost/simple_blog_new_comment.php?postIndex=xx
<form action='simple_blog_new_comment.php' method='POST'>
Commentary:<br>
<textarea onfocus='clearContent(this)' cols='30' rows='5' name="txt_comment">Enter the text here...</textarea><br>
Author: <input type='text' name='txt_comment_author'><br>
<input type='submit' name='btn_comment_submit'><br><br>
</form>
I found a solution for this problem I would like to share in case someone have the same trouble.
Finally I get working my "Posts" and "Comments" databases fixing the variable reference problem using $_SESSION superglobal variable.
It works like this:
session_start(); // This allows the use of $_SESSION superglobal var
$_SESSION['index'] = $_GET['postIndex']; // Save the variable into $_SESSION
With this superglobal variable you can keep the index variable as a cookie for using it as long as you keep the session opened.
More related info here: http://php.net/manual/es/reserved.variables.session.php
Thanks again! :D
I am not sure if I understood you question. I suppose you want to get the parameter by URL and send it through a form. I think you should do the next.
<?php
$index=$_REQUEST["Index"];
?>
<form action='simple_blog_new_comment.php' method='POST'>
Commentary:<br>
<textarea onfocus='clearContent(this)' cols='30' rows='5' name="txt_comment">Enter the text here...</textarea><br>
Author: <input type='text' name='txt_comment_author'><br>
<?php echo "<input type=hidden name=num_index value=" . $index . ">"; ?>
<input type='submit' name='btn_comment_submit'><br><br>
</form>
In the simple_blog_new_comment.php you will need this if you want to get the value of num_index.
<?php
$kk=$_REQUEST["num_index"];
echo $kk;
?>
I think you are looking for something similar. I hope it would be useful.
Related
I have a really long form, which is basically a sign up for a college. And it's been working fine, although for some reason now I am not able to receive the POST data for the form anymore. I seem to receive all the variables, expect for the 'SUBMIT' button, which is what I look for to find out that it's posted.
It's a sliding form, meaning that it's about 450px high and it is split up into different 'pages', and at the bottom of each page there is previous, save for later, and next buttons. The previous and next activate jQuery functionality which slides the form left or right. The save for later button is a submit button which puts any received data into the database for retrieval later. The HTML for these buttons is:
<input type='button' class='previous' value='Previous'>
<input type='submit' class='saveforlater' name='saveforlater' value='Save For Later'>
<input type='button' class='next' value='Next'>
All three of these buttons work fine, if I test the receiving of saveforlater in PHP like so:
if(isset($_POST['saveforlater'])) {
// Do Stuff
}
It works every time.
However, right at the end of the form I have this:
<input type='button' class='previous' value='Previous'>
<input type='submit' class='next send' name='sendapplication' value='Send Application'>
And It used to work, but I cannot figure out what I've changed in order to stop it working. If I var_dump($_POST), I get all the other variables, except for this button.
So if I try to:
if(isset($_POST['sendapplication'])) {
I get nothing. NOTHING! I don't know why :(. It's such a simple thing and it's frustrating me.
If you click this button, you will see with var_dump or debug() returns this variable.
For submit buttons inside <form>, only buttons clicked will be setted as $_POST variable with its value.
Your input should have the attribute name. Your html should look like
<input type='button' class='previous' name='previous' value='Previous'>
<input type='submit' class='saveforlater' name='saveforlater' value='Save For Later'>
<input name='saveforlater' type='button' class='next' value='Next'>
Your php should look like in /enrol
<?php
var_dump($_POST);
Hope this helps you
How many fields are there in your form??
I think it is the problem due to size of limited post request. It is the problem of server configuration.
If you want to control it refer following link
What is the size limit of a post request?
If you're using a HTML form to send the data and a localhost server, your attribute action needs to have the PHP file directory on the server.
If I have three PHP pages, lets call them one.php, two.php, and three.php, and I use something like this on the first page:
<form name = "one" action = "two.php" onsubmit="return validateForm();" method ="post">
I understand that I can now use the variables I passed alone from one.php in two.php...
My question is, how would I use the variables from one.php in three.php?
I keep getting "undefined index" errors every time I try to call them as I would in two.php.
Thank you very much for your help!
Edit: I should have mentioned that I am using Sessions as well.
On the second page, I do something like this:
$_SESSION['name'] = $_POST['name'];
and it lets me pass on the name gathered from that first page to the second one, but when I attempt to do this on the third page, it just says "undefined index".
Instead of using post, you would need to use $_SESSION
so for example:
one.php
session_start();
$_SESSION["mypostvalue"] = $_POST["mypostvalue"];
$_SESSION["mypostvalue1"] = $_POST["mypostvalue1"];
three.php
session_start();
echo $_SESSION["mypostvalue"];
echo "<br />";
echo $_SESSION["mypostvalue1"];
Then once you are done with the session data, you need to get rid of it like this:
session_start();
$_SESSION = null;
session_destory();
Note Only use session_start() once per page, and before any data is sent to the browser such as html or white space.
I understand that I can now use the variables I passed alone from one.php in two.php...
Not as such.
Successful form controls in the form will submit their values to two.php. You can then access them via $_POST['name_of_control'].
If you generate form controls (possibly of type hidden) with values created from PHP variables (making sure to escape them to defend against XSS attacks) then they will appear in the form and be submitted.
My question is, how would I use the variables from one.php in three.php?
That depends on how the browser gets to three.php. You could generate another form and put the values in hidden inputs again.
$_POST is per request You should pass them to the third file using $_SESSION or $_GET.
Just make sure your input elements are named correctly. If you want to use $_POST["myvar"] in two.php, then the name attribute in one.php needs to be "myvar".
<form action="two.php" method="post">
<input type='text' value='test myvar' name='myvar' />
<input type='submit' value='go to 2' />
</form>
To use the variables from one.php in three.php, just pass them through as hidden inputs. That is, echo them back to the form in two.php:
<form action="three.php" method="post">
<input type='text' value='test var 2' name='myvar2' />
<?php echo "<input type='hidden' name='myvar' value='" . $_POST["myvar"] . "' />"; ?>
<input type='submit' value='go to 3' />
</form>
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/
Is it possible in php to include a forms value into the action redirection?
For example:
<form method='POST' name='Select' action='customer.php?CID=xxxxx'>
<input type=text width='5' name='searchVal' />
where xxxxx is the value entered into the form.
I've tried a number of different ways and I'm just not figuring it out! (Still sort of new to php) Any help would be appreciated.
It was looking like I would have to use $_POST and $_GET. A little more information might be in order... customer.php displays a list of customers in order by ID, name, etc. The user currently clicks on the customer ID that they want to display the details for. I'm trying to add a box where they can just enter the customer number to get to the details quickly, but I still want to have the listing displayed. From what it is sounding like, I will have to do this as two separate programs...is that true?
How about this:
<form method='POST' name='Select' action='customer.php'>
<input type='hidden' value='xxxxx' name='CID' />
<input type=text width='5' name='searchVal' />
...
</form>
You are free to add as much hidden values as needed.
Note, that you can even use PHP-like array notation_
<input type='hidden' value='xxxxx' name='CID[1]' />
<input type='hidden' value='yyyyy' name='CID[2]' />
At the PHP-side, access those values using this syntax:
$_POST[ 'CID' ][ 1 ]
$_POST[ 'CID' ][ 2 ]
UPDATE-1
Ah, you want to use a user-entered values to the Action URL just before the form gets submitted?
In this case you need to use JavaScript. Access the DOM to change the Action URL.
But let me ask, why you need to post a form value additionally as a parameter of the Action URL?
UPDATE-2
You wrote: 'From what it is sounding like, I will have to do this as two separate programs...is that true?'
No, actually not. You can still use one customer.php which checks at its beginning, if it was called using a linked customer in the table element or a searched customer in the search field.
In other words: You don't need to prepare two scripts, but two forms for two purposes which call the same script customer.php.
You can include the required value in a hidden field in your form:
<input type="hidden" name="CID" value="xxxxx" />
The reason this is required is that you are submitting the form to your server via POST, but appending parameters to the URL requires submission via the GET method.
Not without a post to the server. The value in the form is filled in client-side, so it has to return to the server before you can add it to the action. (at least, if you want to use php).
You can either
add it after posting (might not be usefull)
use javascript
just not use the GET CID, but get it out of the POST in your customer.php script.
I got it finally! It's actually very simple!
In the body of the code I put this:
<form action='#_SELF' method='GET' name='Projected'>
<input type=text size=5 name='CID' value='' title='Enter Customer number to display' />
<a href='#' onclick='document.Projected.submit();' title='Enter Customer number to display'>Go</a>
And at the top of the code I just do a:
if (!isset($_GET['CID'])) { ...
It works exactly the way I wanted it to!
Thanks everyone for the help! I appreciate it! (And I'm learning more and more about PHP everyday!)
Im pretty sure you cant do that unfortunately
I would like to hard-code the form below. In other words, I would like to post $submission and $fullurl to index.php as "tweet" without giving the user the option to edit them.
How can I do this?
EDIT: I want the user to still click a button that says "Tweet" to post the variables.
Thanks in advance,
John
<form method='post' action='index.php'>
<br />
<textarea name="tweet" cols="50" rows="5" id="tweet" ><?php echo $submission ?> <?php echo $fullurl ?></textarea>
<br />
<input type='submit' value='Tweet' name='submit' id='submit' />
</form>
<form method='post' action='index.php'>
<p><?php echo $submission ?> <?php echo $fullurl ?></p>
<input type="hidden" name="tweet" value="<?php echo $submission ?> <?php echo $fullurl ?>">
<input type='submit' value='Tweet' name='submit' id='submit' />
</form>
That still doesn't mean the user won't be able to doctor the value through, everything's editable client-side one way or another. Save the to-be-submitted tweet server-side in a session if you need absolutely immutable values.
I'm not entirely sure what you are asking, but it sounds like you just want to have some variables kept serverside without a user being able to edit them, between page loads.
In that case you may wish to learn about Sessions and session variables. These allow you to store stuff in between page loads without a user being able to edit them (but you can still read from them so you can display your variables on the page!)
If you could perhaps rephrase your question some if this isn't the answer you are looking for, we would be better able to assist you.
You could set the disabled
attribute of the <textarea>. But then
it would not be submitted with the
form. So you'd have to make a hidden
input with the value or use sessions to persist the data if it could not be recreated therwise.
You could also use JavaScript to blur the text area on focus. But there are ways around this, i.e. disable JavaScript.
In the end, I think you should reconsider the user interface and user experience. A textarea that isn't editable, probably should be a form element. Just display the data you plan to tweet and allow them to Appove it.
You could just make it completely invisible (style="visibility:hidden"), or disable it (`disabled="true"').
You can also just use document.getElementById('formID').submit(); to submit the form automatically.
Better yet, just make them all hidden inputs, and submit on page load with the above code.