Languages :: PHP :: form submittion |
|||
| By: PHP newbee |
Date: 22/03/2003 00:00:00 |
Points: 20 | Status: Answered Quality : Excellent |
|
I have a php document that submits a form but before it submits the form it checks the values entered. The form validation is written in javascript and the input type for the button is of type button. When the form is submitted I want it to validate the form which it does and then execute some php code. The problem is that with the input type button it verifies the form fine but does not know to execute the php code. If I change the input type to submit it executes the php code but if something is missing in the form it refreshes the page and the values are gone. This is just a sample page I created without the extensive validation because it is less code. <html> <?PHP if ($_POST['button_button']) { if ($_POST['text']=='') echo 'enter test!'; else echo 'this is the button'; } elseif ($_POST['button_submit']) echo 'this is submit'; else echo ''; ?> <form method="POST"> <input type="text" name="text"> <INPUT type="button" name="button_button" value="button"> <INPUT type="submit" name="button_submit" value="submit"> </FORM> </html> If I click on button first it does tell me to "enter test" but if I click on submit first it does not seem to repost. Can anyone help? Thanks in advance! |
|||
| By: TheFalklands | Date: 23/03/2003 07:07:00 | Type : Comment |
|
| Hi, if you want to fill the fields with the previous values change the fields like this : <input type="text" name="text" value="$HTTP_POST_VARS[text]"> I can't find the reason to use the button instead of submit justifiable :-) If you put the form tag as <form method="post" action="...??" onSubmit=myFunction()> then the myFunction will be called when ever the form is submitted, so that you can put the validations there Still if you like to use the button then use it like this <input type='button' name='button' value='button' onClick='myFunction(); formname.submit()'> The myFunction does the javascript verifications. Cheers, |
|||
| By: VGR | Date: 23/03/2003 07:23:00 | Type : Comment |
|
| very easy. Two solutions. either build your FORM with action=nextpage.php and on the button (type=submit) just add an OnClick='javascript:return(validate())' so that validate has to return true or false. This works fine. The other solution is the full-javascript. At the end of positive validation, do (in the javascript) window.location.href='nextpage.php?someparameters' and voilà you are done. |
|||
| By: PHP newbee | Date: 23/03/2003 07:52:00 | Type : Comment |
|
| This is my original file: <form action="<?php print $PHP_SELF?>" method="post" name="wizardform"> Please enter the following information. Required fields are in <b>bold</b>. <script language="javascript"> <!-- function check_input() { if (document.wizardform.fname.value == "") { alert ("First Name cannot be empty!"); return false; } if (document.wizardform.lname.value == "") { alert ("Last Name cannot be empty!"); return false; } if (document.wizardform.email.value == "") { alert ("Email Address cannot be empty!"); return false; } var good; var goodEmail = document.wizardform.email.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)| (\.org)|(\..{2,2}))$)\b/gi); if (goodEmail){ } else { alert('Please enter a valid e-mail address.') document.wizardform.email.focus(); document.wizardform.email.select(); return false; } /*if (ret == 2) { alert ("Email Address contains invalid characters! It must contain a valid email address!"); return false; }*/ if (document.wizardform.addr1.value == "") { alert ("Address (Line 1) cannot be empty!"); return false; } if (document.wizardform.city.value == "") { alert ("City cannot be empty!"); return false; } if (document.wizardform.state.value == "") { alert ("State cannot be empty!"); return false; } //validate zip var zipDigits = 5 var zip = document.wizardform.zip if ((zip.value==null)||(zip.value=="")){ alert("Please Enter your Zip Code") zip.focus() return false } if (isInteger(zip.value)==false){ alert("Please Enter a Valid Zip Code") zip.value="" zip.focus() return false } if (zip.value.length < 5){ alert("Please Enter a Valid Zip Code") zip.value="" zip.focus() return false } //end validate zip //validate extension var zipextDigits = 5 var zipext = document.wizardform.zip_plus if (zipext.value!=""){ if (isInteger(zipext.value)==false) { alert("Please Enter a Valid extension") zipext.value="" zipext.focus() return false } if (zipext.value.length < 4){ alert("Please Enter a Valid extension") zipext.value="" zipext.focus() return false } } //end validate extension if (document.wizardform.country.value == "") { alert ("Country cannot be empty!"); return false; } //validate phone number var digits = "0123456789"; // non-digit characters which are allowed in phone numbers var phoneNumberDelimiters = "()- "; // characters which are allowed in international phone numbers // (a leading + is OK) var validWorldPhoneChars = phoneNumberDelimiters + "+"; // Minimum no of digits in an international phone no. var minDigitsInIPhoneNumber = 10; function isInteger(s) { var i; for (i = 0; i < s.length; i++) { // Check that current character is number. var c = s.charAt(i); if (((c < "0") || (c > "9"))) return false; } // All characters are numbers. return true; } function stripCharsInBag(s, bag) { var i; var returnString = ""; // Search through string's characters one by one. // If character is not in bag, append to returnString. for (i = 0; i < s.length; i++) { // Check that current character isn't whitespace. var c = s.charAt(i); if (bag.indexOf(c) == -1) returnString += c; } return returnString; } function checkInternationalPhone(strPhone){ s=stripCharsInBag(strPhone,validWorldPhoneChars); return (isInteger(s) && s.length >= minDigitsInIPhoneNumber); } var Phone=document.wizardform.dayphone if ((Phone.value==null)||(Phone.value=="")){ alert("Please Enter your Phone Number") Phone.focus() return false } if (checkInternationalPhone(Phone.value)==false){ alert("Please Enter a Valid Phone Number") Phone.value="" Phone.focus() return false } //end validate phone //validate night phone var evePhone=document.wizardform.evephone if (evePhone.value!=""){ if (checkInternationalPhone(evePhone.value)==false){ alert("Please Enter a Valid Night Phone Number") evePhone.value="" evePhone.focus() return false } } //end validate night phone return true; } //--> </script> <TABLE width=600 border=0 cellspacing=0 cellpadding=1> <TR > <TD valign=top class=head > <TABLE width=100% border=0 cellspacing=0 cellpadding=2 class=frame> <TR > <TD class=head3 >Personal Information</TD> </TR> </TABLE> </TD> </TR> <TR > <TD class=head3 > <table width=100% rows=9 border=0 cellspacing=0 cellpadding=1> <tr class=cell1> <td width= ><b>Name</b> </td> <td> <select name=prefix TABINDEX=1 > <option value="Mr">Mr</option> <option value="Mrs">Mrs</option> <option value="Ms">Ms</option> </select> <input type=text name=fname value="" TABINDEX=2 size=15 maxlength=28> <input type=text name=mi value="" TABINDEX=3 size=1 maxlength=1> <input type=text name=lname value="" TABINDEX=4 size=15 maxlength=28> </td> </tr> <tr class=cell1> <td><b>Email</b> </td> <td> <input type=text name=email value="" TABINDEX=5 size=50 maxlength=50> </td> </tr> <tr class=cell1> <td><b>Addr1</b> </td> <td> <input type=text name=addr1 value="" TABINDEX=6 size=50 maxlength=40> </td> </tr> <tr class=cell1> <td>Addr2 </td> <td> <input type=text name=addr2 value="" TABINDEX=7 size=50 maxlength=40> </td> </tr> <tr class=cell1> <td><b>City</b> </td> <td> <input type=text name=city value="" TABINDEX=8 size=15 maxlength=26> <b>State</b> <select name="state" TABINDEX=9> <option value=""> <option value="AL">AL <option value="AK">AK <option value="AZ">AZ <option value="AR">AR <option value="CA">CA <option value="CO">CO <option value="CT">CT <option value="DE">DE <option value="DC">DC <option value="FL">FL <option value="GA">GA <option value="HI">HI <option value="ID">ID <option value="IL">IL <option value="IN">IN <option value="IA">IA <option value="KS">KS <option value="KY">KY <option value="LA">LA <option value="ME">ME <option value="MD">MD <option value="MA">MA <option value="MI">MI <option value="MN">MN <option value="MS">MS <option value="MO">MO <option value="MT">MT <option value="NE">NE <option value="NV">NV <option value="NH">NH <option value="NJ">NJ <option value="NM">NM <option value="NY">NY <option value="NC">NC <option value="ND">ND <option value="OH">OH <option value="OK">OK <option value="OR">OR <option value="PA">PA <option value="RI">RI <option value="SC">SC <option value="SD">SD <option value="TN">TN <option value="TX">TX <option value="UT">UT <option value="VT">VT <option value="VA">VA <option value="WA">WA <option value="WV">WV <option value="WI">WI <option value="WY">WY </select> <SCRIPT LANGUAGE="javascript"> <!-- var x = 0; var found = false; for (x; x<52;x++) { // dc and blank if ( document.wizardform.state.options[x].value == "" ) { document.wizardform.state.options[x].selected = true; found = true; } if (found == true) break; } //--> </SCRIPT> <b>Zip</b> <input type=text name=zip value="" TABINDEX=10 size=5 maxlength=5> - <input type=text name=zip_plus value="" size=4 maxlength=4 TABINDEX=11> </td> </tr> <tr class=cell1> <td><b>Country</b> </td> <td> <input type=text name=country value="USA" TABINDEX=12 size=15 maxlength=20> </td> </tr> <tr class=cell1> <td colspan=2>All phone numbers should be formated: (XXX)XXX-XXXX</td> </tr> <tr class=cell1> <td><b>Day Phone</b> </td> <td> <input type=text name=dayphone value="" TABINDEX=13 size=15 maxlength=14> Night Phone <input type=text name=evephone value="" TABINDEX=14 size=15 maxlength=14> </td> </tr> </table> </TD> </TR> </TABLE> <!-- <TABLE width=600 border=0 cellspacing=0 cellpadding=1> <TR > <TD valign=top class=head ><TABLE width=100% border=0 cellspacing=0 cellpadding=2 class=frame> <TR > <TD class=head3 > Security Information</TD> </TR> </TABLE> </TD> </TR> <TR > <TD class=head3 ><table width=100% rows=3 border=0 cellspacing=0 cellpadding=1> <tr class=cell1> <td width= ><b>Question</b> </td> <td width= ><input type=text name=secquestion value="" TABINDEX=15 size=30 maxlength=50> </td> </tr> <tr class=cell1> <td ><b>Answer</b> </td> <td ><input type=text name=secanswer value="" TABINDEX=16 size=30 maxlength=50> </td> </tr> </table></TD> </TR> </TABLE> --> <input type="button" value="Next" onClick="javascript:check_input();" style="border:1 solid #000000; cursor:pointer; cursor:hand; width:75" name="Submit"> <input type="reset" style="border:1 solid #000000; cursor:hand; cursor:pointer; width:75"> the validation works fine with the input type as button for the submit button, but if I change the input type to submit for the submit button, if one of the fields are not filled in properly it will not save the values in the text fields like it does with the input type as button. I also want to add php code to this that will add the data to a mysql database but when submitted the name="button" variable does not seem to post. |
|||
| By: VGR | Date: 23/03/2003 19:27:00 | Type : Answer |
|
| As already said : don't do this <input type="button" value="Next" onClick="javascript:check_input();" style="border:1 solid #000000; cursor:pointer; cursor:hand; width:75" name="Submit"> it doesn't work do <input type="submit" value="Next" onClick="javascript:return(check_input());" style="border:1 solid #000000; cursor:pointer; cursor:hand; width:75" name="Submit"> OR stick with your type=button and do this in the OnClick : onClick="javascript:if(check_input()) myform.submit();" you have to name the FORM with name="myform", of course |
|||
| By: PHP newbee | Date: 23/03/2003 19:47:00 | Type : Comment |
|
| This worked: input type="submit" value="Next" onClick="javascript:return(check_input());" style="border:1 solid #000000; cursor:pointer; cursor:hand; width:75" name="Submit"> Thanks A lot VGR, I've been trying to figure this out for so long. |
|||
| By: PHP newbee | Date: 23/03/2003 19:49:00 | Type : Comment |
|
| Excellent job! |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








