Jump to content

Need Help With PHP Form


newseed

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
Link to comment
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
Link to comment
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.');		
}

Link to comment
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()"?                 
       }

Link to comment
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.

Link to comment
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?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...