Jump to content

dynamic form checkboxes


turkeyslyr

Recommended Posts

I would appreciate any help. I am new to php and have been given a task of coming up with a form using an outside data file not MSQL but a CSV or TXT file type. I need to make the data from these files (it would be peoples names) load into the form dynamically and create checkboxes that state either they selected yes or no. Right now the TXT file has the input statue included. Can that be added dynamically to the form. So the TXT file just has names? And they get dumped into the form.

 

There will be multiple forms for different areas of our business so the number of names is unknown. Then I need to mail their selection off when they hit submit.

 

I have been able to do this manually but with some 150 forms I don't have the time to devote souly to this. Plus there has to be an easier way. Hopefully?

 

<?php

$errmsg  = 'Could not send file!'; // error message

if(isset($_POST['send']))
{
$Jay=($_POST['Jay'])?"Jay Alloway: Yes":"Jay Alloway: No";
$Richard=($_POST['Richard'])?"Richard Becker: Yes":"Richard Becker: No";
$Nancy=($_POST['Nancy'])?"Nancy Becker: Yes":"Nancy Becker: No";
$Julie=($_POST['Julie'])?"Julie Bell: Yes":"Julie Bell: No";

//	$email   = $_POST['email'];
//	$question = $_POST['question'];
$subject = 'Constituency Employee Verification';

$headers = "MIME-Version: 1.0\r\n";
$headers = "Content-type: text/html; charset=iso-8859-1\r\n";
$headers = "Content-Transfer-Encoding: 7bit\r\n";
$headers = "From: $email\r\n";

if(trim($email) == '')
{
	$errmsg = 'Please enter your email address';
}
else if(!isEmail($email))
{
	$errmsg = 'Your email address is not valid';
}

if($errmsg == '')
{
	if(get_magic_quotes_gpc())
	{
		$question = stripslashes($question);
	}	

	// the email will be sent here
	$to = "yourmail@mail.com";
	// the email subject ( modify it as you wish )
	$subject = 'Results from Academic Services Technology Verification form' ;

	// the mail message ( add any additional information if you want )
	$msg = "$Jay \n$Richard \n$Nancy \n$Julie \n\n" . $message;

	//mail($to, $subject, $msg, "From: $from\r\nReply-To: $from\r\nReturn-Path: $from\r\n");

	$mailsent = mail("$to,$email", $subject, $msg, $headers);
	if (mailsent) {
	echo "<h2>Thank you for filling out our Constituency Employee form!</h2>";
	echo "<h3>Your message has been sent!</h3>";
	echo "<p>Please print the following for your records:</p>";
	echo "<p><b>Subject:</b> $subject</p>";
	echo "<p>$Jay<br>";
	echo "$Richard<br>";
	echo "$Nancy<br>";
	echo "$Julie<br>";

//		echo "<p><b>E-mail:</b><br>$email</p>";
//		echo "<p><b>Additional Employees:</b> <br>";
//		echo "$question</p>";
      } else {
      	echo "There was an error...";
      }
?>
<div><h3>For your records.</h3> <p>You should also recieve an e-mail with all the information that you just filled out.</p> 
</div>
<?php
}
}


if(!isset($_POST['send']) || $errmsg != '')
{
?>
<div align="left" class="errmsg"><?=$errmsg;?></div>
<form id="msgform" name="msgform" method="post">
    <table id="ValidCheckbox4" width="620">
      <caption>
      <div style="font-style:normal; text-align: left;"><h3>Center Engagement Comm Develop</h3></div>
      </caption>
      <tbody>
      </tbody>
        <tr>
          <td valign="top">
          	<?php
//set file to read
$file = 'Computing_Telecom_Services.txt';

//open file
$fh = fopen($file, 'r') or die('Could not open file');

//read file contents
$data = fread($fh, filesize($file)) or die('Could not read file!');

//close file
fclose($fh);

//print file contents
echo $data;
?>
            </td>
        </tr>
    	</table>
    <input name="send" type="submit"  id="send" value="Send it">
    <!--<input type="submit" value="Send it"/>-->
<input type="reset" value="Reset"/>
</form>

<?php
}

function isEmail($email)
{
return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i"
		,$email));
}
?>
[code]

Link to comment
Share on other sites

How do I get the names to post to the e-mail?

 

Everything is working up to bringing in the names from an external file and adding them into the checkboxes. How can have them appear in the e-mail and page for the user to print out. I have tried filling in several variables. But nothing seems to bring in the names and allows them to say either yes or no.

 

Any suggestions???

 

<?php

$errmsg  = ''; // error message
$email = ''; // sender's email addres
$question = ''; // question one

if(isset($_POST['send']))
{
//$firstname=($_POST['firstname'])?"$firstname: Yes":"$firstname: No";
$lines=($_POST['firstname'])?'$line: Yes':'$line: No';
$email   = $_POST['email'];
$question = $_POST['question'];
$subject = 'Constituency Employee Verification';

$headers = "MIME-Version: 1.0\r\n";
$headers = "Content-type: text/html; charset=iso-8859-1\r\n";
$headers = "Content-Transfer-Encoding: 7bit\r\n";
$headers = "From: $email\r\n";

if(trim($email) == '')
{
	$errmsg = 'Please enter your email address';
}
else if(!isEmail($email))
{
	$errmsg = 'Your email address is not valid';
}

if($errmsg == '')
{
	if(get_magic_quotes_gpc())
	{
		$question = stripslashes($question);
	}	

	// the email will be sent here
	$to = "youremail@mail.com";
	// the email subject ( modify it as you wish )
	$subject = 'Results from Verification form' ;

	// the mail message ( add any additional information if you want )
	$msg = "Names = $firstname \n\nE-mail = $email \n\nAdditional Employees = $question \n\n " . $message;

	//mail($to, $subject, $msg, "From: $from\r\nReply-To: $from\r\nReturn-Path: $from\r\n");

	$mailsent = mail("$to,$email", $subject, $msg, $headers);
	if (mailsent) {
	echo "<h2>Thank you for filling out our Constituency Employee form!</h2>";
	echo "<h3>Your message has been sent!</h3>";
	echo "<p>Please print the following for your records:</p>";
	echo "<p><b>Subject:</b> $subject</p>";
	echo "<p><b>Names:</b><br>$firstname<br></p>";
	echo "<p><b>E-mail:</b><br>$email</p>";
	echo "<p><b>Additional Employees:</b> <br>";
	echo "$question</p>";
      } else {
      	echo "There was an error...";
      }
?>
<div><h3>For your records.</h3> <p>You should also recieve an e-mail with all the information that you just filled out.</p> 
<p>If you have any questions please contact <script language="JavaScript">
	var addr = 'foundation';
	var host = 'found.ksu.edu';
	var email = '<a href="mailto:' + addr + '@' + host + '">' + addr + '@' + host + '</a>';
	document.write(email);
	</script>.</p></div>
<?php
}
}


if(!isset($_POST['send']) || $errmsg != '')
{
//	echo 'Could not send';
?>

<div align="left" class="errmsg"><?=$errmsg;?></div>
<form id="msgform" name="msgform" method="post">
    <table id="ValidCheckbox4" width="620">
      <caption>
      <div style="font-style:normal; text-align: left;"><h3>Center Engagement Comm Develop</h3></div>
      </caption>
      <tbody>
      </tbody>
        <tr>
          <td valign="top">
<?php
$file = 'people.txt';
    $lines = file($file);

    foreach ($lines as $value => $line) {
       $firstname = strtok($line, ' ');
       echo "<input type='checkbox' name=\'{firstname}\' value=\'{$firstname}\'> $line<br />";
    }

?>
[code]

Link to comment
Share on other sites

Well here is try number three. Not sure this is a great practice of coding. But so far I have been on my own here.

 

This is the form processing code I have. It gives me back the last name in my external list. Or DB if you will.

 


<?php

$errmsg  = ''; // error message
$line = '';
$email = ''; // sender's email addres
$question = ''; // question one

if(isset($_POST['send']))
{
//$firstname=($_POST['firstname'])?"$firstname: Yes":"$firstname: No";
$file = 'people.txt';
    $lines = file($file);

    foreach ($lines as $value => $line) {
       $firstname = strtok($line, ' ');
   //echo "$line";
   //$line = ($_POST['name[]'])?"name[]: Yes":"name[]: No";
   if ($_POST['name[]']=='y'){
   		echo "$line: Yes\n";
   }
   else{
   		echo "$line: No\n";
   }
    }

$email = $_POST['email'];
$question = $_POST['question'];
$subject = 'Constituency Employee Verification';

$headers = "MIME-Version: 1.0\r\n";
$headers = "Content-type: text/html; charset=iso-8859-1\r\n";
$headers = "Content-Transfer-Encoding: 7bit\r\n";
$headers = "From: $email\r\n";

if(trim($email) == '')
{
	$errmsg = 'Please enter your email address';
}
else if(!isEmail($email))
{
	$errmsg = 'Your email address is not valid';
}

if($errmsg == '')
{
	if(get_magic_quotes_gpc())
	{
		$question = stripslashes($question);
	}	

	// the email will be sent here
	$to = "yourname@email.com";
	// the email subject ( modify it as you wish )
	$subject = 'Results from Academic Services Technology Verification form' ;

	// the mail message ( add any additional information if you want )
	$msg = "Names = $line \n\nE-mail = $email \n\nAdditional Employees = $question \n\n ";
	//$msg .="\n$firstname";
	//mail($to, $subject, $msg, "From: $from\r\nReply-To: $from\r\nReturn-Path: $from\r\n");

	$mailsent = mail("$to,$email", $subject, $msg, $headers);
	if (mailsent) {
	echo "<h2>Thank you for filling out our form!</h2>";
	echo "<h3>Your message has been sent!</h3>";
	echo "<p>Please print the following for your records:</p>";
	echo "<p><b>Subject:</b> $subject</p>";
	echo "<p><b>Names:</b><br>$line<br></p>";
	echo "<p><b>E-mail:</b><br>$email</p>";
	echo "<p><b>Additional Employees:</b><br>";
	echo "$question</p>";
      } else {
      	echo "There was an error...";
      }
?>

And here is what the dynamic checkbox code looks like.


$file = 'people.txt';
    $lines = file($file);

    foreach ($lines as $value => $line) {
       $firstname = strtok($line, ' ');
       echo "<input type='checkbox' name=\'names[]\' value=\'y\'> $line<br />\n";
    }

That part is ok.

 

But I can't get more than one name from the list to come back to my e-mail. I am needing the whole list to come back.

Is there anyone who can help?

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.