Log in

View Full Version : Problems with sending data to a MySQL



samsath
10-30-2009, 01:46 AM
Hallo,
I am trying to make a survey page on my website using html, JavaScript and php then transferring the data to a MySQL, but i can't get the information from the page to the mysql if anyone could help me out in anyway i would be so grateful.

HTML and Javascript for the questions.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Internet Survey - Section 1</title>
<script type="text/javascript">

function ajaxFunction(){
var ajaxRequest;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){

ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}else{

ajaxRequest = new XMLHttpRequest();
}
return ajaxRequest;


var data = 'Name=' + escape(document.getElementById("Name").value) +',Location='+ escape(document.getElementById("Location").value) +',Age='+ escape(document.getElementById("Age").value)+',Job='+escape(document.getElementById("Job").value) +',Job1='+ escape(document.getElementById("Job1").value) +',CompOwn='+ escape(document.getElementById("CompOwn").value) +',InternetAc='+ escape(document.getElementById("InternetAc").value);

var string = data;
ajaxRequest.open("get", "php_s1_addrow.php" + string, true);
ajaxRequest.send(null);
}
</script>

</head>
<body>
<h1><strong>Section 1 - Personal Information</strong></h1>
<blockquote>
<p>Please fill out as much information in this section as you want.</p>
<form method="post" action="php_s1_addrow.php">
<p>
<label for="Name">Name</label>
<input name="Name" type="text" id="Name" size="50" />
(This can be any name you want, eg username)</p>
<p>
<label for="Location">Location</label>
<input name="Location" type="text" id="Location" size="47" />
city, country
</p>
<p>
<label for="Age">Age</label>
<input name="Age" type="text" id="Age" size="10" />
Age</p>
<p>Sex
<label>
<input type="radio" name="Sex" value="male" id="Sex" />
Male</label>
<label>
<input type="radio" name="Sex" value="female" id="Sex" />
Female</label>
<br />
<br />
</p>
<p>Are you employed?</p>
<table width="200">
<tr>
<td align="center" valign="middle"><label>
<input type="radio" name="Job" value="yes" id="Job" />
Yes
</label></td>
</tr>
<tr>
<td align="center" valign="middle"><blockquote>
<p>
<input type="radio" name="Job" value="no" id="Job" />
No</p>
</blockquote></td>
</tr>
</table>
<p>
<label for="Job1">If 'Yes' please say what type of job you do.</label>
<input name="Job1" type="text" id="Job1" size="35" />
</p>
<p>Do you own a computer?</p>
<table width="200">
<tr>
<td align="center"><label>
<input type="radio" name="CompOwn" value="yes" id="CompOwn" />
Yes
</label></td>
</tr>
<tr>
<td align="center"><label>
<input type="radio" name="CompOwn" value="no" id="CompOwn" />
No
</label></td>
</tr>
</table>
<p>Do you own any devise other than a computer that can access the internet?</p>
<table width="200">
<tr>
<td align="center" valign="middle"><blockquote>
<p>
<input type="radio" name="InternetAc" value="yes" id="InternetAc" />
Yes</p>
</blockquote></td>
</tr>
<tr>
<td align="center" valign="middle"><blockquote>
<p>
<input type="radio" name="InternetAc" value="no" id="InternetAc" />
No</p>
</blockquote></td>
</tr>
</table>
<p>&nbsp;</p>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<p>
<a href="s2_computer.html"> <input type="submit" name="Next_1" id="Next_1" value="Next" onclick='ajaxFunction()'/> </a>
</p>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
<p>&nbsp;</p>
</form>
<p>&nbsp;</p>
</blockquote>
</body>
</html>

The PHP code

<?
//initilize PHP

$dbhost = "localhost";
$username = "215756";
$password = "lcar1990";

if($_POST['ajaxFunction()']) //If submit is hit
{
//then connect as user
//change user and password to your mySQL name and password
mysql_connect("localhost","$username","$password");

//select which database you want to edit
mysql_select_db("$username");

//find out how many rows
$query="SELECT * FROM S1";
$result=mysql_query($query);
$num=mysql_numrows($result);
$id = $num++ ;

//convert all the posts to variables:
$Name = $_REQUEST['Name'];
$Location = $_REQUEST['Location'];
$Age = $_REQUEST['Age'];
$Employed = $_REQUEST['Job'];
$Job = $_REQUEST['Job1'];
$Computer = $_REQUEST['CompOwn'];
$Dives = $_REQUEST['InternetAc'];

//Insert the values into the correct database with the right fields
//mysql table = markers
//table columns = id, name, address, lat, long, type
//post variables = $title, $address, '$lat, $long, $type
$result=MYSQL_QUERY("INSERT INTO S1(ID,Name,Location,Age,Employed,Job,Computer,Dives)".
"VALUES ('NULL', '$Name', '$Location', '$Age, '$Employed', '$Job', '$Computer', '$Dives')");

//confirm
echo "Query Finished";
}
?>


Thank You