Jump to content

ERROR APPEAR BUT STILL INSERT DATA TO DATABASE !!


alvinchua

Recommended Posts

hi guy .. i am still new to php i have created 1 .htm form and a php form.. and this is the follow code ..

 

.htm form

 

<html>

 

<body>

 

<script language="JavaScript" src="ew.js"></script>

<script language="JavaScript">

function validate(form) {

var txt1 = form.firstname;

if (txt1.value=="") {

  alert ("Please fill in the box");

  firstname.focus();

  return false;

  }

  return true;

}

//-->

 

</script>

 

<form  onSubmit="return validate(this);" method="post" action ="new.php">

<br>

<br>

 

<table border="0" cellspacing="2" cellpadding="4" bgcolor="#CCCCCC">

<tr>

<td bgcolor="#666666"><span style="color: #FFFFFF;">firstname</span> </td>

<td bgcolor="#F5F5F5"><input type="text" name="firstname" size="30" maxlength="20"></span> </td>

</tr>

<tr>

<td bgcolor="#666666"><span style="color: #FFFFFF;">lastname</span> </td>

<td bgcolor="#F5F5F5"><input type="text" name="lastname" size="30" maxlength="20"></span> </td>

</tr>

<tr>

<td bgcolor="#666666"><span style="color: #FFFFFF;">age</span> </td>

<td bgcolor="#F5F5F5"><input type="text" name="age" size="30" maxlength="20"></span> </td>

 

</tr>

</table>

 

<p>

<input type="submit" name="Action" value="ADD" >

</p>

 

</form>

 

the following is a php form code ...

 

<html>

<body>

 

<?php

$con = mysql_connect("localhost","username","");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }mysql_select_db("login", $con);

  $sql="INSERT INTO person (firstname, lastname, age)VALUES('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

if (!mysql_query($sql,$con))

  {

  die('Error: ' . mysql_error());

  }

echo "1 record added";

mysql_close($con)

?>

</body>

 

 

</html>

 

the javascript will pop up a alertbox saying that firstname was not entered but... the null value still enter into the database.. can some1 help me in this... i need the error message(the pop up) appear and it stop proceeding to the new.php form...

 

thanks..

 

 

the PHP code is doing what it is asked to do and putting what it is told to do into the database, the html form is not stopping the posting of the data to the php page

 

what you can do is make it a prompt box

<script language="JavaScript">
function validate(form) {
var txt1 = form.firstname;
if (txt1.value=="") {
   var first = prompt ("Please enter your first name");
   form.firstname.value = first;
   }
//-->

</script>

hmm... is there anyway beside making a prompt box ?? is there a way to make the php and the htm together in a form ??

 

i just want to insert the value into the textbox and if got error .. it will not proceed to the add records into the database.. any1??

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.