use GET and POST in the same form [duplicate] - php

This question already has answers here:
Post and get at the same time in php
(6 answers)
Closed 9 years ago.
Regards.
I want to send some of my values in the form by GET method and some by POST method. How can i do it ?
Here is an example. I wan to make this kind of page, but i want a single submit button for both the forms. Is it possible to use GET method on some feilds and POST method on some ?
<html>
<form name="f2" action="a.php" method="POST">
Name: <input type="text" name ="uname"/><br>
Password: <input type="password" name ="pass"/><br>
<input type="submit" >
</form>
<br><br>
<form name="f1" action="a.php" method="GET">
Fav Movie: <input type="text" name="favmovie"><br>
Movie rating:<input type="text" name="rating"><br>
<input type="submit" value="My FAv movie">
</form>
</html>

HttpRequests do not allow for multiple request types, however you could do
<form method="post" action="answer.php?foo=bar">

You cannot send the values of some input fields via GET and others via POST with PHP and HTML.
If you add JavaScript, you could add the values of some input fields to the query string of the action-attribute of the form. But this seems to be too much of a hassle.

For same submit button -
It will be only possible if you start your form tag with conditional Programming.
i.e. after rendering from web server it will show only one form tag for a specific submit button.
Hope this helps you.

The first question is, why would you want to do that?
I think the simple answer is no, a form can only be submit via one method, by get or post

Related

echo $_POST['code']; error in google chrome [duplicate]

This question already has answers here:
Chrome: ERR_BLOCKED_BY_XSS_AUDITOR details
(8 answers)
Closed 4 years ago.
I have one page where I submit hidden form where I store a large html code that is made in javascript and I want to send that code via form to another php file. It should work but my browser blocks it(google chrome):
<form action="napravi.php" method="post">
<input type="text" name="code" value=""/>
<input type="submit" id="forma_napravi"/>
</form>
and in javascript:
document.getElementById("forma_napravi").click();
and php page where I send the form data:
if(isset($_POST['code'])){
echo $_POST['code'];
}
and this is error that browser shows to me:
It will show error if you send html or javascript code this way because it is google chrome's in-built design.
I tried your code it was working. But when I put some code in place of blank, e.g. value="<script>alert('123');</script>", I got the same error
You may use htmlentities() to parse the html to string. Then the code you send will have no effect at all.
give your form an id and check if it is been submitted. like this-
In your html file
<form action="napravi.php" method="post" id="form">
<input type="text" name="code" value=""/>
<input type="submit" id="forma_napravi"/>
</form>
In your javascript
document.getElementById("form").on('submit', function(){
// your code
});
Hope this will help

How to post HTML FORM Data to a http link given to you by a client using php

Hi guys im quite newbie on coding but i want to ask How to post HTML FORM Data to a http link given to you by a client thanks.
Try creating a .html file as below:
<html>
<form method="post" action="urclientlink">
<input type="text" name="firstname"/>
<input type="text" name="lastname"/>
<input type="text" name="submitButton" value="Submit Form"/>
</form>
</html>
Please elaborate on your question for a better answer. Thanks!
This is no different from submitting form data to a page on your own site. You simply amend the form's action attribute:
<form action="http://another-server.com/another-page">
You also need to be sure you get the right METHOD: e.g. GET or POST:
<form action="http://another-server.com/another-page" method="GET">
You will need to check with the client to see which page and method they need.

POST Route (via form) Vs. GET Route (post via URL Params) [duplicate]

This question already has answers here:
When should I use GET or POST method? What's the difference between them?
(15 answers)
Closed 5 years ago.
I never understand these 2 completely, can someone please put an end to this ?
I have 2 routes :
1- Route::get('/admin/dashboard', 'DashboardController#dashboard');
2- Route::post('/admin/dashboard', 'DashboardController#dashboard_post');
If I do this
{hostname}/admin/dashboard?test=123
My first route should trigger.
If I create a form, with 1 input and submit the form to /admin/dashboard
My second route should trigger.
What is different between these 2 POST ?
Are they behaving the same thing ?
How would one know to use one over the other ?
You have to specify in your form whether it's submitting a GET or a POST request.
<form action="/action_page.php" method="get"> will send a GET request and trigger the first route.
<form action="/action_page.php" method="post"> will send a POST request and trigger the second one.
More about the difference between get and post.
Its basically a matter of what they are used for. If you want to e.g. create something new or upload a file you should use a POST request. If you want to get Information from the Server, which is already there (e.g. data from a database) you should use GET.
To sum it up in short: Use POST for sending data and GET to receive data from the server.
For your form: you have to specify which request method should be used:
<html>
// Use GET
<form action="form.php" method="GET">
<input type="text" name="text">
<button type="submit">Submit</button>
</form>
// Use POST
<form action="form.php" method="POST">
<input type="text" name="text">
<button type="submit">Submit</button>
</form>
</html>

Using HTML textfield to pass a GET parameter to PHP file

I want to pass a GET parameter to a PHP file , by data typed in a text field. I want something like this :
<form id="Form1" action="action.php?nameExample=textFieldData" method="get">
the textfield is out of this form that's why I need to affect the value of the textfield into get PHP parameters so I can send it with another data in the same msg
the textfield code :
Username: <input type="text" name="nameExample"><br>
anyway to do it ?
You can't use $_GET parameters in an HTML form action attribute if the <form method='get'. Use <input type='hidden' name='nameExample' value='whatever' />, or better yet, use AJAX without a form.
This should do what you want to. The JS code copies the value of an external input field to a hidden input field.
HTML:
<form action="action.php" method="get" id="Form1">
<input type="text" name="otherData" value="xxx">
<input type="hidden" name="nameExample" value="" id="nameExampleCopy">
<input type="submit">
</form>
Username: <input type="text" name="nameExample" id="nameExampleOriginal">
JS:
document.getElementById('Form1').addEventListener('submit', function () {
var originalElement = document.getElementById('nameExampleOriginal');
var hiddenElement = document.getElementById('nameExampleCopy');
hiddenElement.value = originalElement.value;
});
But you should think about some points:
Is get the right method here? It sounds like post could be better.
Is it semantically a good idea to pass a value from outside the form inside it? We don't know your application, but generally it is a good idea to have all submitted data inside the form. As HTTP pointed out in his comment, sessions could also help with that.

Retrieve Values with Post method from Separate Form Fields in PHP

This might be a basic question of HTML but I'd like to know if it is possible in PHP. Usually when building a form submission page, the form tag is set per one form group. So the submit button is assigned per one form tag. The elements of form fields have to be close as enclosed in the form tag. If I want more complicated layouts, then is it possible to separate form tags to divide form fields?
For example,
<?php
print_r($_POST);
?>
<form name="test" action="" method="post">
First name: <input type="text" name="firstname" value="" /><br />
<input type="submit" value="submit" />
</form>
<p>some other contents</p>
<form name="test" action="" method="post">
Last name: <input type="text" name="lastname" value="" />
</form>
In this case, even if the second field is filled, the value won't be sent. If possible, I'd like to have just one submit button and control multiple forms.
Wrap your entire page into a single form and use some kind of deliminator for your elements (for instance "form2_field1"). I have never found having multiple forms useful (even if there are hide/unhide values) and my guess is that the submit button will probably only submit the form it is wrapped in. As one of the comments has mentioned, use JQuery if you want more complex forms. However, my recommendation is just to send over the entire form, whether it is complex or not and process it according to what you want.

Categories