Jump to content

Include variables on a form validation function.


jkkenzie

Recommended Posts

Hi!

What is the syntax of adding a variable as a checklist of available items based on a user input?

e.g a user types a project name on an input box (project1)

 

I want when javascript validate this field as below, to check from a list of variables gotten from the database using php. (in other words i would like to have a message that says "the Project name you entered".$row[''projectname"]. " is already available" but not using php. all i want to do is let php get all available project names and put them in variables which should be supplied to javascript to use them to validate my form) 

 

<script language="javascript" type="text/javascript">
	<!--
	function validate ( form ) {
    if(form.name.value == ""){
      alert('Please fill in the field');
      form.name.focus();
      return false;
    }
    return true;
  }
	//-->
	</script>

 

Thanks in advance...

 

Regards,

Jose

Hmm i assume you are familiar with php and know how to access and pulling all the data from MySQL.

 

<script language="javascript" type="text/javascript">
var all_my_project_name = [
// My php script are starting to append inside the javascript!!!
<?php
    // Connect to database and get the query into the $qry.
    for($i=0; $i<mysql_num_rows($qry); $i++){
        // Remember to put the quote.
        echo '"' . mysql_result("projectname", $i) . '"';
        if(($i+1) < mysql_num_rows($qry)){
            // The data are not finished yet! Need a comma.
            echo ",";
        }
    }
?>
];

// Alright, back to javascript!
function validate ( form ) {
    if(form.name.value == ""){
      alert('Please fill in the field');
      form.name.focus();
      return false;
    }
    // Add-in validation
    for(var i=0; i<all_my_project_name.length; i++){
        if(all_my_project_name[i] == form.name.value){
            alert("Duplicated project name!");
            form.name.focus();
            return false;
        }
    }
    return true;
  }
</script>

 

This code are written directly from the forum. I did't test out. You try run it and view the source. Make sure the all_my_project_name's variable are working correctly. Other than that, the validation function should't be any problem.

The error is in your code above on line with the following code:

    echo '"' . mysql_result("projectname", $i) . '"';   

 

The error is: mysql_result(): supplied argument is not a valid MySQL result resource in...line...

 

Ops, then i was my faults. The mysql_result should be like this:

 

    echo '"' . mysql_result($qry, $i, "projectname") . '"';   

 

Long time did't used this functions. hehe

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.