Why I am getting GET vars in a POST form? - html

I have this code:
<?
$page = $_GET['page'];
$find = $_GET['find'];
?>
<form method="post" action="#">
<input type="text" name="whatever" value="1">
<button class="btn btn-default" type="submit">Post this</button>
</form>
My initial URL is:
htttp://www.someplace.com?page=1&find=lookfor
When sending the post form I am getting back "page" and "find" vars along the "whatever" input value. Why? Is this happening because my form action is "#"?
By the way, this is what I want, this saves me the work of posting hidden input values. But I want to be sure it is valid.

Using action="#", you will submit the form to the current URL. Your GET vars are part of this URL so that is why you're getting them again.
More infos on this question.

Related

How do I remove a parameter with blank response from URL generated by the <form> tag?

I am writing a search box in HTML that takes the user input and append it to the URL as a parameter. The code of the form looks like this.
<form name="form" action="" method="get">
<input type="text" name="id" id="idresponse">
<input type="submit" value="Submit">
</form>
This will bring the user from example.com/test.html to example.com/test.html?id=12345678 assuming they entered 12345678 at the text box.
However, if the user inputted nothing and clicked Submit, they will be brought to example.com/test.html?id=, which I don't want. How can I modify the code so that the form knows that a certain field is left blank and do not send the parameter with the URL? In this case, the desired URL would be example.com/test.html.
edit 20210405 2057 changed the id of the input from idresposne to idresponse to avoid confusion
The so-called URL parameters is the querystring of the URL.
The following code does not use jQuery, but achieves a similar effect. (written by RobG)
<form name="form" onsubmit="disableEmptyInputs(this)" action="" method="get">
<input type="text" name="id" id="idresponse">
<input type="submit" value="Submit">
</form>
<script>
function disableEmptyInputs(form) {
var controls = form.elements;
for (var i=0, iLen=controls.length; i<iLen; i++) {
if (controls[i].value == '') controls[i].disabled = true;
}
}
<script>
This will remove all the parameters but the ? will still trail the URL. i.e. the URL will be example.com/test.html? instead. However, this does not matter because they both point to the same address.
Refer to these links (kindly provided by Progman) for other ways of doing this, including using jQuery.
Delete empty values from form's params before submitting it
Delete empty values from form's params before submitting it
How can I remove empty fields from my form in the querystring?
Thanks.

What does <form action="?"> do when sending to self? [duplicate]

This question already has answers here:
What does an entry "action='?'" in html form mean?
(6 answers)
Closed 7 years ago.
I am following php mysql novice to ninja:
form template below
<form action="?" method="post">
<div>
<label for="joketext">Type your joke here:</label>
<textarea id="joketext" name="joketext" rows="3" cols="40"></textarea>
</div>
<div><input type="submit" value="Add"></div>
</form>
Part of the PHP controller:
if(isset($_POST['joketext'])) //insert block
{
try
{ //prepared starement
$sql = 'INSERT INTO joke SET
joketext = :joketext,
jokedate = CURDATE()';
What does the '?' do in the form action
? is used to separate the URL path from the query string. In this case, the query string is empty, so it's the same as if it had been action="".
However, there's a difference. If the original page was loaded using a URL that had a query string, action="" will submit the form with that same query string. Putting an explicit ? in the URL replaces the original query string with this empty one.
It uses the current URL with an empty query string as the action of the form. An empty query string which means no query string at all.
That way the form will post the data to a location "?", If your file contains the PHP code you won't need any action="?" You can remove it, the form will post to it self and replace isset($_POST["joketext"]) with isset($_POST["submit"]) to detect the submit button that have been clicked not the joketext exist
it will be like this
HTML:
<form method="post">
<div>
<label for="joketext">Type your joke here:</label>
<textarea id="joketext" name="joketext" rows="3" cols="40"></textarea>
</div>
<div><input type="submit" name="submit" value="Add"></div>
PHP:
if(isset($_POST['submit'])) //insert block
{
try
{ //prepared starement
$sql = 'INSERT INTO joke SET
joketext = :joketext,
jokedate = CURDATE()';

Get to Post function

I got an URL in a html tag, so in a GET format :
http://toto.fr/grc/start.swe?SWECmd=ExecuteLogin&SWEAC=SWECmd=InvokeMethod&SWEMethod=GotoView&SWEService=GRC+Debranchement+Generique&BusObject=Contact&BusComp=Contact&ViewName=GRC+Contact+Synthetic+View&SWEUserName=titi&SWEPassword=toto&ValeurChamp=35925436&Champ=Person UId
I want to call it in POST. Is there a way to do this easily ?
You need to create an HTML form with all the URL parameters as hidden variables.
e.g.
<form action="http://toto.fr/grc/start.swe" method="POST">
<input type="hidden" name="SWECmd" value="ExecuteLogin" />
<!--- repeat for all other parameters --->
</form>
Replace the <form method="GET"> to <form method="POST">
if this doesn't work, could you post your code?
I've just been thinking about this again and if you really do need to have a link submit a POST request then it may be possible by giving the form an id attribute, create your link with an id attribute and then you can add a click event handler which will submit the form in Javascript.
See the mock-up code below (syntax may not be perfect!)...
HTML.
<form id="loginForm" action="http://toto.fr/grc/start.swe" method="POST">
<input type="hidden" name="SWECmd" value="ExecuteLogin" />
<!--- repeat for all other parameters --->
</form>
<a id="LoginLink" href="#">Login</a>
Javascript.
<script type="text/javascript">
var loginForm = document.getElementById('loginForm');
var loginLink = document.getElementById('loginLink');
loginLink.addEventListener('click', login);
var login = function() {
loginForm.submit();
}
</script>

Where does the data go that a user enters in an HTML text field?

Im very confused and am not sure what to type into google to find out how to receive data that a user enters in an HTML text field. So that is why I am asking this question here. Any help would be really appreciated.
You can know which data has been entered with php, javascript, ajax,...
Your HTML text field:
<form action="process.php" name="myForm" id="idForm">
<input type="text" placeholder="Name" id="name"/>
<input type="submit" value="submit"/>
</form>
<!-- if you want to take it with javascript add that-->
<script type="text/javascript" src="test.js">
</script>
Get it with PHP:
process.php looks like that:
<?php
// Verify $_POST['name'] exist
if(isset($_POST['name']))
{
//verify $_POST['name'] is not empty
if(!empty($_POST['name']))
{
echo $_POST['name'];
}
}
?>
Get it with Javascript (This can able you not to refresh the page):
test.js:
var name = document.forms["myForm"]["name"].value;

php mail function (two buttons ?)

I have the following code in my form:
<form action="mail.php" method="POST">
Is there a way to add another submit button and let php know that if button1 is used it sends the data to mail.php while is button two is used it send the data to mail2.php ?
Thanks for the help
It would be correct to use the value of the radio-button, passing the parameter option, like:
<input type="radio" name="how" value="mail1" id="radio1"><label for="radio1">Name of parameter1</label>
<input type="radio" name="how" value="mail2" id="radio2"><label for="radio2">Name of parameter2</label>
and on the server:
$mail = $_POST['how']==mail1?'mail1':'mail2';
include($mail.'.php');