Question

My code is super easy, but yet it's not working. It used to work, but then I changed something up and now I'm back to basic again. Can you guys see why it doesn't work?

Client code

<h1>Logg inn</h1>
<form method="get" action="herpaderp.php">
    <input type="text" class="form-control" id="title" value="Testtittel"> 
    <input type="text" class="form-control" id="desc" value="Testbeskrivelse">
    <input type="text" class="form-control" id="src" value="lollolol.jpg">
    <button type="submit" class="btn btn-primary">Logg inn</button>
</form>

Server code:

<?php
if(isset($_GET["title"]))
    echo $_GET["title"];
if(isset($_GET["desc"]))
    echo $_GET["desc"];
if(isset($_GET["src"]))
    echo $_GET["src"];
?>
Was it helpful?

Solution

Simple, your form elements need to be named and not as an ID.

You have id="title" yet it should be name="title" (added to what you presently have)

You can use id="title", but just make sure you also include name="title"

  • Do the same for the others

(A lookahead) => I also noticed you are using the word desc be careful if you're using that in conjunction with a DB, as desc is an MySQL reserved word, just saying.

An example:

The following will throw an error:

"INSERT INTO yourTable (desc) VALUES ('value')"

Correct method: (using the tick ` character around the column name)

"INSERT INTO yourTable (`desc`) VALUES ('value')"

OTHER TIPS

It seems you are changed in the html form or you haven't use name attributes

<h1>Logg inn</h1>
<form method="get" action="herpaderp.php">
    <input type="text" class="form-control" id="title" name='title' value="Testtittel"> 
    <input type="text" class="form-control" id="desc" name='desc' value="Testbeskrivelse">
    <input type="text" class="form-control" id="src" name='src' value="lollolol.jpg">
    <button type="submit" class="btn btn-primary">Logg inn</button>
</form>

You can use php code as bellow (But it is same as yours)

<?php

if(isset($_GET["title"])) echo $_GET["title"];

if(isset($_GET["desc"]))  echo $_GET["desc"];

if(isset($_GET["src"]))  echo $_GET["src"];

?>

$_GET[] takes name=""; property not id="".

So your code will become:

<h1>Logg inn</h1>
<form method="get" action="herpaderp.php">
    <input type="text" class="form-control" id="title" name="title" value="Testtittel"> 
    <input type="text" class="form-control" id="desc" name="desc" value="Testbeskrivelse">
    <input type="text" class="form-control" id="src" name="src" value="lollolol.jpg">
    <button type="submit" class="btn btn-primary">Logg inn</button>
</form>

As already suggested replace the id= of the fields with the name=

feature of PHP's processing of POST and GET variables is that it automatically decodes indexed form variable names.

<?php
//display the POST or GET parameter
var_dump($_POST);    
?>

Output :

//will get you something like:
 array('title'=>'john','description'=>'smith')

But to make this $_POST work You Must write in the .HTML file in the form ( action="http://example.php")

To display the submitted data you could simply echo $_POST; in The "example.php"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top