0

Hi Forum,

I'm currently working on a project where I create a form in Javascript/HTMl(easy). By form, i mean an actual form where it shall contain combo boxes, drop-down lists, buttons etc. Something like a questionnaire. That's all good and simple enough. However, how can I use this questionnaire/form to talk to a database. It needs to store information to a database. Whatever the user inputs, it needs to save that data to a database.

Any help is greatly appreciated.

Usmaan

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by roswell1329
0

You would need something to handle the data from your form. Your form should have an "action" attribute specified which defines where to send the data. I don't think you can send it back to the JavaScript form, because JavaScript has to be executed by the browser.

Therefore, you'll need another tool to handle the data. PHP is one of the easiest ways to handle form data and connect to a database, but there are many other ways -- ASP, Perl, Python, Ruby, C/C++, etc.

Here would be a simple form-handling script using PHP that takes in the data from a form's input boxes named "f_name", "l_name", and "email_addr" and submits the values of those to a table named "users" in a database named "mydb" on the local server:

<?php
function db_connect()
{
  $result = @mysql_pconnect("localhost", "root", "somepass");
  if (!$result)
    return false;
  if (!@mysql_select_db("mydb"))
    return false;
  return $result;
}
$conn = db_connect();
$sql = "insert into users (fname, lname, email) values ('$f_name', '$l_name', '$email_addr');
$result = mysql_query ($sql, $conn);
header("Location: http://localhost/thankyou.php");
?>

Edited by roswell1329: added example

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.