Jump to content

[SOLVED] form validation in php / javascript question


Recommended Posts

I have a form on my site that needs validating:

 

<form id="details" onsubmit="return validateFormOnSubmit(this)" name="updates" method="post" action="details_upd.php">

 

At first I was using the javascript function validateFormOnSubmit() to do the validation, but I wanted to have a backup plan in case the user didn't have javascript enabled, so I added some form validation to the details_upd.php file.

 

I thought that the javascript would run first and then the php, but it seems to be ignoring the validateFormOnSubmit() now that I added the php validation.

Is there a way I can get it to run the function, and only run the php validation if javascript is disabled?

 

TIA

Ed

 

Yes, but which runs first?

I want

 

1. First javascript validation:

function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateEmpty(theForm.from);
  reason += validateEmail(theForm.email);
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

 

 

 

2. Then php validation in

 

if(((!isset($_POST["name"])) || ($_POST["name"] == "")) || ((!isset($_POST["email"])) || ($_POST["email"] == "")))
{
header("Location: http://www.goldassets.co.uk/market-updates-problem.php");
exit();
}
if(!eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$', $_POST["email"]))
{
header("Location: http://www.goldassets.co.uk/market-updates-problem.php");
exit();
}

 

Javascript will always run first because it runs on the client machine where as php runs on the server.

 

I don't think so.

If edzillions page runs the page as details....php and on that page he outputs the form and checks the values.

Where is your validateEmpty() function ?

In the same javascript file:

<script src="js/validation.js"  type="text/javascript" language="JavaScript" ></script>

The function itself:

function validateEmpty(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a name.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

That is what i am suggesting

 

you must be having some problem in validateFormOnSubmit() function thats why it is not working first

 

where as it should work first

 

just try to put only alert message in this function it will work !!

 

Cheers !

I don't think so.

 

Sorry, but you are mistaken on that. Javascript will always run first.

 

As to the original question - you have a mistake somewhere in your javascript. If your javascript fails, it will default to the php (which is one reason you should never rely only on javascript validation, which you seem to have realized).

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.