9 Replies - 982 Views - Last Post: 07 November 2011 - 02:45 PM Rate Topic: -----

#1 kushanku  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 09-October 10

Problem with $_GET and HTML form

Posted 06 November 2011 - 02:31 PM

Hi all. I am having an issue with the following code

<?php
$variable = $_GET["lastname"];
?>

<html>
 <body>
	<form>
		Last name: <input type="text" name="firstname" /><br />
		
		<input type="submit" name="Submit1" value="rewind">
		
	</form>
 <?php function($lastname)?>
 </body>
</html>



The function fails to work the first time I click submit. I am aware that the $_GET method passes variables using the URL. If I edit the url manually (eg 127.0.0.1/test.php?firstname=Dave&Submit) then the form works correctly first time.

How can I get the form to work correctly first time without manually editing the url?

Thanks in advance for you help.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with $_GET and HTML form

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10567
  • View blog
  • Posts: 39,117
  • Joined: 27-December 08

Re: Problem with $_GET and HTML form

Posted 06 November 2011 - 02:37 PM

Your function doesn't do anything. In fact, the syntax is illegal, as functions have curly braces. While anonymous functions are possible in PHP, I'd avoid them at this point. A better question is what are you trying to accomplish?
Was This Post Helpful? 2
  • +
  • -

#3 kushanku  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 09-October 10

Re: Problem with $_GET and HTML form

Posted 06 November 2011 - 03:12 PM

View Postmacosxnerd101, on 06 November 2011 - 02:37 PM, said:

Your function doesn't do anything. In fact, the syntax is illegal, as functions have curly braces. While anonymous functions are possible in PHP, I'd avoid them at this point. A better question is what are you trying to accomplish?

This isn't actually my code, Its just a stripped down version for demonstration purposes. Sorry for the confusion.

My question is really about how make the $_GET method retrieve a variable from a html form, first time, without manually changing the URL.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10567
  • View blog
  • Posts: 39,117
  • Joined: 27-December 08

Re: Problem with $_GET and HTML form

Posted 06 November 2011 - 03:18 PM

Let's start by separating your HTML and PHP so you have a better understanding of how things work.
<html>
   <head>
      <title>Form Demo</title>
   </head>

   <body>
       <form method="get" action="process.php">
           <input type="text" name="test" />
           <input type="submit" name="submit" value="submit" />
       </form>

    </body>
</html>



<?php
    echo "You entered " . $_GET['test'];
?>



So what will happen is that the user will enter something in the form, then hit the submit button. The browser will then send that to the server, which will in turn process the data. The server will then send some output back to the browser.
Was This Post Helpful? 2
  • +
  • -

#5 kushanku  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 09-October 10

Re: Problem with $_GET and HTML form

Posted 06 November 2011 - 03:40 PM

View Postmacosxnerd101, on 06 November 2011 - 03:18 PM, said:

Let's start by separating your HTML and PHP so you have a better understanding of how things work.
<html>
   <head>
      <title>Form Demo</title>
   </head>

   <body>
       <form method="get" action="process.php">
           <input type="text" name="test" />
           <input type="submit" name="submit" value="submit" />
       </form>

    </body>
</html>



<?php
    echo "You entered " . $_GET['test'];
?>



So what will happen is that the user will enter something in the form, then hit the submit button. The browser will then send that to the server, which will in turn process the data. The server will then send some output back to the browser.


Okay. To give you some more information. I have a function that makes an edit to a table in a MySQL database. When I hit submit without manually changing the url to be: 127.0.0.1/test.php?firstname=Dave&Submit the table shows the value to be NULL. When I hit submit after manually changing the url, the MySQL table shows the correct value.

Any idea on this?

Thanks again.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10567
  • View blog
  • Posts: 39,117
  • Joined: 27-December 08

Re: Problem with $_GET and HTML form

Posted 06 November 2011 - 03:43 PM

When dealing with databases, avoid using get, as the user can modify the URL. Use post instead. Post your relevant code as well, so we can get a better idea of what's wrong.
Was This Post Helpful? 0
  • +
  • -

#7 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Problem with $_GET and HTML form

Posted 06 November 2011 - 04:07 PM

Edit: Didn't look close enough at your code, what I posted was incorrect, my mistake.

This post has been edited by Jstall: 06 November 2011 - 04:09 PM

Was This Post Helpful? 1
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10567
  • View blog
  • Posts: 39,117
  • Joined: 27-December 08

Re: Problem with $_GET and HTML form

Posted 06 November 2011 - 05:55 PM

You were right. The <input> field is named "firstname", but $_GET['lastname'] is being accessed.
Was This Post Helpful? 1
  • +
  • -

#9 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Problem with $_GET and HTML form

Posted 07 November 2011 - 05:11 AM

Oh, so I was. But not for the reason I thought :P I was actually posting because I was looking at the form you posted with the input named "test",then after the post I realized that it was only in your code and not the OP's.

Regardless, nice catch, thanks!
Was This Post Helpful? 0
  • +
  • -

#10 kushanku  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 09-October 10

Re: Problem with $_GET and HTML form

Posted 07 November 2011 - 02:45 PM

Thanks for all your help guys. Next time I will provide the actual code used; my example code (which I should have checked more thoroughly) only added to the confusion.

I think that using $_POST instead of $_GET will solve my problem. I'll post the solution if it works.

Thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1