**test.html**
<!DOCTYPE html>
<html>
<head>
<title>Database test</title>
<link rel="stylesheet" type="text/css" href="test.css">
<script>
function validateForm(n,mes)
{
valid=true;
var x=n.value;
var errn="error_"+n.id;
var email = document.getElementById('EMAIL').value;
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
//alert(errn);
if (x==null || x=="" || x.trim()=="")
{
document.getElementById(errn).innerHTML=mes;
valid=false;
}
else
{
document.getElementById(errn).innerHTML="";
if(n.id == 'EMAIL')
{
if (!filter.test(email)) {
document.getElementById(errn).innerHTML="Please Provide a valid email address";
email.focus();
valid=false;
} else {
document.getElementById(errn).innerHTML="";
valid=true;
}
}
}
return valid;
}
</script>
</head>
<body>
<h1>Just a Database test</h1>
<form name="SignUp" action="http://127.0.0.1/cgi-bin/connectivity.cgi" onsubmit="return validateForm();" method="post">
Name :<input id="NAME" type="text" name="name" onblur="validateForm(this,'Name must be filled out');"> <p id="error_NAME"></p>
Email :<input id="EMAIL" type="text" name="email" onblur="validateForm(this,'Email must be filled out');"> <p id="error_EMAIL"></p>
<input type="submit" value="Send">
</form>
</body>
</html>
**connectivity.cgi**
#!C:/usr/bin/perl -w
use CGI;
use strict;
use DBI();
use CGI::Carp qw(fatalsToBrowser);
print "content-type: text/html; charset=iso-8859-1\n\n";
my $q=new CGI;
my $name=$q->param('name');
my $email=$q->param('email');
print $q->header;
#connect to database.
my $dbh = DBI->connect("DBI:mysql:database=test;host=localhost","root","mukesh",
{'RaiseError' => 1});
eval {$dbh->do("CREATE TABLE IF NOT EXISTS emp (name VARCHAR(20), email VARCHAR(50) UNIQUE NOT NULL)")};
print "creating table emp failed: $@" if $@;
print "<br>";
my $sth = $dbh->prepare("SELECT COUNT(*) FROM emp WHERE email = ?");
$sth->execute($email);
my $rows = $sth->fetchrow_arrayref();
$sth->finish();
if (!$rows) {
$sth=$dbh->prepare_cached(<<SQL);
INSERT INTO emp(name,email) values(?,?)
SQL
$sth->execute($name,$email);
$sth->finish();
} else {
print "<h4>$email already exists, please enter a new email address<br></h4>";
exit 1;
}
my $sql = qq/select * from emp order by name/;
$sth = $dbh->prepare($sql) or die "Can't prepare $sql:$dbh->errstrn";
#pass sql query to database handle
my $rv = $sth->execute() or die "can't execute the query: $sth->errstrn";
while (my @row = $sth->fetchrow_array) {
print join(", ",@row),"<br>";
}
$sth->finish();
$dbh->disconnect();
print "<br>Total no of records : $rv";
if ($rv>=1){
print "<br>Record has been successfully updated !!!<br>";
}else{
print "<br>Error!!while inserting record<br>";
exit;
}
my problem is that after quring the mysql db i want the output to be displayed on the same page as the form is
but when i hit the send button it takes me to a new page and the output/error is displayed like this.
Content-Type: text/html; charset=ISO-8859-1
mukesh kumar singh, munish259272@gmail.com
rajesh kumar, rajesh@gmail.com
Total no of records : 2
Record has been successfully updated !!!
or
Content-Type: text/html; charset=ISO-8859-1
munish259272@gmail.com already exists, please enter a new email address
i want the output or error to be come on the same page below the form(text boxes and buttons).
do i need ajax or php for that i not sure how to do it
munitjsr2 -1 Light Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.