djcochran Posted April 11, 2009 Share Posted April 11, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/153655-grabbing-data-from-html-forms/ Share on other sites More sharing options...
.josh Posted April 11, 2009 Share Posted April 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153655-grabbing-data-from-html-forms/#findComment-807408 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.