can anyone tell me why this does not work:

<div>
    <table>
    <FORM name="search_form"  method='POST' onSubmit="return validate_form();" ACTION='jobsearch.php'>
    <tr><td>keyword:</td><td >
        <INPUT TYPE='TEXT' NAME='keyword'></td>
        <td>Industry:</td><td >
        <select name="cat_id">
        <option value="">---select one---</option>
        <option value="1">Accounting</option>
        <option value="2">Administration</option>
        <option value="3">Automation</option>
        <option value="4">Banking</option>
        <option value="5">Biotech</option>
        <option value="6">Bussiness</option>
        <option value="7">Construction</option>
        <option value="8">Construction</option>
        <option value="9">Consulting</option>
        </select></td></tr>
        <tr><td>city and state or zipcode: </td><td >
        <INPUT TYPE='TEXT' NAME='place'></td></tr>
        
        <tr ><td colspan="2"><input type="submit" value="Search"></td></tr>
    </form>
    </table>
    </div>

the javascript containing the function validate_form was linked in the header section:

<HEAD>
<link href='style_sbdjobs.css' type='text/css' rel='stylesheet' />
 
<script src="reg_form.js"></script>
 
 
 
 
</HEAD>

the problem is when the submit button is clicked, the javascript function is not called instead the php function is called directly but i need the form to be validated first before it is sent to the server. here is what the javascript reg_form.js looks like:

function validate_form( )
{
    valid = true;
 
        if ( document.search_form.keyword.value == "" )
        {
                alert ( "Please type in a keyword." );
                valid = false;
                break;
        }
 
        
 
        if ( document.search_form.cat_id.selectedIndex == 0 )
        {
                alert ( "Please select an industry." );
                valid = false;
                break;
        }
        
        if ( document.search_form.place.value == "" )
        {
                alert ( "Please type in a zipcode or city and state." );
                valid = false;
                break;
        }
        
 
 
        return valid;
}

hello i have changed your code,its works fine now..

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script language="javascript">
 
function validate_form( )
{
    var d=document.search_form;
        if (d.keyword.value=="")
        {
                alert ( "Please type in a keyword." );
                return false;
                
        }
        if (d.cat_id.selectedIndex==0)
        {
                alert ( "Please select an industry." );
                        return false;
        }
        if (d.place.value=="")
        {
                alert ( "Please type in a zipcode or city and state." );
                        return false;
        }
        return valid;
}
</script>
</head>
<body>
    <div>
    <table>
    <FORM name="search_form" ACTION='jobsearch.php' method='POST' onSubmit="return validate_form();" >
    <tr><td>keyword:</td><td >
        <INPUT TYPE='TEXT' NAME='keyword'></td>
        <td>Industry:</td><td >
        <select name="cat_id">
        <option value="">---select one---</option>
        <option value="1">Accounting</option>
        <option value="2">Administration</option>
        <option value="3">Automation</option>
        <option value="4">Banking</option>
        <option value="5">Biotech</option>
        <option value="6">Bussiness</option>
        <option value="7">Construction</option>
        <option value="8">Construction</option>
        <option value="9">Consulting</option>
        </select></td></tr>
        <tr><td>city and state or zipcode: </td><td >
        <INPUT TYPE='TEXT' NAME='place'></td></tr>
        
        <tr ><td colspan="2"><input type="submit" value="Search"></td></tr>
    </form>
    </table>
    </div>
 </body>
</html>

thanks man, that was great.

Can I just ask? Do you have PHP validation is place as well in case the user disables javascript?

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.