Jump to content
Killersites Community
Sign in to follow this  
newseed

Need Help With PHP Form

Recommended Posts

I know just enough to not break a php site but I am trying to get this form to work. What I want is when a user clicks on one of the 3 radio buttons the input field below will become active allowing them to enter text. Right now that input field is active but i want it to be inactive until they choose either the 2nd or 3rd radio buttons.

 

I am using a simple free form found here but the source code just does not have that function I am looking for.

Edited by Eddie

Share this post


Link to post
Share on other sites

First thing I noticed: your input looks like this:

 

<input id="item_affiliate" class="currency optional" type="text" name="item[affiliate_field]" disabled="disabled"></span>

 

with an "item_affiliate" id, whereas the jquery code refers to an item with a "item_affiliate_field" id. Those two names need to match.

Share this post


Link to post
Share on other sites

Ok..it's NOW working. I have no clue as to why I did not think of that before.

 

The only issue I have now is the last radio button. Link

 

Right now the first button is set as default thus keeping the input field disabled. I can then choose the second radio button and the input field will become enabled as it should. However, if I click on the third radio button first then it doesn't enable the input field.

 

Looks like we need to add another line of code to make it work but I really don't have a clue why it's not working as is.

Edited by Eddie

Share this post


Link to post
Share on other sites

I think you're just running into the issue because you aren't supposed to have multiple elements on the page sharing the same id. Id's are supposed to be unique per page. Perhaps switch to using classes instead?

Share this post


Link to post
Share on other sites

Thanks for your patience Ben. I managed to get the radio buttons to work where the input field activates when choosing the 2nd and 3rd buttons. I am also able to pass all the variables to form process and email them to myself with the exception of the first radio button. When that button is chosen it gives me an error stating a field is not correctly entered. My suspicion is the input field is still requiring data entry. Somehow the logic requires some kind of if else statement but I don't have a clue how to approach that.

 

Here's the partial code that initiate the varibles:

 

if(!isset($_POST['Full_Name']) ||
	!isset($_POST['Email_Address']) ||
	!isset($_POST['Telephone_Number']) ||
	!isset($_POST['Your_Message']) || 
	!isset($_POST['AntiSpam']) ||
	!isset($_POST['Desired_Date']) ||
	!isset($_POST['affiliate']) || // radio buttons
	!isset($_POST['affiliate_field']) // Input field when the 2nd and 3rd radio buttons are selected

	)

{
	died('Sorry, there appears to be a problem with your form submission.');		
}

Share this post


Link to post
Share on other sites

I'd rewrite the if function to:

 

if(!isset($_POST['Full_Name']) ||
               !isset($_POST['Email_Address']) ||
               !isset($_POST['Telephone_Number']) ||
               !isset($_POST['Your_Message']) || 
               !isset($_POST['AntiSpam']) ||
               !isset($_POST['Desired_Date']) ||
               !isset($_POST['affiliate']) || // radio buttons
               (isset($_POST['affiliate'] && $_POST['affiliate'] != 'Athlete' && !isset($_POST['affiliate_field']) // if 2nd/3rd radio button selected, make sure affiliate_field is filled in
               )

{
               died('Sorry, there appears to be a problem with your form submission.'); // not sure if this should be "die()"?                 
       }

Share this post


Link to post
Share on other sites

It didn't work at first and so I doubled checked the number or open and close ( ) and { } and found a couple of them out of place and one extra.

 

Here's the finished code:

 

if(!isset($_POST['Full_Name']) || 
               !isset($_POST['Email_Address']) || 
               !isset($_POST['Telephone_Number']) || 
               !isset($_POST['Your_Message']) ||  
               !isset($_POST['AntiSpam']) || 
               !isset($_POST['Desired_Date']) || 
               !isset($_POST['affiliate']) || // radio buttons 
               !isset($_POST['affiliate']) && $_POST['affiliate'] != 'Athlete' && !isset($_POST['affiliate_field']) // if 2nd/3rd radio button selected, make sure affiliate_field is filled in 
               ) 


{ 
               die('Sorry, there appears to be a problem with your form submission.'); // not sure if this should be "die()"?                  
       }

 

It's now passing the value corrently for the first radio button.

 

The only issue I have now is validatation for the optional input field. I thought what you done would fix it but it didn't. I only need it to validate when the 2nd or 3rd radio button is selected but not cause an error if the 1st radio is selected because the input field will be blank by default.

 

Here's the validation code:

 

<script type="text/javascript">
required.add('Full_Name','NOT_EMPTY','Full Name');
required.add('Email_Address','EMAIL','Email Address');
required.add('Telephone_Number','NOT_EMPTY','Telephone Number');
required.add('Your_Message','NOT_EMPTY','Your Message');
required.add('AntiSpam','NOT_EMPTY','Anti-Spam Question');
required.add('affiliate_field','NOT_EMPTY','Affiliate');

</script>

 

Here's the core file for the validator:

 

function has_id(id){try{var tmp=document.getElementById(id).value;}catch(e){return false;}
return true;}
function has_name(nm){try{var tmp=cfrm.nm.type;}catch(e){return false;}
return true;}
function $$(id){if(!has_id(id)&&!has_name(id)){alert("Field "+id+" does not exist!\n Form validation configuration error.");return false;}
if(has_id(id)){return document.getElementById(id).value;}else{return;}}
function $val(id){return document.getElementById(id);}
function trim(id){$val(id).value=$val(id).value.replace(/^\s+/,'').replace(/\s+$/,'');}
var required={field:[],add:function(name,type,mess){this.field[this.field.length]=[name,type,mess];},out:function(){return this.field;},clear:function(){this.field=[];}};var validate={check:function(cform){var error_message='Please fix the following errors:\n\n';var mess_part='';var to_focus='';var tmp=true;for(var i=0;i<required.field.length;i++){if(this.checkit(required.field[i][0],required.field[i][1],cform)){}else{error_message=error_message+required.field[i][2]+' must be supplied\n';if(has_id(required.field[i][0])&&to_focus.length===0){to_focus=required.field[i][0];}
tmp=false;}}
if(!tmp){alert(error_message);}
if(to_focus.length>0){document.getElementById(to_focus).focus();}
return tmp;},checkit:function(cvalue,ctype,cform){if(ctype=="NOT_EMPTY"){if(this.trim($$(cvalue)).length<1){return false;}else{return true;}}else if(ctype=="EMAIL"){exp=/^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;if($$(cvalue).match(exp)==null){return false;}else{return true;}}},trim:function(s){if(s.length>0){return s.replace(/^\s+/,'').replace(/\s+$/,'');}else{return s;}}};

 

Thanks.

Share this post


Link to post
Share on other sites

Sorry about the misplaced parenthesis -- I was typing that out quickly. Glad to hear you got it working.

 

I'm not sure if I can help regarding the Javascript validation... My impression is that the validator expects to know which fields it needs to validate up front when the page first loads, not when the form is submitted. That makes it much tricker to add optional validation, and I'm not sure how I'd go about that, short of maybe removing the current Javascript validation and either writing something custom with jQuery that validated when the form was submitted or switching to a different Javascript solution that supported that sort of optional validation.

 

If it's a publicly available validator script, maybe there will be documentation for it?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×