Problem: onClick Function

This is a discussion on "Problem: onClick Function" within the Web Page Design section. This forum, and the thread "Problem: onClick Function are both part of the Design Your Website category.



Go Back   Webforumz.com > Main Forums > Design Your Website > Web Page Design

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jul 26th, 2007, 09:06
New Member
Join Date: Jul 2007
Location: Pune, Maharashtra, India
Age: 20
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to mihirc Send a message via Skype™ to mihirc
Question Problem: onClick Function

Hello All,
My first post in this forum.

I have made a general contact us module based in html/and table for Joomla. I have added verification and CAPTCHA. but the problem i am facing is that, for the submit button i need to give a 3 commands to make them work properly, i.e. unless the person has entered the cumpolsary fields he wont be able to send the mail. But the problem is the onclick command is parsing only the first of the attributes.

Here is the php code.

Code: Select all
input[type="text"] {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
background-image: url('images/input_bg.png');
background-repeat: no-repeat;
border: 1px solid #999999;
margin: 5px 0 0 0;
padding: 3px 3px 3px 3px;
display: inline;
}
textarea {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
background-image: url('images/input_bg.png');
background-repeat: no-repeat;
border: 1px solid #999999;
margin: 5px 0 0 0;
padding: 3px 3px 3px 3px;
display: inline;
}

* {
font-family: Arial;
font-size: 12px;
}
</style>
<div id="main">

<form method="POST" name="mailform">
  <table border="0">
    
    <tr>
      <td colspan="2"><strong>Send us an Enquiry</strong></td>
      </tr>
      
    <tr>
      <td>Your Name<br />
      <input type="text" name="name" id="name" class="from" size="<?php echo $width ?>"></td>
    </tr>
    
    <tr>
      <td>Company<br />
      <input name="company" type="text" class="from" id="companyname" size="<?php echo $company ?>"></td>
    </tr>
    
    <tr>
      <td>Email:<br />
      <input type="text" name="from" id="from" class="from" size="<?php echo $sendmail ?>"></td>
    </tr>
    
    <tr>
      <td>Phone<br />
      <input type="text" name="phone" id="phone" class="from" size="<?php echo $phone ?>"></td>
    </tr>
    
    <tr>
      <td>Fax<br />
      <input type="text" name="fax" id="fax" class="from" size="<?php echo $fax ?>"></td>
    </tr>
    
    <tr>
      <td>Subject:<br />
      <input type="text" name="subject" id="subject" class="subject" size="<?php echo $subject ?>"></td>
    </tr>
    
    <tr>
      <td>Mesage:<br />
      <textarea name="body" id="body" class="body" rows="<?php echo $messageh ?>" columns="<?php echo $messagew ?>"></textarea><br />
      <img src="modules/contactform/captcha.php?.png" alt="CAPTCHA" style="margin-top:4px;" /> <br />
          <input type="text" name="captchastring" size="6" /> (Case Sensitive!)</td> 
    </tr>
    
    <tr>
      <td colspan="2">  
          <input type="hidden" name="toaddress" value="<?php echo $to; ?>" id="toaddress">
          <input name="button" type="button" id="submit" value="Send Enquiry" onclick="return checkForm(); mailIt(this.form); document.mailform.captchastring.value='';">
  <input name="reset" type="reset" value="Reset">
             </td>
             
             </tr>
             </table>
</form>
</div>
<div id="status"></div>
THanking You,
mihir.
Reply With Quote

  #2 (permalink)  
Old Jul 26th, 2007, 10:08
SuperMember

SuperMember
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,111
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Problem: onClick Function

Code: Select all
onclick="return checkForm(); mailIt(this.form); document.mailform.captchastring.value='';"
"Return" will end your function by returning a value (the result of checkForm(), in this case): that's why it doesn't get beyond the first item.

I recommend removing your onclick to the .js javascript file itself. Delete it from the HTML, and write this in the javascript:

Code: Select all
document.getElementById("submit").onclick=function{firstFunction(); secondFunction(); andSoOnFunction();}
Alternatively:

Code: Select all
document.getElementById("submit").onclick=myOnclickFunction()

function myOnclickFunction() {
firstFunction()
secondFunction()
andSoOnFunction()
}
Reply With Quote
  #3 (permalink)  
Old Jul 26th, 2007, 10:16
New Member
Join Date: Jul 2007
Location: Pune, Maharashtra, India
Age: 20
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to mihirc Send a message via Skype™ to mihirc
Re: Problem: onClick Function

Hello,
thanks for the reply.

Just one thing, while defining it in the js where shall the return tag come?

I added this, just as you suggest (i hope so...)
Code: Select all
<script> document.getElementById("submit").onclick=function{return checkForm(); mailIt(this.form); document.mailform.captchastring.value=('');}
</script>
But then, i got this error, in the firebug.

missing ( before formal parameters

thanks,
mihir.

Last edited by mihirc; Jul 26th, 2007 at 10:47.
Reply With Quote
  #4 (permalink)  
Old Jul 26th, 2007, 11:04
SuperMember

SuperMember
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,111
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Problem: onClick Function

Quote:
Originally Posted by mihirc View Post
But then, i got this error, in the firebug.

missing ( before formal parameters
Oops! Sorry, that should have been:

Code: Select all
document.getElementById("submit").onclick=function(){return checkForm(); mailIt(this.form); document.mailform.captchastring.value=('');}
I don't know/understand what your functions do, but I would put the return statement last -- otherwise, it will terminate your function and the other two will not run.
Reply With Quote
  #5 (permalink)  
Old Jul 26th, 2007, 11:22
New Member
Join Date: Jul 2007
Location: Pune, Maharashtra, India
Age: 20
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to mihirc Send a message via Skype™ to mihirc
Re: Problem: onClick Function

Ok,Just to explain you better...

The mailIt function sends a mail to a defined email id. And the checkform validates the form. SO what i want is unless the form is validated the mail shouldnt be going. If i keep the mailIt function before, then it is checking the form and giving the correct error, but it is also sending the mail, which i dont want.

mihir.
Reply With Quote
  #6 (permalink)  
Old Jul 26th, 2007, 11:24
New Member
Join Date: Jul 2007
Location: Pune, Maharashtra, India
Age: 20
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to mihirc Send a message via Skype™ to mihirc
Re: Problem: onClick Function

And after using ur suggested correction,
I came across this error...

document.getElementById("submit") has no properties


Sorry For Putting you through all this trouble...

mihir.
Reply With Quote
  #7 (permalink)  
Old Jul 26th, 2007, 12:38
SuperMember

SuperMember
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,111
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Problem: onClick Function

Quote:
Originally Posted by mihirc View Post
And after using ur suggested correction,
I came across this error...

document.getElementById("submit") has no properties


Sorry For Putting you through all this trouble...

mihir.
Ah ha!

I think that means it's failing to find the element with that ID. Not sure why this is happening, but at least you can add an object detect to avoid the error:

Code: Select all
var x = document.getElementById("submit")
if (x) {
x.onclick=function(){mailIt(this.form); document.mailform.captchastring.value=(''); return checkForm();}
}

This may not actually make it work, but at least it will stop the error message (for pages that lack this element).
Reply With Quote
  #8 (permalink)  
Old Jul 26th, 2007, 12:45
New Member
Join Date: Jul 2007
Location: Pune, Maharashtra, India
Age: 20
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to mihirc Send a message via Skype™ to mihirc
Re: Problem: onClick Function

Hi.

well id did avoid the error, but then it disabled both the scripts from working.

you can take a look at the live site.. www.dev003.tekdi.com

Its a quarter past 6 here... and am going to stay up all night to solve this...

My trial and error is getting me to nothing but sheer, desperation.

mihir.
Reply With Quote
  #9 (permalink)  
Old Jul 26th, 2007, 12:54
SuperMember

SuperMember
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,111
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Problem: onClick Function

Quote:
Originally Posted by mihirc View Post
My trial and error is getting me to nothing but sheer, desperation.
Then stop using trial and error.

Your site's XHTML code is a horrible mess of errors. Fix them all: http://validator.w3.org/
Reply With Quote
  #10 (permalink)  
Old Jul 26th, 2007, 12:55
New Member
Join Date: Jul 2007
Location: Pune, Maharashtra, India
Age: 20
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to mihirc Send a message via Skype™ to mihirc
Re: Problem: onClick Function

I know know,

this is joomla based site...

mihir.
Reply With Quote
  #11 (permalink)  
Old Jul 26th, 2007, 13:05
SuperMember

SuperMember
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,111
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Problem: onClick Function

Quote:
Originally Posted by mihirc View Post
I know know,

this is joomla based site...

mihir.
Sorry, but I'm not willing to debug any website that has a large number of known errors. I don't have any skills with manipulating junk code; it's a black art. I left that behind years ago.

If joomla creates junk code, then you must deal with the consequences
Reply With Quote
  #12 (permalink)  
Old Jul 26th, 2007, 20:44
Junior Member
Join Date: Jul 2007
Location: West Midlands
Age: 26
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Problem: onClick Function

The other alternative is to use the return checkform() method but add more code to the JS function

so
Code: Select all
onclick="return checkForm(); mailIt(this.form); document.mailform.captchastring.value='';"
and simply use
[code]onclick="return checkForm(this.form, ';');"[/cpde]
then your function would look like
Code: Select all
function checkForm(FORM, CAPTCHA){
   mailIt(FORM);
   document.mailform.captchastring.value= CAPTCHA
hope that helps
Reply With Quote
  #13 (permalink)  
Old Jul 27th, 2007, 05:07
New Member
Join Date: Jul 2007
Location: Pune, Maharashtra, India
Age: 20
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to mihirc Send a message via Skype™ to mihirc
Re: Problem: onClick Function

Hi,
thanks for the reply...

Here is what i did as you suggested (putting up the whole code...)

Code: Select all
<? 
$to = $params->get('email');  
$width = $params->get('width'); 
$company = $params->get('company');
$phone = $params->get('phone');
$fax = $params->get('fax');
$subject = $params->get('subject');
$sendmail = $params->get('sendmail');
$messagew = $params->get('messagew');
$messageh = $params->get('messageh');
$val_com = $params->get('val_com'); 
$val_phone = $params->get('val_phone'); 
$val_fax = $params->get('val_fax'); 
?>

<script type="text/javascript" src="modules/contactform/ajaxcontact.js"></script>
<script> 
function checkForm(FORM, CAPTCHA)
{
    var cname, cemail, csubject, cmessage, ccompany, cphone, cfax;
    with(window.document.mailform);
    mailIt(FORM);
   document.mailform.captchastring.value= CAPTCHA
    {
        cname    = name;
        cemail   = from;
        csubject = subject;
        cmessage = body;
        ccompany = company;
        cphone   = phone;
        cfax     = fax;
        
    }
    
    if(trim(cname.value) == '')
    {
        alert('Please enter Your Name');
        cname.focus();
        return false;
    }
    else if(trim(cemail.value) == '')
    {
        alert('Please enter your email');
        cemail.focus();
        return false;
    }
    else if(!isEmail(trim(cemail.value)))
    {
        alert('Email address is not valid');
        cemail.focus();
        return false;
    }
    else if(trim(csubject.value) == '')
    {
        alert('Subject Is Cumpolsary');
        csubject.focus();
        return false;
    }
    else if(trim(cmessage.value) == '')
    {
        alert('Please enter your Enquiry');
        cmessage.focus();
        return false;
    }
    <?php if ($val_com) {?>
        else if(trim(ccompany.value) == '')
    {
        alert('Please enter your Company Name');
        cmessage.focus();
        return false;
    }
    <?php } ?>
    <?php if ($val_phone) {?>
        else if(trim(cphone.value) == '')
    {
        alert('Please enter your Phone Number');
        cmessage.focus();
        return false;
    }
    <?php } ?>
        <?php if ($val_fax) {?>
        else if(trim(cfax.value) == '')
    {
        alert('Please enter your Company Name');
        cmessage.focus();
        return false;
    }
    <?php } ?>
    else
    {
        cname.value    = trim(cname.value);
        cemail.value   = trim(cemail.value);
        csubject.value = trim(csubject.value);
        ccompany.value = trim(ccompany.value);
        cphone.value = trim(cphone.value);
        cfax.value = trim(cfax.value);
        return true;
    }
}

/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
    return str.replace(/^\s+|\s+$/g,'');
}

/*
Check if a string is in valid email format. 
Returns true if valid, false otherwise.
*/
function isEmail(str)
{
    var regex = /^[-_.a-z0-9]+@(([-_a-z0-9]+\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;
    return regex.test(str);
}



</script>

<style type="text/css">
input[type="text"] {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
background-image: url('images/input_bg.png');
background-repeat: no-repeat;
border: 1px solid #999999;
margin: 5px 0 0 0;
padding: 3px 3px 3px 3px;
display: inline;
}
textarea {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
background-image: url('images/input_bg.png');
background-repeat: no-repeat;
border: 1px solid #999999;
margin: 5px 0 0 0;
padding: 3px 3px 3px 3px;
display: inline;
}

* {
font-family: Arial;
font-size: 12px;
}
</style>
<div id="main">

<form method="POST" name="mailform">
  <table border="0">
    
    <tr>
      <td colspan="2"><strong>Send us an Enquiry</strong></td>
      </tr>
      
    <tr>
      <td>Your Name<br />
      <input type="text" name="name" id="name" class="from" size="<?php echo $width ?>"></td>
    </tr>
    
    <tr>
      <td>Company<br />
      <input name="company" type="text" class="from" id="companyname" size="<?php echo $company ?>"></td>
    </tr>
    
    <tr>
      <td>Email:<br />
      <input type="text" name="from" id="from" class="from" size="<?php echo $sendmail ?>"></td>
    </tr>
    
    <tr>
      <td>Phone<br />
      <input type="text" name="phone" id="phone" class="from" size="<?php echo $phone ?>"></td>
    </tr>
    
    <tr>
      <td>Fax<br />
      <input type="text" name="fax" id="fax" class="from" size="<?php echo $fax ?>"></td>
    </tr>
    
    <tr>
      <td>Subject:<br />
      <input type="text" name="subject" id="subject" class="subject" size="<?php echo $subject ?>"></td>
    </tr>
    
    <tr>
      <td>Mesage:<br />
      <textarea name="body" id="body" class="body" rows="<?php echo $messageh ?>" columns="<?php echo $messagew ?>"></textarea><br />
      <img src="modules/contactform/captcha.php?.png" alt="CAPTCHA" style="margin-top:4px;" /> <br />
          <input type="text" name="captchastring" size="6" /> (Case Sensitive!)</td> 
    </tr>
    
    <tr>
      <td colspan="2">  
          <input type="hidden" name="toaddress" value="<?php echo $to; ?>" id="toaddress">

         <input name="submit" type="button" id="submit" value="Send Enquiry" onclick="return mailIt(this.form)">
  
  <input name="reset" type="reset" value="Reset">
    
             </td>
             
             </tr>
             </table>
</form>
</div>
<div id="status"></div>
I really cant get it work...

mihir.
Reply With Quote
  #14 (permalink)  
Old Jul 27th, 2007, 10:13
Junior Member
Join Date: Jul 2007
Location: West Midlands
Age: 26
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Problem: onClick Function

well there are so many things wrong with that code that i'm not even gonna attempt to put the problems here. Give me 5 mins i'll rewrite it for you

Right well I cant test the code fully (as I dont have all the functions and PHP classes) but this should work

Code: Select all
<? 
    $to = $params->get('email');  
    $width = $params->get('width'); 
    $company = $params->get('company');
    $phone = $params->get('phone');
    $fax = $params->get('fax');
    $subject = $params->get('subject');
    $sendmail = $params->get('sendmail');
    $messagew = $params->get('messagew');
    $messageh = $params->get('messageh');
    $val_com = $params->get('val_com'); 
    $val_phone = $params->get('val_phone'); 
    $val_fax = $params->get('val_fax'); 
    
?>

<script type="text/javascript" src="modules/contactform/ajaxcontact.js"></script>
<script type="text/javascript" language="javascript"> 
<!-- #
    function trim(str){
        return str.replace(/^\s+|\s+$/g,'');
    }

    function checkForm(FORM, CAPTCHA){
        var cname, cemail, csubject, cmessage, ccompany, cphone, cfax;

        mailIt(FORM);
        document.getElementById("captchastring").value = CAPTCHA;
        
        // get the form field data
        cname = document.getElementById("name");
        cemail = document.getElementById("from");
        csubject = document.getElementById("subject");
        cmessage = document.getElementById("body");
        ccompany = document.getElementById("company");
        cphone = document.getElementById("phone");
        cfax = document.getElementById("cfax");
        
        if (trim(cname.value) == ""){
            alert('Please enter Your Name');
            cname.focus();
            return false;
        } else if (trim(cemail.value) == ''){
            alert('Please enter your email');
            cemail.focus();
            return false;
        } else if(!isEmail(trim(cemail.value))){
            alert('Email address is not valid');
            cemail.focus();
            return false;
           } else if(trim(csubject.value) == ''){
            alert('Subject Is Cumpolsary');
            csubject.focus();
            return false;
        } else if(trim(cmessage.value) == ''){
            alert('Please enter your Enquiry');
            cmessage.focus();
            return false;
        }<?php if ($val_com) {?> else if(trim(ccompany.value) == '') {
            alert('Please enter your Company Name');
            cmessage.focus();
            return false;
        }<?php }  if ($val_phone) {?> else if(trim(cphone.value) == ''){
            alert('Please enter your Phone Number');
            cmessage.focus();
            return false;
        }<?php }  if ($val_fax) {?> else if(trim(cfax.value) == ''){
            alert('Please enter your Company Name');
            cmessage.focus();
            return false;
        }<?php } ?> else {
            cname.value        = trim(cname.value);
            cemail.value       = trim(cemail.value);
            csubject.value     = trim(csubject.value);
            ccompany.value     = trim(ccompany.value);
            cphone.value     = trim(cphone.value);
            cfax.value         = trim(cfax.value);
            return true;
        }
    }

/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
/*
Check if a string is in valid email format. 
Returns true if valid, false otherwise.
*/
function isEmail(str)
{
    var regex = /^[-_.a-z0-9]+@(([-_a-z0-9]+\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;
    return regex.test(str);
}


-->
</script>

<style type="text/css">
input[type="text"] {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
background-image: url('images/input_bg.png');
background-repeat: no-repeat;
border: 1px solid #999999;
margin: 5px 0 0 0;
padding: 3px 3px 3px 3px;
display: inline;
}
textarea {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
background-image: url('images/input_bg.png');
background-repeat: no-repeat;
border: 1px solid #999999;
margin: 5px 0 0 0;
padding: 3px 3px 3px 3px;
display: inline;
}

* {
font-family: Arial;
font-size: 12px;
}
</style>
<div id="main">

<form method="POST" name="mailform" action="<?=$PHP_SELF;?>" onSubmit="return checkForm(this.form, ';');">
  <table border="0">
    
    <tr>
      <td colspan="2"><strong>Send us an Enquiry</strong></td>
      </tr>
      
    <tr>
      <td>Your Name<br />
      <input type="text" name="name" id="name" class="from" size="<?php echo $width ?>"></td>
    </tr>
    
    <tr>
      <td>Company<br />
      <input name="company" type="text" class="from" id="companyname" size="<?php echo $company ?>"></td>
    </tr>
    
    <tr>
      <td>Email:<br />
      <input type="text" name="from" id="from" class="from" size="<?php echo $sendmail ?>"></td>
    </tr>
    
    <tr>
      <td>Phone<br />
      <input type="text" name="phone" id="phone" class="from" size="<?php echo $phone ?>"></td>
    </tr>
    
    <tr>
      <td>Fax<br />
      <input type="text" name="fax" id="fax" class="from" size="<?php echo $fax ?>"></td>
    </tr>
    
    <tr>
      <td>Subject:<br />
      <input type="text" name="subject" id="subject" class="subject" size="<?php echo $subject ?>"></td>
    </tr>
    
    <tr>
      <td>Mesage:<br />
      <textarea name="body" id="body" class="body" rows="<?php echo $messageh ?>" columns="<?php echo $messagew ?>"></textarea><br />
      <img src="modules/contactform/captcha.php?.png" alt="CAPTCHA" style="margin-top:4px;" /> <br />
          <input type="text" name="captchastring" id="captchastring" size="6" /> (Case Sensitive!)</td> 
    </tr>
    
    <tr>
      <td colspan="2">  
          <input type="hidden" name="toaddress" value="<?php echo $to; ?>" id="toaddress">

         <input name="submit" type="submit" id="submit" value="Send Enquiry" >
  
  <input name="reset" type="reset" value="Reset">
    
             </td>
             
             </tr>
             </table>
</form>
</div>
<div id="status"></div>

Last edited by Paramiliar; Jul 27th, 2007 at 10:17. Reason: added code
Reply With Quote
  #15 (permalink)  
Old Jul 27th, 2007, 10:16
New Member
Join Date: Jul 2007
Location: Pune, Maharashtra, India
Age: 20
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to mihirc Send a message via Skype™ to mihirc
Re: Problem: onClick Function

Hi,
wow cool...
Would you like to check the javascript and the send.php file also? so that it could be easier for you to take a look...

you are being great help...

mihir.
Reply With Quote
  #16 (permalink)  
Old Jul 27th, 2007, 11:09
New Member
Join Date: Jul 2007
Location: Pune, Maharashtra, India
Age: 20
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to mihirc Send a message via Skype™ to mihirc
Re: Problem: onClick Function

Well,

thanks for the code, but its only re-loading the page.

i am attaching the js and the php for you...

Code: Select all
<?php
session_start();

$to = ($_POST['toaddress']);
$from = $_POST['from'];
$subject = stripcslashes($_POST['subject']);
$body.= "\n Name: ".stripcslashes($_POST['name']);
$body.= "\n Company: ".stripcslashes($_POST['company']);
$body.= "\n Phone: ".stripcslashes($_POST['phone']);
$body.= "\n Fax: ".stripcslashes($_POST['fax']);
$body.= "\n Message: ".stripcslashes($_POST['body']);

        if ($_SESSION['CAPTCHAString'] == $_POST['captchastring'])
        {

            if(mail($to, $subject, $body, 'From: '.$from)){
            
            echo "Enquiry Sent! We'll contact you shortly";
            } 
            
            else {
            echo "Mail Sending Failed. Please try again or contact Site Admin";
            }

        }
        else
        {

          echo 'Mail not sent. Please re-enter verification code.';

        }

    ?>


Code: Select all
function mailIt(form){

/*config*/
var url = "modules/contactform/send.php"; /*url of the mailer program*/
var loadingimg = "images/loading.gif"; /* Loading Image */
var XHRequest = false;

if(window.XMLHttpRequest){
XHRequest = new XMLHttpRequest();
} else if(window.ActiveXObject){
XHRequest = new ActiveXObject("Microsoft.XMLHTTP");
}

if(XHRequest){
//XHRequest.open("GET", url+"?to=" + form.to.value + "&from=" + form.from.value + "&subject=" + form.subject.value + "&body=" + form.body.value);
XHRequest.open("POST", url);
XHRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

XHRequest.onreadystatechange = function(){
if(XHRequest.readyState < 4){
document.getElementById("status").innerHTML = "<img src='"+loadingimg+"'> Sending ...";
} 

else if(XHRequest.readyState == 4 && XHRequest.status == 200){
document.getElementById("status").innerHTML = XHRequest.responseText;
delete XHRequest;
XHRequest = null;
}
}
//XHRequest.send(null);


XHRequest.send("&captchastring=" + form.captchastring.value +"&toaddress=" + form.toaddress.value + "&name=" + form.name.value + "&company=" + form.company.value + "&from="+ form.from.value + "&subject=" + form.subject.value + "&phone=" + form.phone.value + "&fax=" + form.fax.value + "&body=" + form.body.value);
}
}
mihir.
Reply With Quote
  #17 (permalink)  
Old Jul 28th, 2007, 11:20
Junior Member
Join Date: Jul 2007
Location: West Midlands
Age: 26
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Problem: onClick Function

Ok one question, why are you using JS to send an email?

What you should be doing is checking the data with Javascript, if it is all there then allowing the form to submit to php which then does all the emailing (of course once it has done tests itself)

So the page just refresh's you dont see any of the php code being executed?

Try This

Code: Select all
<?php
    session_start();
    
    ### I have given you my protectdata function, you should ALWAYS protect 
    ### your data when allowing the user to post data, reason? I could easily
    ### Gain access to your entire site, database and domain by sending
    ### commands in the post field!
    function protectdata($data){
        # function to protect the post and get variables
        $search = array('@<script[^>]*?>.*?</script>@si', '@<style[^>]*?>.*?</style>@siU', '@<![\s\S]*?--[ \t\n\r]*>@');
        $data = preg_replace($search, '', $data);
        $data = stripslashes($data);
        $data = strip_tags($data);
        return $data;
    }

    
    if ($_POST['Submit']){### check the name and id of the submit button, its case sensative
        $to = protectdata($_POST['toaddress']);
        $from = protectdata($_POST['from']);
        $subject = protectdata($_POST['subject']);
        $body.= "\n Name: ".protectdata($_POST['name']);
        $body.= "\n Company: ".protectdata($_POST['company']);
        $body.= "\n Phone: ".protectdata($_POST['phone']);
        $body.= "\n Fax: ".protectdata($_POST['fax']);
        $body.= "\n Message: ".protectdata($_POST['body']);

        if ($_SESSION['CAPTCHAString'] == $_POST['captchastring']){
            $mailing = mail($to, $subject, $body, "From: ".$from."\r\n");
            if ($mailing){
                echo "Enquiry Sent! We'll contact you shortly";
            } else {
                echo "Mail Sending Failed. Please try again or contact Site Admin";
            }
        } else {
            echo 'Mail not sent. Please re-enter verification code.';
        }
    }

    ?>
Reply With Quote
Reply

Tags
onclick

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with displaying random quotes from a function Z101 JavaScript Forum 3 Dec 7th, 2007 22:34
Need help on an onclick function DaveKenroy JavaScript Forum 3 Jan 28th, 2007 11:31
problem calling function via onClick Eagle JavaScript Forum 5 Jan 15th, 2007 21:41
onclick function tomd1985 JavaScript Forum 0 Mar 13th, 2006 18:20
problem with Image Object's onLoad function jono JavaScript Forum 2 Jan 10th, 2005 08:10


All times are GMT. The time now is 21:10.