Jump to content

I'm unable to loop the same variable instance twice


scrubbicus

Recommended Posts

So I have an e-mail form that I'm creating

 

$form = new form('contact');

if(!isset($_POST['submit'])) {

  $form->displayForm();

} else {

  if($form->checkForm() == NULL || $form->checkForm() == true) {

    $form->displaySubmit();

  }

}

 

So I have that to call the functions and I don't want to have to copy and paste everything in the function but within each of the functions is this loop.

 

while($row = mysql_fetch_array($this->_results)) {

 

}

 

I first had this problem when I tried to generate the sent message but I fixed that by create an isset and !isset for both the form and the sent message. Now I'm trying to error check the form by looping through my table to get the $_POST form names then check if any of those are empty() and if nothing is empty then nothing gets returned false so displaySubmit() can be called. It does get to that but it doesn't generate the sent message because I'm guessing the checkForm() is already using that instance variable and looping it.

Link to comment
Share on other sites

Okay here's the whole class

 

<?php

class form {

private $_results;

 

public function __construct($form) {

$this->_results = mysql_query("SELECT * FROM forms WHERE module='$form' ORDER BY id ASC");

}

 

 

 

 

public function displayForm() {

$count = '1';

$num_cols = '2';

echo '<div id="div_form">';

echo '<form method="post" action="#">';

while($row = mysql_fetch_array($this->_results)) {

echo '<div id="div_form" class="column'.$count.'">';

if($row['type'] == 'text') {

echo '<div class="form">';

echo '<div class="form_header">'.ucwords($row['name']).'</div>';

echo '<div class="form_field"> <input type="text" name="'.strtolower($row['name']).'" maxlength="'.$row['maxlength'].'" /> </div>';

echo '</div>';

} elseif($row['type'] == 'select') {

echo '<div class="form">';

echo '<div class="form_header">'.$row['name'].'</div>';

echo '<div class="form_field">';

echo '<select name="'.strtolower($row['name']).'">';

$parameters = explode(',',$row['parameters']);

foreach($parameters as $i) {

echo '<option name="'.$i.'">'.ucwords($i).'</option>';

}

echo '</select></div></div>';

} elseif($row['type'] == 'textarea') {

echo '<div class="form">';

echo '<div class="form_header">'.ucwords($row['name']).'</div>';

echo '<div class="form_field"> <textarea name="'.strtolower($row['name']).'"></textarea> </div>';

echo '</div>';

}

if($count == $num_cols) { $count = '0'; } $count++;

echo '</div>';

}

echo '<div id="form" class="submit"> <input type="submit" name="submit" value="Send E-Mail" id="button" class="email" /> </div> </div>';

}

 

 

 

 

 

public function checkForm() {

while($row = mysql_fetch_array($this->_results)) {

$value = $_POST[strtolower($row['name'])];

$field = strtolower($row['name']);

if($field == 'category') {

if($value == '-- Select One --') {

die('<b>Please choose a category</b> <br /> Hit back on your browser.');

return false;

}

}

elseif(empty($value)) {

die('<b>Please fill out all required fields</b> <br /> Hit back on your browser.');

return false;

}

}

}

 

 

 

 

 

public function displaySubmit() {

echo '<p class="header"> Thank You, '.$_POST['name'].' </p>';

echo '<p class="message">Thank you for submitting your e-mail. We will get back to you as soon as possible. You\'ll be receiving an auto-response. You may want to check your <b>spam</b> box it\'s been known to end up there.</p>';

echo '<p class="header"> Sent E-Mail Message </p>';

while($row = mysql_fetch_array($this->_results)) {

echo '<b>'.ucwords($row['name']).':</b> '.htmlentities(mysql_real_escape_string($_POST[strtolower($row['name'])])).'<br />';

}

}

 

 

}

Link to comment
Share on other sites

I have enabled them but it's not giving me any errors it's just not looping through the displaySubmit()

 

Forgive the PHP structure. It's my first try at trying to make a fully dynamic website. I'm trying to make the second version of my website to be kind of like a CMS. I figure developing one of those would throw a bunch of new challenges at me.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.