bradghost Posted October 23, 2011 Share Posted October 23, 2011 Hi all, it's been a while since I wrote in PHP but a University project has forced me to touch up on my knowledge of it. I'm trying to re-create an old form making system I made a few years ago from memory. But it's not working. I know it's something very obvious but I just need another eye to take a look for me. I get the following error message: Warning: Invalid argument supplied for foreach() in /home/birdneto/public_html/rebelfrogv2/p_createaccount.php on line 169 So I know it's do with my show_form(); function. Can anyone help? <div id="page-body-special"> <?php // List page details $page_header = "Create your account"; $page_desc = "Enter your details below to create an account. Creating an account comes with great benefits! You can create a wishlist, sign up for our mailing list and purchase items. Make sure you enter all your information correctly."; // List form details $form_name = "createaccount"; // List all the field names here $field_email_address = "email_address"; $field_email_address_confirm = "confirm_email_address"; $field_password = "password"; $field_password_confirm = "confirm_password"; $field_house_number = "house_number"; $field_street_name = "street_name"; $field_city = "city"; $field_county = "county"; $field_postcode = "postcode"; $field_phone_number = "phone_number"; $field_newsletter = "newsletter"; $field_submit = "submit"; // Define all the field meta data (field name, maxlength, minlength, field type, [field options], [field description]) $fields = array( array($field_email_address, 80, 8, "text", $_POST[$field_email_address]), array($field_email_address_confirm, 80, 8, "text", $_POST[$field_email_address_confirm]), array($field_password, 16, 8, "password", $_POST[$field_password]), array($field_password_confirm, 16, 8, "password", $_POST[$field_password_confirm]), array($field_house_number, 2, 4, "password", $_POST[$field_house_number]), array($field_street_name, 16, 8, "password", $_POST[$field_street_name]), array($field_city, 16, 8, "password", $_POST[$field_city]), array($field_county, 16, 8, "password", $_POST[$field_county]), array($field_postcode, 16, 8, "password", $_POST[$field_postcode]), array($field_phone_number, 16, 8, "password", $_POST[$field_phone_number]), array($field_newsletter, 0, 0, "checkbox", $_POST[$field_newsletter], 0, "Would you like to sign up to our newsletter?"), array($field_submit, 0, 0, "submit", "Create Account") ); // Check if form has been submitted or not. All fields must be declared here with an if statement if($_POST['submit']) { // Set no_errors and show_form to inital 0 $no_errors = 1; function show_errors($field) { if($field == $field_email_address) { if(strlen($_POST[$field_email_address]) < $fields[0][2] and strlen($_POST[$field_email_address]) > $fields[0][1]) { $no_errors = 0; $error = "Your email address must be between " . $fields[0][2] . " and " . $fields[0][1] . " characters"; } } if($field == $field_email_address_confirm) { if($_POST[$field_email_address_confirm] != $_POST[$field_email_address]) { $no_errors = 0; $error = "Your emails do not match each other"; } } if($field == $field_password) { if(strlen($_POST[$field_password]) < $fields[2][2] and strlen($_POST[$field_password]) > $fields[2][1]) { $no_errors = 0; $error = "Your password must be between " . $fields[2][2] . " and " . $fields[2][1] . " characters"; } } if($field == $field_password_confirm) { if($_POST[$field_password_confirm] != $_POST[$field_password_confirm]) { $no_errors = 0; $error = "Your passwords do not match"; } } if($field == $field_house_number) { if(strlen($_POST[$field_house_number]) < $fields[4][2] and strlen($_POST[$field_house_number]) > $fields[4][1]) { $no_errors = 0; $error = "Your house number must be between " . $fields[4][2] . " and " . $fields[4][1] . " numbers"; } } if($field == $field_street_name) { if(strlen($_POST[$field_street_name]) < $fields[5][2] and strlen($_POST[$field_street_name]) > $fields[5][1]) { $no_errors = 0; $error = "Your street name must be between " . $fields[5][2] . " and " . $fields[5][1] . " characters"; } } if($field == $field_city) { if(strlen($_POST[$field_city]) < $fields[6][2] and strlen($_POST[$field_city]) > $fields[6][1]) { $no_errors = 0; $error = "Your city must be between " . $fields[6][2] . " and " . $fields[6][1] . " characters"; } } if($field == $field_county) { if(strlen($_POST[$field_county]) < $fields[7][2] and strlen($_POST[$field_county]) > $fields[7][1]) { $no_errors = 0; $error = "Your county must be between " . $fields[7][2] . " and " . $fields[7][1] . " characters"; } } if($field == $field_postcode) { if(strlen($_POST[$field_postcode]) < $fields[8][2] and strlen($_POST[$field_postcode]) > $fields[8][1]) { $no_errors = 0; $error = "Your postcode must be between " . $fields[8][2] . " and " . $fields[8][1] . " characters"; } } if($field == $field_phone_number) { if(strlen($_POST[$field_phone_number]) < $fields[7][2]) { $no_errors = 0; $error = "Your phone number must be under " . $fields[7][2] . " numbers"; } } return $error; } if($no_errors == 0) { show_form(); } else { echo "<p>Form posted</p>"; echo $br . $br; } } else { show_form(); } function show_form() { // Show the page header echo $br . '<form method="post" action="./?p=' . $page . '" name="' . $form_name . '">'; echo $br . $br; echo '<table id="form-' . $form_name . '">'; echo $br; // List all fields in form format foreach($fields as $meta) { // Explode the array and implode it, changes commas to spaces, then makes all words have capital letters $label_exp = explode("_", $meta[0]); $label_imp = ucwords(implode(" ", $label_exp)); // Is the field type a submission? if($meta[3] == "submit") { echo ' <tr><td colspan="2"><input type="' . $meta[3] . '" name="' . $meta[0] . '" class="' . $meta[0] . '" value="' . $meta[4] . '" /></td></tr>'; echo $br; } // Is the field type a select? elseif($meta[3] == "select") { $options = explode(",", $meta[5]); echo ' <tr><td><label>' . $label_imp . '</label></td><td><select type="' . $meta[3] . '" name="' . $meta[0] . '" class="' . $meta[0] . '">'; echo $br; foreach($options as $option) { function is_selected() { if($option == $_POST[$meta[0]]) { echo 'selected="selected"'; } } echo(' <option ' . is_selected($option) . '>' . $option . '</option>'); echo $br; } echo ' </select></td></tr>'; echo $br; } // Is the field type a checkbox? elseif($meta[3] == "checkbox") { echo ' <tr><td><label>' . $meta[6] . '</label></td><td><input type="' . $meta[3] . '" name="' . $meta[0] . '" class="' . $meta[0] . '" /></td></tr>'; echo $br; } // Is the field type anything else? else { echo ' <tr><td><label>' . $label_imp . '</label></td><td><input type="' . $meta[3] . '" name="' . $meta[0] . '" class="' . $meta[0] . '" maxlength="' . $meta[1] . '" value="' . $meta[4] . '" /></td></tr>'; echo $br; } } // Show the page footer echo '</table>'; echo $br . $br; echo '</form>'; echo $br . $br; } echo $br . "<h1>" . $page_header . "</h1>"; echo $br . $br; echo "<p>" . $page_desc . "</p>"; echo $br . $br; echo '<div class="clear"></div>'; echo $br . $br; ?> </div> Quote Link to comment https://forums.phpfreaks.com/topic/249660-dynamic-form/ Share on other sites More sharing options...
gizmola Posted October 23, 2011 Share Posted October 23, 2011 It's very simple: Warning: Invalid argument supplied for foreach() in /home/birdneto/public_html/rebelfrogv2/p_createaccount.php on line 169 That indicates that there is a foreach loop on that line that is expecting an array to foreach through, but it is not an array. This is not necessarily an error depending on the nature of your code. However, if you want to catch it in advance you could use is_array(). Also in a production environment people will typically set the errorlevel so that warnings are not triggered. In the future you should identify for people what line the error is pointing to. See error_reporting Quote Link to comment https://forums.phpfreaks.com/topic/249660-dynamic-form/#findComment-1281632 Share on other sites More sharing options...
bradghost Posted October 23, 2011 Author Share Posted October 23, 2011 Okay thanks. So can you not refer to arrays outside a function? Because that is basically what I was trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/249660-dynamic-form/#findComment-1281633 Share on other sites More sharing options...
xyph Posted October 23, 2011 Share Posted October 23, 2011 Use arguments and return the array <?php function add20( $number ) { $total = 20 + $number; return $total; } $someNum = 15; $total = add20( $someNum ); echo $total; ?> Quote Link to comment https://forums.phpfreaks.com/topic/249660-dynamic-form/#findComment-1281635 Share on other sites More sharing options...
bradghost Posted October 23, 2011 Author Share Posted October 23, 2011 So there is no way to refer to a 2 dimensional array outside of a function? I know how to use parameters but my function shouldn't require them. Quote Link to comment https://forums.phpfreaks.com/topic/249660-dynamic-form/#findComment-1281639 Share on other sites More sharing options...
xyph Posted October 23, 2011 Share Posted October 23, 2011 You can, but that involves putting variables into the global scope. This is considered bad in general, because you can easily lose track of how your variables are being manipulated. Why don't you just pass it by reference as a parameter? Quote Link to comment https://forums.phpfreaks.com/topic/249660-dynamic-form/#findComment-1281641 Share on other sites More sharing options...
bradghost Posted October 23, 2011 Author Share Posted October 23, 2011 I see. I could use parameters but it wouldn't be plausible because my function is outputting an entire contact form using many different fields dynamically. The idea of my code was to make a blueprint for my form so that when I need to add a field I just need to put the information into the array but now it's just looking too complication so I might need to double back on myself and change most of it. I wouldn't even know where to start on using parameters with my function referring to arrays. If you read the function show_form(); you will see what I mean. Quote Link to comment https://forums.phpfreaks.com/topic/249660-dynamic-form/#findComment-1281643 Share on other sites More sharing options...
xyph Posted October 23, 2011 Share Posted October 23, 2011 Perhaps you want to use a class/object then? You aren't using functions the way they are meant to be used. Just use raw code unless you need to reuse certain smaller bits. When that happens, make a function an pass all needed information in to it. Quote Link to comment https://forums.phpfreaks.com/topic/249660-dynamic-form/#findComment-1281652 Share on other sites More sharing options...
bradghost Posted October 24, 2011 Author Share Posted October 24, 2011 Okay I found a way around it, I'm going to parse the minlength and maxlength through in the function using parameters like you said in the first place. I just couldn't see it last night. Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/249660-dynamic-form/#findComment-1281802 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.