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()';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With