Jump to content

PHP If Test


oneoleguy

Recommended Posts

One PHP file collects a numeric value for a variable called 'fields'It passes it to another program which test it for being numeric. That program complains:Notice: Undefined variable: fields in /home/vh1176/www/billsplace.us/fg/fields.php on line 176You did not enter a valid number. Please use a number between 1 and 99. The number I entered was 6.I don't understand PHP enough to know what is causing the error. The first program collects the value for 'fields' like this:form action="fields.php" method=post>Enter the number of fields: <input type=text name="fields"> The second program performs the test to see that the value entered is numeric:This is line 176, the one being complained aboutif(!ereg("^([1-9]{1})([0-9]{0,1})$",$fields))Can anyone tell me what the error is?Bill

Link to comment
Share on other sites

I do not know. The code was given to me. It is a form generator. This is a snippet of the code:

							   <table>									   <tr>									 <td height="153"> 									   <table border="0" cellspacing="0" cellpadding="1" bgcolor="#333333">										 <tr>										   <td> 											 <table border="0" cellspacing="0" cellpadding="2" bgcolor="#ffcc00" width="380">											   <tr> 												 <td  bgcolor="#333333"><font color="#FFFFFF"><b> 												   Step 1: number of fields</b></font></td>												 <td  bgcolor="#333333"  align="right" class="small"> 												   <font color="#FFFFFF"></font>												 </td>											   </tr>											   <tr> 												 <td colspan="2" class="small"> 												   please enter the number 												   of fields your form is 												   going to have. enter only 												   numeric digits. the number 												   should be between 1 - 												   99<br>												   <br><form action="fields.php" method=post> 												   Enter the number of fields: 												   <input type=text name="fields">												   <br>												   <br>												   <input type=submit value="next">												 </td>											   </tr>											 </table>										   </td>										 </tr>									   </table>									   <br>									 </td>								   </tr>								 </table>

I suppose the answer is NO it doesn't use the

$_POST['fields']

statement! Should it?

													  <td colspan="2" class="small"> 														now please enter the specific 														information for each field. 														check the required check 														box if you want this form 														field to be required (users 														who don't enter this field 														will get an error and 														will be prompted to try 														again). the field size 														1 and size 2 are flexible. 														if you choose text box 														as your field type, then 														field size 1 is optional 														and field size 2 is not 														required. the value of 														field size 1 will be applied 														to your text box, if left 														blank the default value 														will be used. if you choose 														drop down select or radio 														button, field size 1 is 														required in that case 														and they denote the number 														of options for those two 														fields. if you choose 														text area, then size 1 														and size 2 are both required 														and they denote rows and 														columns of the text area. 														<br>														<br>														<!-- The form stuff --> 														<?php$errors=0;$error="The following errors occured while processing your request:<ul>";if(!ereg("^([1-9]{1})([0-9]{0,1})$",$fields)) <===== This is line 176 <====={$errors++;$error="<li> You did not enter a valid number. Please use a number between 1 and 99.";}

As I said, I entered 6.Bill

Link to comment
Share on other sites

but where is $fields defined previously? It's an empty variable if you haven't given it a value.Try putting this:$fields = $_POST['fields'];$errors=0;$error="The following errors occured while processing your request:<ul>";if(!ereg("^([1-9]{1})([0-9]{0,1})$",$fields)){$errors++;$error="<li> You did not enter a valid number. Please use a number between 1 and 99.";}

Link to comment
Share on other sites

All variables sent through forms are received in their repective super-global arrays - e.g. variables sent through a POST form are placed in the $_POST array. So the entered value of your field named "fields" will end up in $_POST['fields'].

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...