Jump to content

My Array Wont check the values


JREAM

Recommended Posts

Hey can anyone help me make this $required array check the values in the for loop?

You can see what im trying to do pretty basic

Just scroll to the bottom where // Validate is! Please!

<?php

// Top Two
$package = $_POST['package'];
$time_frame = $_POST['time_frame'];

// Details
$company = $_POST['company'];
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$city_state_zip = $_POST['city_state_zip'];
$purchase_order = $_POST['purchase_order'];

// Site Details
$site_details = $_POST['site_details'];
$i_have = $_POST['i_have'];
$sites_you_like = $_POST['sites_you_like'];

// If Submitted
if (isset($_POST['submit'])) {

// Required
$required = array ($package, $time_frame, $name, $email, $phone, $address, $city_state_zip);

// Validate
for ($i = 0; $i < count($required); $i++) {
	if ($required[$i] = '') {
	echo 'error' . $required ;
	}
}
}

else {echo 'good';}

?>

Link to comment
https://forums.phpfreaks.com/topic/128303-my-array-wont-check-the-values/
Share on other sites

You need a double =

<?php

// Top Two
$package = $_POST['package'];
$time_frame = $_POST['time_frame'];

// Details
$company = $_POST['company'];
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$city_state_zip = $_POST['city_state_zip'];
$purchase_order = $_POST['purchase_order'];

// Site Details
$site_details = $_POST['site_details'];
$i_have = $_POST['i_have'];
$sites_you_like = $_POST['sites_you_like'];

// If Submitted
if (isset($_POST['submit'])) {

// Required
$required = array ($package, $time_frame, $name, $email, $phone, $address, $city_state_zip);

// Validate
for ($i = 0; $i < count($required); $i++) {
	if ($required[$i] == '') {
	echo 'error' . $required ;
	}
}
}

else {echo 'good';}

?>/code]

A better way of doing this would be to put the names of the fields in the required array and check the actual posted values:

<?php
$required = array('package','time_frame','name','email','phone','address','city_state_zip');
if (isset($_POST['submit'])) {
   foreach ($required as $fld)
      if (strlen(trim(stripslashes($_POST[$fld]))) == 0)
         echo 'Field ' . $fld . ' required';
}
?>

 

Ken

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.