Jump to content

html, javascript, php


christo

Recommended Posts

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.
[code]
<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>[/code]

What do you think ?
is the js well placed and why doen't it do anything ?
thanks
Link to comment
https://forums.phpfreaks.com/topic/15706-html-javascript-php/
Share on other sites

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. 
Link to comment
https://forums.phpfreaks.com/topic/15706-html-javascript-php/#findComment-64744
Share on other sites

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. 
Link to comment
https://forums.phpfreaks.com/topic/15706-html-javascript-php/#findComment-65108
Share on other sites

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

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

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;
Link to comment
https://forums.phpfreaks.com/topic/15706-html-javascript-php/#findComment-65130
Share on other sites

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  ???
Link to comment
https://forums.phpfreaks.com/topic/15706-html-javascript-php/#findComment-65137
Share on other sites

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.