Jump to content

Validating a PHP form using Javascript...?


MarkGL

Recommended Posts

Not sure if this is the right place to post this but I have created a web page, a small database and used PHP to display the records in the database on the web page together with a small form that will add records. I now need to validate the form using Javascript but to be honest I am clueless as to even where to start. Here is my code so far...

 

<html>
<head>
<title>Dynamic Website</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body background="newbackground.jpg">

<img src="header.jpg">

<?php 
$db_server = mysql_connect("localhost", "root");

if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());

mysql_select_db("books")
or die("Unable to select database: " . mysql_error());

$query = "SELECT * FROM selection";
$result = mysql_query($query);

if (!$result) die ("Database access failed: ". mysql_error());
?> 

<?php // website.php
$db_server = mysql_connect("localhost", "root");

if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());

mysql_select_db("books")
  or die("Unable to select database: " . mysql_error());

if (isset($_POST['title']) &&
    isset($_POST['author']) &&
    isset($_POST['isbn']) &&
    isset($_POST['year']))

{
    $title = get_post('title');
$author = get_post('author');
$isbn = get_post('isbn');
$year = get_post('year');

if (isset($_POST['delete']) && $isbn != "")
{
    $query = "DELETE FROM classics WHERE isbn='$isbn'";

	if (!mysql_query($query, $db_server))
	    echo "DELETE failed: $query<br />" .
		mysql_error() . "<br /><br />";
}
else
{
    $query = "INSERT INTO selection VALUES" .
	"('$title', '$author', '$isbn', '$year')";

	if (!mysql_query($query, $db_server))
	    echo "INSERT failed: $query<br />" .
		mysql_error() . "<br /><br />";
}
}

echo <<<_END
<form action="website.php" method="post"><pre>
  Title <input type="text" name="title" />
Author <input type="text" name="author" />
   ISBN <input type="text" name="isbn" />
   Year <input type="text" name="year" />
        <input type="submit" value="ADD RECORD" />
</pre></form>
_END;

$query = "SELECT * FROM selection";	
$result = mysql_query($query);

if (!$result) die ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);

for ($j = 0 ; $j < $rows ; ++$j)
{ 
    $row = mysql_fetch_row($result);
    echo <<<_END
<pre>
  Title $row[0]
Author $row[1]
   ISBN $row[2]
   Year $row[3]
</pre>
<form action="website.php" method="post">
<input type="hidden" name="delete" value="yes" />
<input type="hidden" name="isbn" value="$row[2]" />
<input type="submit" value="DELETE RECORD" /></form>
_END;
}

mysql_close($db_server);

function get_post($var)
{
    return mysql_real_escape_string($_POST[$var]);
}
?>

 

I guess my question is how do I go about validating the form?

 

Thanks for your time...

Link to comment
https://forums.phpfreaks.com/topic/224834-validating-a-php-form-using-javascript/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.