Submitting variables in POST - php

I have a form that sends stuff like names and email and a message and I get it with $_POST['etc']; - But, I also want to send action=someaction as a part of the url, but I don't want to have any hidden form fields.
Can this be done?
Thanks!

Yep, just add it to your url: <form action="url.php?action=someaction" method="post">. You can retrieve them in your php script using $_GET (in this case, $_GET['action']).

action=someaction cannot be read by $_POST['whatever'] because it's submitted in a GET-Request. You can access GET and POST variables by using $_REQUEST instead of $_GET and $_POST.
To form the request, follow the answer of Spiny Norman.

If I understand you correctly, you simply need to add ?action=someaction to the action attribute of your form tag. These would be fetched from PHP by using $_GET instead of $_POST.
Example:
<form action="http://example.com?action=someaction">...</form>
and to get the value:
$action = $_GET['action'];

Related

Form post action: not seeing query parameters in $_POST

I have a small PHP application (MVC) with a form. The action looks something like this:
<form action="<?php sprintf("/v/.../update?job=%s", $job->id);?>" method="post">
Here $job is a PHP object I pass the view from the controller. I hold onto the id field (an integer) so that I can update the row in the database corresponding to the object.
I'm not seeing this value in $_POST (it is in $GET, though) when I step into my update function for a post request. How should I retrieve this value? Is this expected?
Try to get by $_GET['job'] or remove 'job' from action url and send in hidden text field to get as $_POST['job'].
Also I think your action url is not creating. use echo
<form action="<?php echo sprintf("/v/.../update?job=%s", $job->id);?>" method="post">
you pass your job param as url string, and in this case it can be seen in $_GET or $_REQUEST arraies not $_POST whatever your form action get or post, because this param isn't form input.

Pass value to script without x=something [duplicate]

I am working on a project where i need to access clean URL's always i.e no parameters can be passed in the URL for getting values on another page.
Can anyone please suggest an alternative other than 'Form Submit' method.
Thanks In Advance.
Use $_SESSION for this purpose.
Using $_SESSION you can use variable in multiple pages wherever you want.
// assigning variable
$_SESSION['name']="variable";
//retrieving
echo $_SESSION['name'];
write session_start() at the top of page wherever you want $_SESSION variable
For Clean URL's i prefer you may use HTTP POST Method. Hope that helps.
<form name="frmApp" method="POST" action="/action.php">
</form>
Else, you can use AJAX with jQuery to submit the values to another page.
$.ajax({url:"action.php",success:function(result){
$("div").html(result);
}});
Check out w3schools to get started with AJAX : http://www.w3schools.com/jquery/jquery_ajax.asp
No support for SESSION since i don't like writing php code inside my web page.
Sessions can help, or ajax call.
for using clean url you can use post method in form
<form name='' method='POST' action=''>
You can try mapping resources to the url.
Suppose you want to get mailing address of a customer then you can write.
http://[baseurl]/[customerId]/mailingaddress.
Read more here

Pass values from one page to another without form and without passing in URL

I am working on a project where i need to access clean URL's always i.e no parameters can be passed in the URL for getting values on another page.
Can anyone please suggest an alternative other than 'Form Submit' method.
Thanks In Advance.
Use $_SESSION for this purpose.
Using $_SESSION you can use variable in multiple pages wherever you want.
// assigning variable
$_SESSION['name']="variable";
//retrieving
echo $_SESSION['name'];
write session_start() at the top of page wherever you want $_SESSION variable
For Clean URL's i prefer you may use HTTP POST Method. Hope that helps.
<form name="frmApp" method="POST" action="/action.php">
</form>
Else, you can use AJAX with jQuery to submit the values to another page.
$.ajax({url:"action.php",success:function(result){
$("div").html(result);
}});
Check out w3schools to get started with AJAX : http://www.w3schools.com/jquery/jquery_ajax.asp
No support for SESSION since i don't like writing php code inside my web page.
Sessions can help, or ajax call.
for using clean url you can use post method in form
<form name='' method='POST' action=''>
You can try mapping resources to the url.
Suppose you want to get mailing address of a customer then you can write.
http://[baseurl]/[customerId]/mailingaddress.
Read more here

why all variable names and values are not displayed When using method="post" in HTML forms

i m getting confuse taking to that question why all variable names and values not show in url when we use method "post" in HTML forms.
i hope my question is clear.
That is because POST requests include variables to message body and not URL. See this: http://www.cs.tut.fi/~jkorpela/forms/methods.html (Methods GET and POST in HTML forms - what's the difference? )
Only GET methods show the variable names and values in the URL, not POST methods
When you are using POST method than all the data like your variable name,variable value, cookie is send to server in Request body,
so you cant see that parameter while using POST.
GET and POST methods are two different way to exchange data between server and client.
GET - retrieve data from URL (Eg: http://domain.com/index.php?var1=val1&var2=val2)
echo $_GET['var1']; (will return `val1`) and so on
POST - collect values in a HTML form with method="post", data send with curl with the same method, and so on.
<input name="username" />
echo $_POST['username'];
Use this like example, the actual content is more complex.

GET and POST on the same page?

EDIT: Answer found! Thank you very much people, a lot of answers worked, I chose the hidden field answer as it was easiest :D
I am creating a commenting script and I came across a problem. I am having to use $_POST and $_GET on the same page, which I don't think makes sense.
I am very new to php and am training myself.
I have a page named viewVerses.php - this has a lists of verses. When someone follows the reply link,
echo '<br />Reply';
I'm passing the verseid (commenting on bible verses) into the reply.php, so that a query may be made with that verseid. (This is so that the user can still see the verse he/she is commenting on).
Now reply.php has the form in it for posting a reply. The form goes to postReply.php
This is in postReply.php
$title = $_POST['title'];
$body = $_POST['body'];
$verseid = $_GET[verseid];
Can I get the verseid from the url and the POST the values from the form in the same page?
If not, is there a way I can do this better? Remember, I am new at php and probably won't implement a solution that is super hard. I have to get it for my to put it in my site.
I hope this is clear
I would add a hidden input to the comment form:
<input type="hidden" name="verseid" value="
<?php echo $_GET['verseid']; ?>
" />
That way, in postReply.php, you can access it using $_POST['verseid'].
Yes you can. The method of a form (in a html page) can be POST and the action URL can contain "GET" arguments being something like process.php?vid=1001 so to say. So in process.php you'll have vid as $_GET and the rest of data from the form as $_POST.
Sure you can, just set the action of the form to postReply.php?verseid=id_of_the_verse this way when an user submits a reply, in the POST array will be the reply related data and in the GET the id of the verse.
Yes, it is possible to mix both GET and POST values with one request. The problem you have is probably that you pass the GET value to reply.php, which then passes POST values to postReply.php. So, unless you tell reply.php to send that GET value as well, it will get lost.
You can do this by either specifying the GET value in the action parameter of the form tag, or you could even switch to a POST value with that, by adding a <input type="hidden" name="verseid" value="<?php echo $verseid; ?>" /> to the form.
Although it may seem counter-intuitive an HTTP request can come in with both Form and QueryString data. Like robertbasic says you can access them both via there respective arrays.
Using a form with a hidden input (<input type="hidden" name="verseid" value="..." />) is probably the cleanest way of doing things.
PHP also defines the $_REQUEST global array in addition to $_GET and $_POST. In general you should use either $_GET or $_POST but in this case where verseid is being passed for both methods, it might be more convenient to use $_REQUEST['verseid']. This way you need not care about the HTTP method being used on your script.

Categories