0

My problem is that when I enter data into my html form and click submit, it calls my php file but it doesn't send the parameters.

I have even tried using working code to test it, and even the working code is not passing through variables. I am unsure as to why this is doing this, bad php install? No idea.

Here it is, if you want to see if it works at least for you. But I am not getting anything passed into my variables on my php file. Thanks for the help.

    <html>
    <head>
    <title>Home</title>
    </head>
<body>

<form method="get" action="reg.php">
Firstname: <input type="text" name="firstname">
Lastname: <input type="text" name="lastname">
Age: <input type="text" name="age">
<input type="submit" value="Submit">
</form>

</body>
</html>

And here is the php file:

    <?php
echo $_SERVER['REQUEST_METHOD'];

if(isset($_GET['firstname'])){
    $firstname = $_GET['firstname'];
}
else{
    $firstname = 'null';
}
if(isset($_GET['lastname'])){
    $lastname = $_GET['lastname'];
}
else{
    $lastname = 'null';
}
if(isset($_GET['age'])){
    $age = $_GET['age'];
}
else{
    $age = 'null';
}

$con = mysqli_connect("127.0.0.1","root","", "my_db");


$sql="INSERT INTO persons (FirstName, LastName, Age)
VALUES
('$firstname','$lastname','$age')";
$result = mysqli_query($con, $sql);
if ($result)
  {
echo "1 record added";
  }
else{
    echo "Did not work";
}


error_reporting(E_ALL);
mysqli_close($con);
?> 

When I look at the error report, it says Undefined Index every time, for each piece of working code I tested. I tested 4 files of working code and neither worked but was proven they did. I am starting to think I have a bad php install or something deeper is the problem. Thanks again.

teepleb
  • 39
  • 1
  • 7
  • 2
    What is the output of `$_GET`? Do a `var_dump($_GET)`. – mcryan Feb 25 '13 at 13:10
  • 1
    You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** and should learn how to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from them. – Quentin Feb 25 '13 at 13:10
  • what line is it saying the undefined index is on? also, you could greatly reduce the number of lines of code by forward declaring your variables as null – bizzehdee Feb 25 '13 at 13:11
  • Where exactly are you getting an error? – Pekka Feb 25 '13 at 13:17
  • what output you get by the code: `echo $_SERVER['REQUEST_METHOD'];` – Code Lღver Feb 25 '13 at 13:17
  • It says I am undefined on each variable $_GET['firstname'], $_GET['lastname'], $GET['age']. – teepleb Feb 25 '13 at 13:18
  • Try, instead of `
    `, the following: `
    `. Don't know why but I had encountered that one with some older browsers... Also I'd prefer to post instead of get.
    – George Violaris Feb 25 '13 at 13:18
  • With the request method it shows me that I access it using GET. – teepleb Feb 25 '13 at 13:18
  • after submitting the form, what URL is become there? can you post that here. – Code Lღver Feb 25 '13 at 13:19
  • @GauravVashishtha This is the URL after submitting. file:///C:/wamp/www/reg.php?firstname=Blake&lastname=Teeple&age=20 – teepleb Feb 25 '13 at 13:24
  • Are you absolutely sure that `reg.php` is the right php file? What would be an output of `var_dump($_GET);`? – ozahorulia Feb 25 '13 at 13:25
  • Is this the URL? I really exclaimed for when you have the PHP server, its wamp, then why are you not using the http://localhost/reg.php like URL. Go and try the localhost to access the file. IF you will access like that then how the PHP code will execute properly. – Code Lღver Feb 25 '13 at 13:29
  • @GauravVashishtha I know the PHP code executes properly. I already tested it. My problem is that the variables/parameters are not getting passed from my html form to my php file to enter them into the database. – teepleb Feb 25 '13 at 13:32
  • @Hast I am absolutely sure it is the right file, it is the only one I currently have in my directory. I tried using the var_dump($_GET);, and it just left me with a blank white page. – teepleb Feb 25 '13 at 13:33
  • Whenever I have an issue like this, 9/10 it is a non obvious part of the script doing a header redirect. Resulting in the page loading twice, the second time with no GET vars, etc. I would try doing a print_r($_GET) followed by an exit at the top of the script (and also possibly put a couple of value clauses in the input tags, at least just to ensure a default value). – Kickstart Feb 25 '13 at 13:59
  • I figured it out, thank you all. It was a problem with file protocol. It was using the file protocol to implement the form, and if file protocol is used it means I have no server, etc. Thanks again! – teepleb Feb 25 '13 at 16:25

1 Answers1

-5

Three things:

  1. I would recommend you to use POST instead of GET.
  2. Give your input fields an id and it should work.
  3. You need to sanitize your data before writing them into the database.
Sven
  • 12,997
  • 27
  • 90
  • 148
  • 5
    1. Should be a comment. 2. `id` is only used client side, how would adding one help here? 3. Should be a comment. – Quentin Feb 25 '13 at 13:13
  • 3
    Last time I checked `id` is totally irrelevant to submitting a form. Only the `name` attribute is required for that to work. – Till Helge Feb 25 '13 at 13:13
  • @TillHelgeHelwig Really? Then I have given my inputs an unneeded ID all my life. Sorry! – Sven Feb 25 '13 at 13:15
  • @Sven: Well...the client and any JavaScript is happy if everything has a unique ID...it's just not a requirement for submitting a form to work. – Till Helge Feb 25 '13 at 13:17
  • I tried using POST and I would like to. But when I go to access the pages using the $_SERVER['REQUEST_METHOD'] function, it says I access them using GET. I was told IDs didn't really matter except for easier, readable code. And sanitizing? Not quite sure what you mean there. – teepleb Feb 25 '13 at 13:41