Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

forms

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

like image 430
matt prichard Avatar asked May 26 '15 08:05

matt prichard


3 Answers

? 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.

like image 75
Barmar Avatar answered Oct 23 '22 11:10

Barmar


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.

like image 34
Uma Kanth Avatar answered Oct 23 '22 11:10

Uma Kanth


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()';
like image 1
Mohamed Belal Avatar answered Oct 23 '22 10:10

Mohamed Belal