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

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()';

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.

Why I am getting GET vars in a POST form?

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.

HTML generate URL from Input

i am working on a search function, herefore i need the value of an input to generate the final url (which shows the results)
Let's say the user enters the content he is looking for here:
Name: <input type="text" id="myText">
now i need to generate a hyperlink from
http://constant/constant?query=NAME&someotherconstantthings
here, the NAME needs to be replaced from the content of the input
Try to use PHP, you could add a action to the Form Element to post the entered informations to the PHP file, then generate ur hyperlink.
<form action="phpfilename.php" method="post">
Name: <input type="text" id="myText" name="myText">
<input type="submit" value="Search">
</form>
<?php
$name = $_POST['myText'];
$hyperlink = 'http://constant/constant?query='.$name;
?>
You need something like this:
<form action="URL" method="get">
Enter your name here: <input type="text" name="query" value="">
<input type="submit" value="Search">
</form>
You need to replace the keyword URL with the path to the page which performs the search. You can remove the keyword URL if want to submit the form to the same page.

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;

How do I partially prefill a query box?

I have used this code to show a Google web search box
<form method="get" action="https://google.com/search">
<input type="text" name="q" size="31" value=" ">
</form>
I would like to have the box prefilled with the text
"What to do in" and let the user type the location ( example type only Sydney )
so the query will be:
What to do in Sydney"
Is it possible?
I'm just a beginner and do not know much about HTML.
Your help would be very appreciated.
Thank you
Fernando
use value = "What to do in" and this will work.
If you are in HTML only, then do like this:
<input type="text" name="q" size="31" value="What to do in ">
You should build it in a way it is clear to the user his search will be appened to this line:
What to do in:
something like
<span>What to do in</span><input type="text" id="search" name="q" size="31" value=" ">
and in js or anything else:
$.get('https://www.google.co.il/search?q=what to do in ' + $('#search').val(),function(data){
//Do stuff with seach results
})
You can then append the word to the query
The issue with the above answers is that the user can just delete the whole text and start again..
so if you really want to limit it to "what to do in ..." then you should only offer up the text input for the location.. similar to what raam86's answer was.
<form method="get" action="https://google.com/search">
<label for="whattodo">What to do in</label>
<input type="hidden" name="q" id="real_input" value="what to do in" />
<input type="text" id="whattodo" onkeyup="fillForm(this.value);" />
</form>
<script>
function fillForm( _val )
{
document.getElementById('real_input').value = 'what to do in ' + _val;
}
</script>
Use label where possible - this relates the text to the input area - which means, if a user clicks on the label they'll be focused into the input box ready to type..
You'll notice that there's a hidden input and also a text input
the hidden input gets filled with "what to do in " as well as the text from the text field (as the user types, a bit of javascript is triggered called fillForm() which appends the hidden field (the query field) with the user inputted text.. this ensures that it's always going to have "what to do in" prepended to the query.
You should place
function fillForm( _val )
{
document.getElementById('real_input').value = 'what to do in ' + _val;
}
between the <head> and </head> of your HTML document.