jkkenzie Posted May 7, 2008 Share Posted May 7, 2008 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 Link to comment https://forums.phpfreaks.com/topic/104569-include-variables-on-a-form-validation-function/ Share on other sites More sharing options...
xenophobia Posted May 8, 2008 Share Posted May 8, 2008 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. Link to comment https://forums.phpfreaks.com/topic/104569-include-variables-on-a-form-validation-function/#findComment-535763 Share on other sites More sharing options...
jkkenzie Posted May 8, 2008 Author Share Posted May 8, 2008 The php part appears disabled on php,html and javascript pages. Link to comment https://forums.phpfreaks.com/topic/104569-include-variables-on-a-form-validation-function/#findComment-535914 Share on other sites More sharing options...
xenophobia Posted May 9, 2008 Share Posted May 9, 2008 Meaning got error occurred. Did you include your database connection? I problem still consist, try open your php display_error settings to On and see what is the error. Link to comment https://forums.phpfreaks.com/topic/104569-include-variables-on-a-form-validation-function/#findComment-536492 Share on other sites More sharing options...
jkkenzie Posted May 9, 2008 Author Share Posted May 9, 2008 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... Link to comment https://forums.phpfreaks.com/topic/104569-include-variables-on-a-form-validation-function/#findComment-536527 Share on other sites More sharing options...
xenophobia Posted May 9, 2008 Share Posted May 9, 2008 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 Link to comment https://forums.phpfreaks.com/topic/104569-include-variables-on-a-form-validation-function/#findComment-536534 Share on other sites More sharing options...
jkkenzie Posted May 9, 2008 Author Share Posted May 9, 2008 Its Ok.... Its a common problem...... Thanks, Regards, Joseph Link to comment https://forums.phpfreaks.com/topic/104569-include-variables-on-a-form-validation-function/#findComment-536582 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.