Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP HTML Javascript

Status
Not open for further replies.

proggybilly

Programmer
Apr 30, 2008
110
US
I have a php page where I have an HTML form and when clicking submit, PHP runs through a process of gathering variables and putting information into an mysql database. I want an option that the user has to place a check on the form in order for it to submit. I have a javascript function to check this, however when I get the error that the box is not checked, I click ok and it submits the form anyway. I need a method to drop right back to the page if the javascript returns an error that the box has not been checked.
 
sounds like you've screwed your javascript.

the way to do this is to bind to the form.submit action. test the checked status of the checkbox. if the checkbox is not checked return false, otherwise do whatever you like.

it is the return false that stops the form from submitting.

remember also that you cannot call you submit control 'submit' as javascript gets confused. Give it a name of x_submit or something.
 
Actually it is called save. I will post a sample of code.

Code:
from the form:
    <dt>
     <label for="upload4">Upload file:</label>
    </dt>
    <dd>
      <input type="file" name="upload4" id="upload4" size="50" tabindex="12" >
    </dd>
    <dt>
      <input type="submit" name="submit" value="Save" onclick="ValidateForm(this.form)" tabindex="14"/>
    </dt>
    <dt>
     <label for="agree"><font color="red" size="2">we cannot be held liable for <br> misdrected messages. Please check 'I agree' to be able to send.</label>
    </dt>
    <dd>
      <input type="checkbox" name="agree" id="agree" tabindex="13" /> I agree</font>
    </dd>
  </dl>

Code:
from javascript to php
  <br><script LANGUAGE="JavaScript">
<!--

function ValidateForm(form){
   ErrorText= "";
   if ( form.agree.checked == false ) { alert ( "Please check I agree in order to send." ); return false; }
   if (ErrorText= "") { form.submit() }
}

 -->
</script>
<?php
if($_POST['submit'] == "Save")
{
 $from = $_POST['from'];
 $to = strtolower($_POST['to']);
 $days = $_POST['days'];
 $message = $_POST['message'];
 $notification = $_POST['notify'];
 
Well as he said, your JS needs to return a False value in the event the checkbox is not checked to stop the from from submitting.

So:

Code:
...
 <input type="submit" name="submit" value="Save" onclick="[red]return [/red]ValidateForm(this.form);" tabindex="14"/>
...

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Also Check for the checkbox in your serverside PHP
and remember to check/sanitize the recived data

Anything form the client can be spoofed & easily bypass any serverside security checks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top