Jump to content


Photo

html, javascript, php


  • Please log in to reply
7 replies to this topic

#1 christo

christo
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 26 July 2006 - 03:46 PM

Hi to everybody !

I'm writing an html page where the user fils one or more (if zero infos then pop up Alert) forms (javascript check) and then the page sends the info to a php scipt for treatment and then database check (mysql).

What i don't know is this : Since with the form action = script.php to send the infos then where do i place the javascript ?

Here's what i've donne but it doesn't work.
<html>
    <head>
        <title> * Annuaire * </title>
            <script type="text/javascript">
                function verifForm(formulaire){
                    if( formulaire.name.value == "")
                     alert('Le champ est vide !!!');
                         else
                             formulaire.submit();} </script>
    </head>
        <body>
            <h2 style = "text-align": center;></div>
            <h2 style = "text-align": center;> * Annuaire du xyz * </h2>
            
                <form action = search.php method = "POST">
                     <table border=\"0\">
                     
                    <tr><td>Nom :</td> <td><input type="text" name="name"></td></tr>
					<tr><td>Prenom :</td> <td><input type="text" name="surname"></td></tr>
				<tr><td>Tel:</td> <td><input type="text" name="tel" maxlength="10"></td></tr>
				<tr><td>Email:</td> <td><input type="text" name="email"></td></tr>
					<tr><td>Web :</td> <td><input type="text" name="web"></td></tr>
				<td>Departement:</td><td align="center"><select name="depart" >
				<option value="all" selected="selected">Tous les departements</option>
						<option >Info</option>
						<option >Micro</option>
						<option >Robo</option>
						<option >Admin</option>
                        </select>
					</td>
				</tr>
				<td>Statut:</td><td align="center"><select name="statut" >
				<option value="all" selected="selected">Tout statut</option>
						<option >Permanent</option>
						<option >Doc</option>
						<option >Post-Doc</option>
						<option >Invite</option>
						<option >Stagiaire</option>
                        </select>
					</td>
				</tr>
                 <td><br><input type="submit" value="Envoye" onsubmit="verifForm(this.form)"></input></td>
			    <td><br><input type="reset" value="Retablir"></input></td>
		      </form>
    	</table>
    <body>
<html>

What do you think ?
is the js well placed and why doen't it do anything ?
thanks

#2 bltesar

bltesar
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 27 July 2006 - 06:11 PM

there are a few problems with this code.

First, <form action = search.php method = "POST"> should read
        <form action = "search.php" method = "POST">

Second, I believe the onsubmit event handler should be in the <form> tag, not the <input> tag.
Third, I don't know if the onsubmit event hander can stop the submit from occuring.  I think, but I'm not sure, that the validation function should return true or false, and a false might prevent the submit from going through.  Your validation calls submit again, so it will keep calling itself ad infinitum.

I use a different approach (which is why I forget the onSubmit approach).  I do not use a submit button.  Instead, I use a regular button with an onClick event handler that calls the validation function.  If the data is valid, the validation function then submits the form. 

#3 christo

christo
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 28 July 2006 - 10:40 AM

the problem with the onClick is that if the user hits enter at the keyboard and not use the mouse then the js won't do anything...that why i'm not using it.
But i will do the changes that you talked about and hope for the best, thank eh !

#4 bltesar

bltesar
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 28 July 2006 - 12:06 PM

christo, I checked into this and discovered that you can use the onSubmit as I previously described. 

put the onSubmit="valid_func()" in the <form> tag.

have the function 'return true' for good data and 'return false' for bad data.  The 'return false' will cancel the submission. 

#5 christo

christo
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 28 July 2006 - 12:19 PM

but in the form i have already the action for the php script, where/ can i add this too ?

#6 bltesar

bltesar
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 28 July 2006 - 01:03 PM

your form tag should look like this:
<form action = "search.php" onsubmit="verifForm(this)" method = "POST">

and your veriForm function should look like this:
function verifForm(formulaire){
      if( formulaire.name.value == "")
      {
          alert('Le champ est vide !!!');
          return false;
       }
       else
       {
          return true;
        }
}

and your submit button should look like this:
<input type="submit" value="Envoye">


incidentally, you do not need to pass an argument to the veriForm function if the function will only operate on one form.  Instead of the formulaire argument, you can use document.forms[0].name.value;

#7 christo

christo
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 28 July 2006 - 01:28 PM

That's it you were right about putting it in the form !
i changed the script too and at least i have the alert now!

I still have some probs  like :

Warning: mysql_connect(): Can't connect to MySQL server on 'localhost' (10061) in c:\program files\easyphp1-8\www\search.php on line 57

Erreur de connection avec MySQL !
Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in c:\program files\easyphp1-8\www\search.php on line 58

Erreur connection avec la DB !
Warning: mysql_query(): Can't connect to MySQL server on 'localhost' (10061) in c:\program files\easyphp1-8\www\search.php on line 68

That might be that i have to configure easyphp  ???

#8 bltesar

bltesar
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 28 July 2006 - 02:18 PM

Fix the first error and the rest will go away.  What does your mysql_connect() statement look like?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users