File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes recod is not inserting in mysql databse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "recod is not inserting in mysql databse" Watch "recod is not inserting in mysql databse" New topic
Author

recod is not inserting in mysql databse

vibhas bhingarde
Greenhorn

Joined: Feb 22, 2011
Posts: 11
insert.html file

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="generator" content="CoffeeCup HTML Editor (www.coffeecup.com)">
<meta name="created" content="Sat, 24 Sep 2011 15:42:13 GMT">
<meta name="description" content="">
<meta name="keywords" content="">
<title></title>

<!--[if IE]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<div id="myDiv"><h2>CREATE ACCOUNT</h2></div>
<form name="myform" >
First name :<input type="text" name="fname" id="fname"><br /><br />
Last name :<input type="text" name="lname" id="lname"><br /><br />
age :<input type="text" name="age" id="age" /><br /><br />
Hometown :<input type="text" name="hometown" id="hometown" /><br /><br />
Job :<input type="text" name="job" id="job" /><br /><br />
<input type="button" value="Insert" onClick='ajaxfunction()' />

</form>
</body>
</html>


<script type="text/javascript" language="javascript">

var err=false;


var fname=document.getElementById('fname').value;
var age=document.getElementById('age').value;
var hometown=document.getElementById('hometown').value;
var lname=document.getElementById('lname').value;
var job=document.getElementById('job').value;
function ajaxfunction()
{

try
{
var ajaxreq=new XMLHttpRequest();
}
catch(e)
{
alert("your browser broked");

}

ajaxreq.onreadystatechange=function()
{
if(ajaxreq.readystate==4)
{
document.getElementById('myDiv').innerHTML=ajaxreq.responseText;
}
}


var query1="?fname=" + fname + "&lname=" + lname + "&age=" + age + "&hometown=" + hometown + "&job=" + job;
alert(query1);
ajaxreq.open("POST","insert.php" + query1,true);
ajaxreq.send(null);




}




</script>




insert.php


<?php
$dbhost = "localhost";
$dbuser = "scott";
$dbpass = "tiger";
$dbname = "test";

$link=mysql_connect($dbhost, $dbuser, $dbpass);
if(!$link)
{
die("could not connect to mysql');
}
mysql_select_db($dbname,$link) or die("could not open" .mysql_error());


$query="INSERT INTO employees VALUES('$_POST[fname]','$_POST[lname]','$_POST[age]','$_POST[hometown]','$_POST[job]')";

mysql_query($query) or die (mysql_error() );



mysql_close($link);

echo "record inserted";
?>



my recorded is not being inserted ,sometimes it insert a blank & sometimes it doesnot inserts only.
also echo "record inserted" doesnot gets printed even if it insert blank

help me
thanks
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You are sending the data viw get, not post. You are appending the values to the querystring.

If you want post it has to be placed in send().



Read more about the basics of JavaScript since any tutorial would have pointed this out.

Eric
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: recod is not inserting in mysql databse
 
Similar Threads
How to remove Textbox Element using DOM in HTML
Ajax code getting null value
how to keep register page data if register fails
Need Help With Dynamic text box in JSP
How to get back in old window from a new window