Note that there are some explanatory texts on larger screens.

plurals
  1. POjavascript should return false, form still validates and php inserts data in table
    primarykey
    data
    text
    <p>Hello and many thanks in advance to everybody that will read and answer this post. First of all I am awfully sorry if this has already been answered but I am looking for an answer to this for like 12h on the whole web and nothing that i found worked out. So my issue is as follows: i have a form, a script and a php part. as the webpage will have many form pages i tried to write the script so that i gets the data it needs to validate directly from the fields. therefore the function has an argument. now, the submit button is hidden and i am using enter after the final field to automatically submit the form. even if the javascript works (it creates a red border around the fields that are empty), when i press enter the form still submits through php. in my noobiness i think that the problem is that either the script doesnt return true or false or that the onSubmit has an error therefore it doesnt get passed true of false. i am posting the codes bellow. many thanks, once again!</p> <p>for the form part:</p> <pre><code> &lt;form action="addUser.php" method="post" name="login" onsubmit="return checkField()"&gt; &lt;table border="0" cellpadding="0" cellspacing="0" class="addUser"&gt; &lt;tr&gt; &lt;td&gt;&lt;p class="capRegister"&gt;Date de utilizator&lt;/p&gt;&lt;/td&gt; &lt;td&gt;&lt;p class="capRegister"&gt;Date de identificare&lt;/p&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input class="input1" name="user" type="text" placeholder="alege-ti un nume de utilizator" tabindex="1" onblur="checkField(name)" value="&lt;?php echo $user; ?&gt;"/&gt;&lt;/td&gt; &lt;td&gt;&lt;input class="input1" name="nume" type="text" placeholder="numele tau" tabindex="4" onblur="checkField(name)" value="&lt;?php echo $nume ?&gt;" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input class="input1" name="password" type="password" placeholder="alege-ti o parola" tabindex="2" onblur="checkField(name)" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input class="input1" name="prenume" type="text" placeholder="prenumele tau" tabindex="5" onblur="checkField(name)" value="&lt;?php echo $prenume; ?&gt;"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input class="input1" name="confirma" type="password" placeholder="confirma parola" tabindex="3" onblur="checkField(name)" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input class="input1" name="interior" type="tel" placeholder="interior" tabindex="6" onblur="checkField(name)" value="&lt;?php echo $interior ?&gt;"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td&gt;&lt;input class="input1" name="mobil" type="tel" placeholder="mobil" tabindex="7" onblur="checkField(name)" value="&lt;?php echo $mobil; ?&gt;"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;p&gt;&lt;input class="submitButton" name="login2" type="submit" value="" /&gt;&lt;/p&gt; &lt;/form&gt; </code></pre> <p>now for the script part:</p> <pre><code>function checkField(x) { //initializeaza o variabila care pe baza argumentului functiei verifica campurile dupa nume (argumentul contine numele campului) var continutCamp=document.getElementsByName(x)[0].value; //variabila pentru footer var msgJos; //variabila pentru parola var tempParola=document.getElementsByName('password')[0].value; msgJos=""; document.getElementById('bottomBar').innerHTML=msgJos; document.getElementsByName(x)[0].style.borderStyle='none'; //verifica daca campul este gol if (continutCamp==null || continutCamp=="") { // inconjoara cu o margine rosie eroarea document.getElementsByName(x)[0].style.borderColor='#FF0000'; document.getElementsByName(x)[0].style.borderStyle='solid'; document.getElementsByName(x)[0].style.borderRadius='4px'; // scrie un mesaj in campul de jos msgJos='&lt;p id="footerInfo"&gt;nu ai completat campul '+ x +'&lt;/p&gt;'; document.getElementById('bottomBar').innerHTML=msgJos; //return false pentru a impiedica procesarea formularului return false; } //daca nu se potriveste parola cu confirmarea if((name=="confirma")&amp;&amp;(continutCamp!=tempParola)) { //coloreaza ambele campuri de parola si confirmare cu rosu document.getElementsByName('confirma')[0].style.borderColor='#FF0000'; document.getElementsByName('confirma')[0].style.borderStyle='solid'; document.getElementsByName('confirma')[0].style.borderRadius='4px'; document.getElementsByName('password')[0].style.borderColor='#FF0000'; document.getElementsByName('password')[0].style.borderStyle='solid'; document.getElementsByName('password')[0].style.borderRadius='4px'; //cod ca sa schimbe continutul footerului cu mesajul potrivit msgJos='&lt;p id="footerInfo"&gt;parolele nu se potrivesc&lt;/p&gt;'; document.getElementById('bottomBar').innerHTML=msgJos; //din nou return false ca sa nu se proceseze formularul daca parola nu e aceeasi return false; } } </code></pre> <p>and finally the php part:</p> <pre><code>&lt;?php require_once("localVars.php"); $footerError; if(isset($_POST['user'])) { // $dbc=mysqli_connect(USER,PASS,BAZA) or die ('&lt;p id="footerInfo"&gt; Eroare in timp ce ma conectam &lt;/p&gt;'); $user=$_POST['user']; $parola=$_POST['password']; $nume=$_POST['nume']; $prenume=$_POST['prenume']; $interior=$_POST['interior']; $mobil=$_POST['mobil']; $query="INSERT INTO users (user, parola, nume, prenume, interior, mobil) VALUES ('$user','$parola','$nume','$prenume','$interior','$mobil')"; // $result=mysqli_query($dbc, $query) or die ('&lt;p id="footerInfo"&gt; Eroare in timp ce bagam datele &lt;/p&gt;'); mysqli_close($dbc); } else{ $footerError='nu sunt date de introdus'; } ?&gt; </code></pre> <p>the php has comments on it in order not to send the billion tries in the database. the $_POST checks for user but it did check for submit or any other field name before. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload