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
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. 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.