From Textbox to PHP variable

//nmh's picture

He has: 17 posts

Joined: May 2005

Heya,

I am trying to figure out how to make a textbox send it's value (that has been entered by a user) to a PHP variable. Check the code below:

<? require_once('./global.php'); ?>
  <table>
   <tr>
    <td>
     <form method="POST" action="link.php">
      <input type="Text" id="$urlname" value=""/>
      <input type="Text" id="$url" value=""/>
      <input type="submit" value="Submit" />
     </form>
    </td>
   </tr>
  </table>
  <?
    $user=$bbuserinfo[userid];
    mysql_query("INSERT INTO journal_links (url,postuserid,urlname) VALUES ('$url','$user','$urlname')")
  ?>
'

How can I make it so "" sends it's entered value to $urlname, and that "" sends its value to $url.

Any help would be appreciated. Smiling

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Use the NAME tag which is required for this.

Then in PHP, you can access $_POST['urlname']
(older versions of PHP would let you just directly access $urlname but for security that efault configuration has been changed)

-Greg

CptAwesome's picture

He has: 370 posts

Joined: Dec 2004

Another note, just so you don't scratch your head any more, in this chunk:

<form method="POST" action="link.php">
      <input type="Text" id="$urlname" value=""/>
      <input type="Text" id="$url" value=""/>
      <input type="submit" value="Submit" />
     </form>
'
You have id="$url", and if you try to access this later in php (after it's name instead of id, because id doesn't work in inputs/form pieces), you'd try $_POST[$url] except that php would use the php variable of $url, and it would end up as $_POST[''] and you don't want that. If you did $_POST['$url'] that would work, since single quotes ignores variables, it's just really not something you want to get started as a habit.

//nmh's picture

He has: 17 posts

Joined: May 2005

Thank guys,

It worked great. Smiling

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.