Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Making Fields Required...


bmac

Recommended Posts

Anyone know how to make fields in the create_account.php file required? I need "Company" to be a required field but have no idea how to do this.

 

Also, anyone know how to change the input box to a drop down menu? Tried asking earlier but to no avail...

 

Thanks all, appreciate it.

 

Brian

Link to comment
Share on other sites

basically you have two options:

1) use javascript. You can use Javascript to check whether a field is empty or not. if it is then don't allow the form to be submitted. Very quick and easy to install but can be easily circumvented by turning javascript off

 

2) use PHP. After the form is submitted check to see whether the value exists. if it doesn't send the user back to the form to complete the form.

Something along these lines

<?php
$valid = true;

//The super global $_REQUEST holds both get and post variables 
if (!$_REQUEST[company]) {
$error_message = 'No company';
$valid = false;
}

//repeat these if statements for every field you want to validate
if (!$_REQUEST[email]) {
$error_message = 'No email';
$valid = false;
}

if ($valid == true){
INSERT INTO DATABASE OR WHATEVER
}else{
//FILENAME_FORM should be defined to be the url to the form
tep_redirect(tep_href_link(FILENAME_FORM));
}
?>

 

Because the PHP is processed severside, it cannot be turned off and if written correctly can be totally foolproof (until they make a better fool). It is more work for the coder.

 

I am just about to write some code to make some of the admin fields required and this is how i am planning to do it. I can post my efforts when i am done if you like.

I ain't got time to bleed

Link to comment
Share on other sites

oh for the dropdown try

 

$values[] = array('id' =>"" , 'text' =>"Select");
$values[] = array('id' =>"1" , 'text' =>"option1");
$values[] = array('id' =>"2" , 'text' =>"option2");
etc.....

$form .=  tep_draw_form('form_name', 'form_process_script.php', 'POST');
$form .=  '<br>'. tep_draw_pull_down_menu('drop_down_name', $values, $_REQUEST[image_attrib], 'onChange="this.form.submit();"');
$form .=  '</form>';
echo $form;

 

First you build an array of options, this can also be done in a loop.

Then use tep_draw_form to start the form, you should specify the name of the form and you must specify the correct action of the form, IE what script will process the result.

The call tep_draw_pull_down_menu() passing it the array of values setup earlier.

Close the form and echo the result.

 

Obviously you can add in other fields and the like

I ain't got time to bleed

Link to comment
Share on other sites

Toby,

 

Cheers mate, appreciate the help. This looks _perfect_. I'll give it a shot here this afternoon and let you know if it works out. Also, if you do write some code regarding the validation I'd love to see how it works out. Thanks again,

 

Brian

 

 

 

 

 

 

 

oh for the dropdown try

 

$values[] = array('id' =>"" , 'text' =>"Select");
$values[] = array('id' =>"1" , 'text' =>"option1");
$values[] = array('id' =>"2" , 'text' =>"option2");
etc.....

$form .=  tep_draw_form('form_name', 'form_process_script.php', 'POST');
$form .=  '<br>'. tep_draw_pull_down_menu('drop_down_name', $values, $_REQUEST[image_attrib], 'onChange="this.form.submit();"');
$form .=  '</form>';
echo $form;

 

First you build an array of options, this can also be done in a loop.

Then use tep_draw_form to start the form, you should specify the name of the form and  you must specify the correct action of the form, IE what script will process the result.

The call tep_draw_pull_down_menu() passing it the array of values setup earlier.

Close the form and echo the result.

 

Obviously you can add in other fields and the like

Link to comment
Share on other sites

open includes/form_check.js.php

 

find:

check_input("email_address", <?php echo ENTRY_EMAIL_ADDRESS_MIN_LENGTH; ?>, "<?php echo ENTRY_EMAIL_ADDRESS_ERROR; ?>");

 

add:

check_input("company", '', "<?php echo 'Please enter a company'; ?>");

 

nb. not multi-lingual. If you want to use the language files let me know it's not that hard just more files to edit

I ain't got time to bleed

Link to comment
Share on other sites

Perfect. Swamped at work yesterday but I'll try this right now (and the drop down menu). Appreciate all your help!

 

cheers,

 

Brian

 

 

open includes/form_check.js.php

 

find:

check_input("email_address", <?php echo ENTRY_EMAIL_ADDRESS_MIN_LENGTH; ?>, "<?php echo ENTRY_EMAIL_ADDRESS_ERROR; ?>");

 

add:

check_input("company", '', "<?php echo 'Please enter a company'; ?>");

 

nb. not multi-lingual. If you want to use the language files let me know it's not that hard just more files to edit

Link to comment
Share on other sites

Toby,

 

The dropdown is working perfectly, thanks. Made a few changes to incorporate it into the current create_account file. I'm not sure what this does, however:

 

$_REQUEST[image_attrib]

 

I left it in there b/c everything seems to be working well; but, just wasn't sure what that line does.

 

The validator on the "company" field works perfectly, too. Thanks again,

 

Brian

Link to comment
Share on other sites

no problem.

 

 

As for $_REQUEST[image_attrib], PHP has a set of super globals. These are $_SESSION, $_POST, $_COOKIE, $_GET, $_REQUEST, $_FILE and the big daddy $GLOBALS

 

Each super global is basically an array.

$_COOKIE holds all current cookie variables. So $_COOKIE[user_name] would access the username stored in a cookie for example.

$_FILE holds all sorts of info when uploading files

 

$_POST will hold all vars that are submitted by a form when set to post. So for example when submitting a field called 'company' in a form via the post method you could access it by using $_POST[company].

$_GET does the same thing when used with the GET method and anything in the query string ie index.php?cPath=4 would be $_GET[cPath]

$_REQUEST is simply both $_POST and $_GET merged together.

 

and $GLOBAL holds everything.

 

I use this function in the footer.php file when writing OSc code to keep track of whats going on in the $GLOBALS array. Displaying the contents of the $_REQUEST array is very handy when playing with forms.

 

function debug($array) {
echo '<table width="800" border="0" cellspacing="0" cellpadding="2">';
echo '<tr bgcolor="#666666">
    <td>Key</td>
    <td>Value</td>
  </tr>';

foreach($array as $key=>$value) {

if (is_object($value) AND $object_vars = get_object_vars($value)){

echo '<tr bgcolor="#666666">
	 <td>Object:</td>
	 <td>'.$key.'</td>
 </tr>';

foreach($object_vars as $ob_key=>$ob_value) {
 echo '<tr bgcolor="#ffffff">
   <td>o> '.$ob_key.'</td>
   <td>o> '.$ob_value.'</td>
	 </tr>';
 if (is_array($ob_value)){
	 foreach($ob_value as $arr_key=>$arr_value) {
   echo '<tr bgcolor="#F8F8F8">
  	 <td>a> '.$arr_key.'</td>
  	 <td>a> '.$arr_value;'</td>
   </tr>';

   if (is_array($arr_value)){
  	 foreach($arr_value as $arr2_key=>$arr2_value) {
     echo '<tr bgcolor="#F8F8F8">
    	 <td>a2> '.$arr2_key.'</td>
    	 <td>a2> '.$arr2_value;'</td>
     </tr>';
  	 }
   }
	 }
 }
}

}
print <<<END
  <tr bgcolor="#cccccc">
    <td>$key</td>
    <td>$value</td>
  </tr>
END;
}

echo '</table>';
}
//end of function

//function call displaying all globals
debug($GLOBALS);

//displays all get and post vars, great for debugging form submissions
debug($_REQUEST);

 

If you think this would be useful as a contrib, i'll clean it up and post it.

 

note

In previous versions of php (version's below 4) there were other varabiles that did the same thing like HTTP_POST_VARS is exactly the same as $_POST. Os Commerce uses the old style varables, this is for backward compatibility reasons. I use the new style to provide future proofing. i believe it's always better to look forward than get stuck in the past.

I ain't got time to bleed

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...