Jump to content

Archived

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

xfezz

help with simple form validation

Recommended Posts

I need help with very simple form validation. (php newbie here) actually I have a few questions.

I have a simple form that passes and inserts only one field into my database. I would like to check to see if this field is null before it is sent to the database. If it is null, I would like to have some kind of alert message box to let the user know that it cannot be blank. I would like to know the best way to go about this. Is there some kind of message or alert box in php like javascript has?

Second, I would like to disable the submit button after it is clicked so no multiple entries can be submitted on accident. I currently have this working with javascript, but I am wondering if there is a better way to disable the button incase the clients computer doesnt have javascript for some odd reason.

Lets start with how to disable the submit button after its clicked. Here is the code that I have that disables the button after it is clicked.
[code]< SCRIPT LANGUAGE="JavaScript">
function DisabSub(btn)
{
btn.disabled = true;
}
< /SCRIPT>[/code]

And from there it is called from the onclick event of the submit button. Yes it works, but I am wondering if theres another way (maybe in php?) that will disable the button.

[code] 
<div id="content">
  <fieldset>
    <legend>Article Upload</legend>
      <form name="form" id="form" method="post" action="/port/testport.php">
      <textarea name="txtArticle" cols="48" rows="10" id="txtArticle"></textarea>
      <br>
      <input type="submit" name="Submit" value="Upload Article"onClick="DisabSub(this)" />
    </form>
    </fieldset>
  </div>
[/code]


As for the form validation I am probably making this more complicated than what it needs to be. You can see im passing the data to a php file testport.php. It is here that the connection to the database is made and data is inserted into the database then redirects back to the article page(where the form is).

Here is the code for testport.php

[code]
<?
//set server and database parameters
    $user_name = "root";
    $password = "some password";
    $database = "article_db";
    $server = "localhost";
   
//make connection to database
    $db_handle = mysql_connect($server, $user_name, $password);
    $db_found = mysql_select_db($database, $db_handle);

//declare passed parameters from input page
    $art=$_POST['txtArticle'];

//if the database is found without error, insert data from input page into database
if ($db_found) {
    $query = "INSERT INTO articles (message) VALUES ('$art')";{
    mysql_query($query);
}

//close database connection
    mysql_close($db_handle);
}

//if database isnt found let user know
else {
    echo "Database NOT Found ";

//close database connection
    mysql_close($db_handle);
}

//redirect back to input page
    header("Location: http://192.168.0.111/port/articleinput.php");
?>
[/code]

My question is, where should I have the code that vaildates the form? On the page with the form input? On another php page? Maybe something like validate_form.php and have it as an include? If so how would I go about calling it so it executes before the data is sent to the database? Or do I have the form validation on the testport.php? If so how would I go about alerting the user that the field is null?  ???

Share this post


Link to post
Share on other sites
cos php is a server-side language, you won't be able to execute any php from the browser without querying the server first (unlike javascript).

you can put your validate data code on the testport.php after the db connects. something like this for example:

if ($email == '')
  echo 'you have forgotten to type an email address on your form';

you can also store it on the same script if you wanted and make the script self-execute after the user clicks submit. this is a better option as the script will contain the form again, if the user forgets to fill something out.

Share this post


Link to post
Share on other sites
First of all,like garry27 said, since php is executed solely on the server (not on the computer viewing the page), you cannot have it disable or enable buttons without having the server execute a script, and then get back to the veiwing computer.

In other words, the script will only be executed between pages, not while a user is viewing the page.

The best thing to do for the first part would be to have javascript test if the form is filled, then have testport.php verify it, and return the user to the page if the form was null.

The best thing to do for the second part is probably to leave it how you have it. If you want you can add a script that only accepts 1 entry from any given IP in a given amount of time, so that people would not press it twice in a row.

Here are some suggested modifications:

articleinput.php: (note, I spelled the open script tag wrong so that my post doesn't trip the forum's "Intrusion Prevention System")
[code]
<html>
<head>
<SCRPT LANGUAGE="JavaScript">
<!--
function DisabSub(btn)
{
btn.disabled = true;
}
function Enable()
{
if(document.form.txtArticle.value=="")
{
document.form.Submit.disabled = true;
} else {
document.form.Submit.disabled = false;
}
}
-->
</SCRIPT>
</head>
<body>
<div id="content">
<fieldset>
    <legend>Article Upload</legend>
      <form name="form" id="form" method="post" action="/port/testport.php">
<?php if($_GET[error]==1) echo "You must enter something"; ?>
      <textarea onKeyDown="Enable();" onKeyUp="Enable();" name="txtArticle" cols="48" rows="10" id="txtArticle"></textarea>
      <br>
      <input disabled="disabled" type="submit" name="Submit" value="Upload Article"onClick="DisabSub(this)" />
    </form>
    </fieldset>
  </div>
</body>
</html>
[/code]

testport.php
[code]
<?
if($_POST(txtArticle)=="")  //If the form was empy, send the page back with an error.
{
header("Location: http://192.168.0.111/port/articleinput.php?error=1");
}

//continue code...
[/code]

Is that what you were looking for?

Share this post


Link to post
Share on other sites

×

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.