Jump to content

Grabbing Data from HTML Forms


djcochran

Recommended Posts

I'm having trouble figuring out how to get the data I want to submit to my mysql table out of the html form. I'm trying to get the stuff people type in the textboxes/drop-down list and put it into it's corresponding table.

 

<?php
/**** Dealing with the database ****/
// connect to db
$conn = mysql_connect('localhost','student5','up8833') or trigger_error("SQL", E_USER_ERROR); 
$db = mysql_select_db('student5',$conn) or trigger_error("SQL", E_USER_ERROR); 

// INSERT: if we have a name to add...
if($_POST['name']) {
   // little bit of cleaning...
   $name = mysql_real_escape_string($_POST['name']);
   // insert new name into table
   $sql = "INSERT INTO info (name) VALUES ('','$name')";
   $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
} // end if
?>

 

That's the PHP code. Now...say I want 'name' to be the info from document.form1.lastName.value? How do I get that into there? Can I just put it where 'name' is in the if($_POST[]) thing or what?

Link to comment
https://forums.phpfreaks.com/topic/153655-grabbing-data-from-html-forms/
Share on other sites

document.form1.lastName.value specifies a form field with an id of 'lastName'.  Also, document.form1.lastName.value is javascript, not php, so php will not recognize/parse that. 

 

To pass a form value to the server (and to php) You need to use the name attribute in your field. So for example:

 

<form action = 'somepage.php' method='post'>

  <input type='text' name='lastName' />

</form>

 

the value would be received at $_POST['lastName']

 

You can use an javascript to grab an element's value by id, but the field has to have the name attribute for it to be sent to the server and recognized. 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.