miloudi
asked on
Broken Code
Hello To all of you,
Pleas forgive me if i posted this in the wrong area, but i really need your help guys:
I setup a form to get info from users, display the info on the same page, email it to me and write it to a file:
Here is my code.
<?php
/************************* ********** ******
************************** ********** *****/
$submit = $_POST['submit'];
$emailAddress = $_POST['emailAddress'];
$comments = $_POST['comments'];
$PHP_SELF = $_SERVER['PHP_SELF'];
$name = $_POST['name'];
$ssn=$_POST['ssn'];
$contact_recipient_id = $_POST['contact_recipient_ id'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$option = $_POST['option'];
?>
<?php
if ($_SERVER['REQUEST_METHOD' ] != 'POST'){
$me = $_SERVER['PHP_SELF'];
?>
<form action="<?php echo $me;?>" method='post'>
<table class="info">
<tr><td> </td></tr><t r><td>&nbs p;</td></t r>
<tr><td>Title:</td><td><in put type="radio" name="title" value="Mr"/>Mr
<input type="radio" name="title" value="Mrs"/>Mrs
<input type="radio" name="title" value="Miss"/>Miss</td></t r>
<tr><td id="fn">*First Name:</td><td><input type="text" name="fName" /></td></tr>
<tr><td id="ln">*Last Name:</td><td><input type="text" name="lName" /></td></tr>
<tr><td id="ss">*Social Security Number:</td><td><input type="text" name="ssn" size="9" maxlength="11"/></td></tr>
<tr><td id="ag">*Age:</td><td><inp ut type="text" name="age" size="1" maxlength="3"/></td></tr>
<tr><td>Gender:</td><td><s elect name="gender">
<option value="male">M</option>
<option value="female">F</option>
</select></td></tr>
<tr><td id="ea">*Email Address:</td><td><input type="text" name="email" /></td></tr>
<tr><td id="pn">*Phone Number:</td><td><input type="text" name="phone" size="11" maxlength="13"/></td></tr>
<tr><td>Comments Or Questions:</td>
<td><textarea name="data" rows="4" cols="50"></textarea></td> </tr>
<tr><td><input type="submit" value="Submit" name="submit"/>
<input type="reset" value="Reset" name="reset" /></td></tr>
<tr><td> </td></tr>
</table>
</form>
<?php
} else {
error_reporting(0);
$recipient = 'php@hotmail.com';
$subject = 'Just to say Hi';
$from = stripslashes($_POST['fName ']);
$msg = "Message from: $from\n\n".stripslashes($_ POST['MsgB ody']);
if (mail($recipient, $subject, $msg))
echo nl2br("<b>Message Sent:</b>
To: $recipient
Subject: $subject
Message:
$msg");
else
echo "Message failed to send";
}
?>
<?php
// Open and write to a file
$myFile = "testfile.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$string = $name."\t"."\t".$ssn."\t". $email."\t ".$subject ."\t".$mes sage."\n";
fwrite($fh, $string);
fclose($fh);
?>
</body>
</html>
I don't understand how you can post and email the form at the same time. Originally what i did is i sent the data to another php form and then wrote from there to a file. But it is not the way it should be. Any help will be appreciated.
Also if i want to write to a file, i need to setup another from, don't i?
Thanks.
Pleas forgive me if i posted this in the wrong area, but i really need your help guys:
I setup a form to get info from users, display the info on the same page, email it to me and write it to a file:
Here is my code.
<?php
/*************************
**************************
$submit = $_POST['submit'];
$emailAddress = $_POST['emailAddress'];
$comments = $_POST['comments'];
$PHP_SELF = $_SERVER['PHP_SELF'];
$name = $_POST['name'];
$ssn=$_POST['ssn'];
$contact_recipient_id = $_POST['contact_recipient_
$subject = $_POST['subject'];
$message = $_POST['message'];
$option = $_POST['option'];
?>
<?php
if ($_SERVER['REQUEST_METHOD'
$me = $_SERVER['PHP_SELF'];
?>
<form action="<?php echo $me;?>" method='post'>
<table class="info">
<tr><td> </td></tr><t
<tr><td>Title:</td><td><in
<input type="radio" name="title" value="Mrs"/>Mrs
<input type="radio" name="title" value="Miss"/>Miss</td></t
<tr><td id="fn">*First Name:</td><td><input type="text" name="fName" /></td></tr>
<tr><td id="ln">*Last Name:</td><td><input type="text" name="lName" /></td></tr>
<tr><td id="ss">*Social Security Number:</td><td><input type="text" name="ssn" size="9" maxlength="11"/></td></tr>
<tr><td id="ag">*Age:</td><td><inp
<tr><td>Gender:</td><td><s
<option value="male">M</option>
<option value="female">F</option>
</select></td></tr>
<tr><td id="ea">*Email Address:</td><td><input type="text" name="email" /></td></tr>
<tr><td id="pn">*Phone Number:</td><td><input type="text" name="phone" size="11" maxlength="13"/></td></tr>
<tr><td>Comments Or Questions:</td>
<td><textarea name="data" rows="4" cols="50"></textarea></td>
<tr><td><input type="submit" value="Submit" name="submit"/>
<input type="reset" value="Reset" name="reset" /></td></tr>
<tr><td> </td></tr>
</table>
</form>
<?php
} else {
error_reporting(0);
$recipient = 'php@hotmail.com';
$subject = 'Just to say Hi';
$from = stripslashes($_POST['fName
$msg = "Message from: $from\n\n".stripslashes($_
if (mail($recipient, $subject, $msg))
echo nl2br("<b>Message Sent:</b>
To: $recipient
Subject: $subject
Message:
$msg");
else
echo "Message failed to send";
}
?>
<?php
// Open and write to a file
$myFile = "testfile.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$string = $name."\t"."\t".$ssn."\t".
fwrite($fh, $string);
fclose($fh);
?>
</body>
</html>
I don't understand how you can post and email the form at the same time. Originally what i did is i sent the data to another php form and then wrote from there to a file. But it is not the way it should be. Any help will be appreciated.
Also if i want to write to a file, i need to setup another from, don't i?
Thanks.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry, I should check my own code first before I submit... this works:
<?php
function showform() {
$s = ('<form action="'.$_SERVER["PHP_SE LF"].'?cmd =submitfor m" method="post">
<table class="info">
<tr><td> </td></tr><t r><td>&nbs p;</td></t r>
<tr><td>Title:</td><td><in put type="radio" name="title" value="Mr"/>Mr
<input type="radio" name="title" value="Mrs"/>Mrs
<input type="radio" name="title" value="Miss"/>Miss</td></t r>
<tr><td id="fn">*First Name:</td><td><input type="text" name="fName" /></td></tr>
<tr><td id="ln">*Last Name:</td><td><input type="text" name="lName" /></td></tr>
<tr><td id="ss">*Social Security Number:</td><td><input type="text" name="ssn" size="9" maxlength="11"/></td></tr>
<tr><td id="ag">*Age:</td><td><inp ut type="text" name="age" size="1" maxlength="3"/></td></tr>
<tr><td>Gender:</td><td><s elect name="gender">
<option value="male">M</option>
<option value="female">F</option>
</select></td></tr>
<tr><td id="ea">*Email Address:</td><td><input type="text" name="email" /></td></tr>
<tr><td id="pn">*Phone Number:</td><td><input type="text" name="phone" size="11" maxlength="13"/></td></tr>
<tr><td>Comments Or Questions:</td>
<td><textarea name="data" rows="4" cols="50"></textarea></td> </tr>
<tr><td><input type="submit" value="Submit" name="submit"/>
<input type="reset" value="Reset" name="reset" /></td></tr>
<tr><td> </td></tr>
</table>
</form>');
return $s;
}
function submitform() {
$s = '';
$submit = $_POST['submit'];
$emailAddress = $_POST['emailAddress'];
$comments = $_POST['comments'];
$PHP_SELF = $_SERVER['PHP_SELF'];
$name = $_POST['name'];
$ssn=$_POST['ssn'];
$contact_recipient_id = $_POST['contact_recipient_ id'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$option = $_POST['option'];
error_reporting(0);
$recipient = 'frank@vdmeulen.it';
$subject = 'Just to say Hi';
$from = stripslashes($_POST['fName ']);
$msg = "Message from: $from\n\n".stripslashes($_ POST['MsgB ody']);
if (mail($recipient, $subject, $msg))
$s .= nl2br("<b>Message Sent:</b>
To: $recipient
Subject: $subject
Message: $msg");
else
$s .= "Message failed to send";
// Open and write to a file
$myFile = "testfile.txt";
if ($fh = fopen($myFile, 'a') == false) {
$s .= "can't open file";
return $s;
} else {
$string = $name."\t"."\t".$ssn."\t". $email."\t ".$subject ."\t".$mes sage."\n";
fwrite($fh, $string);
fclose($fh);
}
return $s;
}
// This code gets executed every time your php page is called.
// Grap command/function to execute (default if not set or set with bogus value)
$command = (array_key_exists('cmd', $_REQUEST) ? $_REQUEST['cmd'] : 'showform');
if (in_array($command, array("showform", "submitform")))
$content = $command();
else
$content = showform();
// Dump output to screen
print $content;
?>
<?php
function showform() {
$s = ('<form action="'.$_SERVER["PHP_SE
<table class="info">
<tr><td> </td></tr><t
<tr><td>Title:</td><td><in
<input type="radio" name="title" value="Mrs"/>Mrs
<input type="radio" name="title" value="Miss"/>Miss</td></t
<tr><td id="fn">*First Name:</td><td><input type="text" name="fName" /></td></tr>
<tr><td id="ln">*Last Name:</td><td><input type="text" name="lName" /></td></tr>
<tr><td id="ss">*Social Security Number:</td><td><input type="text" name="ssn" size="9" maxlength="11"/></td></tr>
<tr><td id="ag">*Age:</td><td><inp
<tr><td>Gender:</td><td><s
<option value="male">M</option>
<option value="female">F</option>
</select></td></tr>
<tr><td id="ea">*Email Address:</td><td><input type="text" name="email" /></td></tr>
<tr><td id="pn">*Phone Number:</td><td><input type="text" name="phone" size="11" maxlength="13"/></td></tr>
<tr><td>Comments Or Questions:</td>
<td><textarea name="data" rows="4" cols="50"></textarea></td>
<tr><td><input type="submit" value="Submit" name="submit"/>
<input type="reset" value="Reset" name="reset" /></td></tr>
<tr><td> </td></tr>
</table>
</form>');
return $s;
}
function submitform() {
$s = '';
$submit = $_POST['submit'];
$emailAddress = $_POST['emailAddress'];
$comments = $_POST['comments'];
$PHP_SELF = $_SERVER['PHP_SELF'];
$name = $_POST['name'];
$ssn=$_POST['ssn'];
$contact_recipient_id = $_POST['contact_recipient_
$subject = $_POST['subject'];
$message = $_POST['message'];
$option = $_POST['option'];
error_reporting(0);
$recipient = 'frank@vdmeulen.it';
$subject = 'Just to say Hi';
$from = stripslashes($_POST['fName
$msg = "Message from: $from\n\n".stripslashes($_
if (mail($recipient, $subject, $msg))
$s .= nl2br("<b>Message Sent:</b>
To: $recipient
Subject: $subject
Message: $msg");
else
$s .= "Message failed to send";
// Open and write to a file
$myFile = "testfile.txt";
if ($fh = fopen($myFile, 'a') == false) {
$s .= "can't open file";
return $s;
} else {
$string = $name."\t"."\t".$ssn."\t".
fwrite($fh, $string);
fclose($fh);
}
return $s;
}
// This code gets executed every time your php page is called.
// Grap command/function to execute (default if not set or set with bogus value)
$command = (array_key_exists('cmd', $_REQUEST) ? $_REQUEST['cmd'] : 'showform');
if (in_array($command, array("showform", "submitform")))
$content = $command();
else
$content = showform();
// Dump output to screen
print $content;
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you guys.
I will check my code tonight and i will let you guys know. Please bear with me,, i am a network person and coding is very hard for me.
Tx again.
If you cut and paste my code in a new php document it will work 'out of the box'.
ASKER
Hello teRRef:
i did it on the localhost machine and it gave me errors. On alinux machine :
Here is the output:
Message Sent:
To: lznaidi@hotmail.com
Subject: Just to say Hi
Message: Message from: dolidol
can't open file
It doesn't display the information on the screen. I created the file manually on both machines. I am going to try to understand your code tonight:
Here is the final code that i got:
<?php
function showform() {
$s = ('<form action="'.$_SERVER["PHP_SE
return $s;
}
function submitform() {
$s = '';
$submit = $_POST['submit'];
$emailAddress = $_POST['emailAddress'];
$comments = $_POST['comments'];
$PHP_SELF = $_SERVER['PHP_SELF'];
$name = $_POST['name'];
$ssn=$_POST['ssn'];
$contact_recipient_id = $_POST['contact_recipient_
$subject = $_POST['subject'];
$message = $_POST['message'];
$option = $_POST['option'];
error_reporting(0);
$recipient = 'lznaidi@hotmail.com';
$subject = 'Just to say Hi';
$from = stripslashes($_POST['fName
$msg = "Message from: $from\n\n".stripslashes($_
if (mail($recipient, $subject, $msg))
$s .= nl2br("<b>Message Sent:</b>
To: $recipient
Subject: $subject
Message: $msg");
else
$s .= "Message failed to send";
// Open and write to a file
$myFile = "testfile.txt";
if ($fh = fopen($myFile, 'a') == false) {
$s .= "can't open file";
return $s;
} else {
$string = $name."\t"."\t".$ssn."\t".
fwrite($fh, $string);
fclose($fh);
}
return $s;
}
// This code gets executed every time your php page is called.
// Grap command/function to execute (default if not set or set with bogus value)
$command = (array_key_exists('cmd', $_REQUEST) ? $_REQUEST['cmd'] : 'showform');
if (in_array($command, array("showform", "submitform")))
$content = $command();
else
$content = showform();
// Dump output to screen
print $content;
?>
Thanks for taking the time to review the code...
ASKER
Dan82,
I have tried your code as well but it kinds of loops: back onto the same page:
<?php
//get the values from the form
$name = $_POST['name'];
$submit = $_POST['submit'];
$emailAddress = $_POST['emailAddress'];
$comments = $_POST['comments'];
$PHP_SELF = $_SERVER['PHP_SELF'];
$name = $_POST['name'];
$ssn=$_POST['ssn'];
$contact_recipient_id = $_POST['contact_recipient_ id'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$option = $_POST['option'];
//code inside this statement only runs when the form has been submitted
//hence it will not run the when the page first loads
if (isset($_POST['submit'])){
//Construct Email
$emailBody=<<<EOMAILBODY
Online submission query.
Name: $name
$comments
This message was generated by an online query form.
EOMAILBODY;
//address to send the mail to and subject
$subject = "On Line Form";
//send mail
mail ($emailAddress, $subject, $emailBody);
//WRITE to file
//havn't had time to check this code so its just a copy of yours, i'm assuming it works
// Open and write to a file
$myFile = "testfile.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$string = $name."\t"."\t".$ssn."\t". $email."\t ".$subject ."\t".$mes sage."\n";
fwrite($fh, $string);
fclose($fh);
//WE ARE STILL INSIDE if (isset($_POST['submit'])){
//now we could output the HTML here to display if the form has been submitted
// then use the exit statement to stop the rest of the page loading
//we first break from PHP mode back into HTML, and write the HTML to display when
//submitted. Then go back into PHP mode use the exit statement and close
//the if (isset($_POST['submit'])){ statement.
//This HTML will NOT be displayed unless the form has been submitted as even thou it
//is not printed in HTML mode it is still within the 'if' statement
?>
<html>
<body>
THIS IS DISPLAYED WHEN THE FORM HAS BEEN SUBMITTED
</body>
</html>
<?php
//now
exit;
}
?>
<html>
<table class="info">
<tr><td> </td></tr><t r><td>&nbs p;</td></t r>
<tr><td>Title:</td><td><in put type="radio" name="title" value="Mr"/>Mr
<input type="radio" name="title" value="Mrs"/>Mrs
<input type="radio" name="title" value="Miss"/>Miss</td></t r>
<tr><td id="fn">*First Name:</td><td><input type="text" name="fName" /></td></tr>
<tr><td id="ln">*Last Name:</td><td><input type="text" name="lName" /></td></tr>
<tr><td id="ss">*Social Security Number:</td><td><input type="text" name="ssn" size="9" maxlength="11"/></td></tr>
<tr><td id="ag">*Age:</td><td><inp ut type="text" name="age" size="1" maxlength="3"/></td></tr>
<tr><td>Gender:</td><td><s elect name="gender">
<option value="male">M</option>
<option value="female">F</option>
</select></td></tr>
<tr><td id="ea">*Email Address:</td><td><input type="text" name="email" /></td></tr>
<tr><td id="pn">*Phone Number:</td><td><input type="text" name="phone" size="11" maxlength="13"/></td></tr>
<tr><td>Comments Or Questions:</td>
<td><textarea name="data" rows="4" cols="50"></textarea></td> </tr>
<tr><td><input type="submit" value="Submit" name="submit"/>
<input type="reset" value="Reset" name="reset" /></td></tr>
<tr><td> </td></tr>
</table>
</html>
Thanks.
I have tried your code as well but it kinds of loops: back onto the same page:
<?php
//get the values from the form
$name = $_POST['name'];
$submit = $_POST['submit'];
$emailAddress = $_POST['emailAddress'];
$comments = $_POST['comments'];
$PHP_SELF = $_SERVER['PHP_SELF'];
$name = $_POST['name'];
$ssn=$_POST['ssn'];
$contact_recipient_id = $_POST['contact_recipient_
$subject = $_POST['subject'];
$message = $_POST['message'];
$option = $_POST['option'];
//code inside this statement only runs when the form has been submitted
//hence it will not run the when the page first loads
if (isset($_POST['submit'])){
//Construct Email
$emailBody=<<<EOMAILBODY
Online submission query.
Name: $name
$comments
This message was generated by an online query form.
EOMAILBODY;
//address to send the mail to and subject
$subject = "On Line Form";
//send mail
mail ($emailAddress, $subject, $emailBody);
//WRITE to file
//havn't had time to check this code so its just a copy of yours, i'm assuming it works
// Open and write to a file
$myFile = "testfile.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$string = $name."\t"."\t".$ssn."\t".
fwrite($fh, $string);
fclose($fh);
//WE ARE STILL INSIDE if (isset($_POST['submit'])){
//now we could output the HTML here to display if the form has been submitted
// then use the exit statement to stop the rest of the page loading
//we first break from PHP mode back into HTML, and write the HTML to display when
//submitted. Then go back into PHP mode use the exit statement and close
//the if (isset($_POST['submit'])){
//This HTML will NOT be displayed unless the form has been submitted as even thou it
//is not printed in HTML mode it is still within the 'if' statement
?>
<html>
<body>
THIS IS DISPLAYED WHEN THE FORM HAS BEEN SUBMITTED
</body>
</html>
<?php
//now
exit;
}
?>
<html>
<table class="info">
<tr><td> </td></tr><t
<tr><td>Title:</td><td><in
<input type="radio" name="title" value="Mrs"/>Mrs
<input type="radio" name="title" value="Miss"/>Miss</td></t
<tr><td id="fn">*First Name:</td><td><input type="text" name="fName" /></td></tr>
<tr><td id="ln">*Last Name:</td><td><input type="text" name="lName" /></td></tr>
<tr><td id="ss">*Social Security Number:</td><td><input type="text" name="ssn" size="9" maxlength="11"/></td></tr>
<tr><td id="ag">*Age:</td><td><inp
<tr><td>Gender:</td><td><s
<option value="male">M</option>
<option value="female">F</option>
</select></td></tr>
<tr><td id="ea">*Email Address:</td><td><input type="text" name="email" /></td></tr>
<tr><td id="pn">*Phone Number:</td><td><input type="text" name="phone" size="11" maxlength="13"/></td></tr>
<tr><td>Comments Or Questions:</td>
<td><textarea name="data" rows="4" cols="50"></textarea></td>
<tr><td><input type="submit" value="Submit" name="submit"/>
<input type="reset" value="Reset" name="reset" /></td></tr>
<tr><td> </td></tr>
</table>
</html>
Thanks.
Those are not 'error' messages, it is the string $s that is being printed. You could add the information you want to be displayed to $s.
One more tip, you get the "can't open file" message because you try to write to a directory where the webserver doesn't have that privilege.
Try to write to your temp directory (on your Linux PC this is probably /tmp):
$myFile = "/tmp/testfile.txt";
One more tip, you get the "can't open file" message because you try to write to a directory where the webserver doesn't have that privilege.
Try to write to your temp directory (on your Linux PC this is probably /tmp):
$myFile = "/tmp/testfile.txt";
I've clean up the code and placed it here
it will write to a file send the data as an email and redisplay the same form with the data filled in
http://www.dwaterhouse.f2s.com/1.~php
Note:
the values from the radio buttons are not redisplyed after the form has been submitted, but if this is the kind of think you want then i'll fix it
I'm also going to post a similar alternative in a minute.
it will write to a file send the data as an email and redisplay the same form with the data filled in
http://www.dwaterhouse.f2s.com/1.~php
Note:
the values from the radio buttons are not redisplyed after the form has been submitted, but if this is the kind of think you want then i'll fix it
I'm also going to post a similar alternative in a minute.
Alternative output:
Code
http://www.dwaterhouse.f2s.com/2~.php
Example:
http://www.dwaterhouse.f2s.com/2.php
Here is an example of the previous method
http://www.dwaterhouse.f2s.com/1.php
Same again this does not output all the values in your form but if its what you want that easilt fixed
PS on the example code the line that sends the email has been commented out (don't want a mailbox full of test data)
AND the lines which write the file have been commented out due to server permissions, i've not changed these lines just cut and pasted yours as them seem to work
Good Luck
Code
http://www.dwaterhouse.f2s.com/2~.php
Example:
http://www.dwaterhouse.f2s.com/2.php
Here is an example of the previous method
http://www.dwaterhouse.f2s.com/1.php
Same again this does not output all the values in your form but if its what you want that easilt fixed
PS on the example code the line that sends the email has been commented out (don't want a mailbox full of test data)
AND the lines which write the file have been commented out due to server permissions, i've not changed these lines just cut and pasted yours as them seem to work
Good Luck
ASKER
Duh,
You guys are right.
I will try to understand the code tonight and i will get back to you.
Thank you both again.....
You guys are right.
I will try to understand the code tonight and i will get back to you.
Thank you both again.....
ASKER
Dan82,
The example in this one
http://www.dwaterhouse.f2s.com/2.php
is the one i am looking for but th link for the code is not working.
Does it write to the file correctly because with the last code, it was just writing $subject and $email, i think.
Could you resubmit the link again, tx.
There is another issue:
If i want to read from the file and sort by ssn, how should i do it?
Thanks.
The example in this one
http://www.dwaterhouse.f2s.com/2.php
is the one i am looking for but th link for the code is not working.
Does it write to the file correctly because with the last code, it was just writing $subject and $email, i think.
Could you resubmit the link again, tx.
There is another issue:
If i want to read from the file and sort by ssn, how should i do it?
Thanks.
ASKER
TeRRef,
I changed the right s on that file, and i wasn't able to write to it?
Thanks.
I changed the right s on that file, and i wasn't able to write to it?
Thanks.
ASKER
Dan 82,
I was able to write correctly to the file but:
For the display of the form, i didn't get it yet(could you help).
also:
For reading and sorting from the file by SSN:
Do you think this will work:
I would like to setup a button on the same page for the user to be able to display the data while being sorted once the submit button is clicked. Is it doable?
Here is what i started:(could you help)
<?php
$err_cnt = 0;
$read_ctr = 1;
$lines = array();
@ $fp = fopen("testfile.txt", 'r'); //Open file for reading
if ($fp)
{
while (true)
{
$line = fgets($fp,200);
if (feof($fp))
{
break;
}
$fields = explode('*', $line);
$name_read = $fields[0];
$ssn_read = $fields[1];
$email_read = $fields[2];
$subject_read = $fields[3];
$message_read = $message[4];
$new_line = "$ssn_read.$name_read.$ema il_read.$s ubject_rea d.$message _read\n";
array_push($lines, $new_line);
$read_ctr++;
}
Thanks again
I was able to write correctly to the file but:
For the display of the form, i didn't get it yet(could you help).
also:
For reading and sorting from the file by SSN:
Do you think this will work:
I would like to setup a button on the same page for the user to be able to display the data while being sorted once the submit button is clicked. Is it doable?
Here is what i started:(could you help)
<?php
$err_cnt = 0;
$read_ctr = 1;
$lines = array();
@ $fp = fopen("testfile.txt", 'r'); //Open file for reading
if ($fp)
{
while (true)
{
$line = fgets($fp,200);
if (feof($fp))
{
break;
}
$fields = explode('*', $line);
$name_read = $fields[0];
$ssn_read = $fields[1];
$email_read = $fields[2];
$subject_read = $fields[3];
$message_read = $message[4];
$new_line = "$ssn_read.$name_read.$ema
array_push($lines, $new_line);
$read_ctr++;
}
Thanks again
ASKER
I am able to display the information entered by the user, write to a file and send an email.
Now the question is how can i read from that file and sort it by SSN.
I am trired and i am going to bed.
Thanks for the help...
Now the question is how can i read from that file and sort it by SSN.
I am trired and i am going to bed.
Thanks for the help...
Maybe you could put that in a new question?
Sorry the link is
http://www.dwaterhouse.f2s.com/2.~php
I'll look at it now and clean the code up, get it displaying all values from the form in the e-mail, then have a look at writing to a file, reading and sorting it.
What version of PHP are you using?
I've not done anything like that in PHP for a while and may consider an Object Orientated approach, so may need 5 plus. I'll only do it for PHP5 if that what your running and it has performance benifits.
i'll post the cleaned and improved code to http://www.dwaterhouse.f2s.com/2-2.~php when finished.
and place the example at http://www.dwaterhouse.f2s.com/2-2.php
Good Luck
http://www.dwaterhouse.f2s.com/2.~php
I'll look at it now and clean the code up, get it displaying all values from the form in the e-mail, then have a look at writing to a file, reading and sorting it.
What version of PHP are you using?
I've not done anything like that in PHP for a while and may consider an Object Orientated approach, so may need 5 plus. I'll only do it for PHP5 if that what your running and it has performance benifits.
i'll post the cleaned and improved code to http://www.dwaterhouse.f2s.com/2-2.~php when finished.
and place the example at http://www.dwaterhouse.f2s.com/2-2.php
Good Luck
Ok i've posted an updated version (no changes to file read/write) thou
exmaple: http://www.dwaterhouse.f2s.com/2-2.php
code: http://www.dwaterhouse.f2s.com/2-2.~php
Same again you need to uncomment line 30-37 approx, these are the lines that submit the email and write the file, won't work with these in where i've hosted the example because of permissions.
You also need to edit the mail address the form is sent to on line 29.
exmaple: http://www.dwaterhouse.f2s.com/2-2.php
code: http://www.dwaterhouse.f2s.com/2-2.~php
Same again you need to uncomment line 30-37 approx, these are the lines that submit the email and write the file, won't work with these in where i've hosted the example because of permissions.
You also need to edit the mail address the form is sent to on line 29.
ASKER
Hi:
How about i will add an additional 100 for the secodn part(array).
I don't think i need to post another question for it.
Thanks.
I want to help you, so I looked at Dan_82's code to extent it to add the functionallity since you seem to be going with his code. Sorry, but I find the code messy and not the right way to go in my opinion. It is MUCH better to use functions. Because:
- You code is clearer
- Much easier to extend
- It prevents code repetition
Of course it is up to you and if you use the current code, I'm sure Dan_82 will be more than happy to help you further.
Happy coding!
- You code is clearer
- Much easier to extend
- It prevents code repetition
Of course it is up to you and if you use the current code, I'm sure Dan_82 will be more than happy to help you further.
Happy coding!
Fair point by TeRReF about use off function, but code was just ment to be an example and something to work from not to cut 'n' paste and be 100% relied upon, any way i've put into basic functions, you may want to split it down further into a function to send the email one to write the file and one to display the info instead of one which does all 3 after the form is submitted, but you will have to address the issue of accessing variable outside these functions, or place repetative code within them.
new code is here
http://www.dwaterhouse.f2s.com/2-3.~php
example
http://www.dwaterhouse.f2s.com/2-3.php
Same again uncomment lines 27-33
new code is here
http://www.dwaterhouse.f2s.com/2-3.~php
example
http://www.dwaterhouse.f2s.com/2-3.php
Same again uncomment lines 27-33
ASKER
I really appreciate bot the quick response and the help but teRRef, the reason i went with Dan82 is because i was able to understand the logic. I respect your cuys responses.
But i still have the question of readig from the file and sorting by SSN.
Do you have any ideas how i can do that ?
Thanks again.
PS: And Dan i am using php5.1.2 on the local machine.
I can increase the points if you want....
But i still have the question of readig from the file and sorting by SSN.
Do you have any ideas how i can do that ?
Thanks again.
PS: And Dan i am using php5.1.2 on the local machine.
I can increase the points if you want....
You should not use while(true), chances are you'll end up in an indefinate loop!
<?php
$lines = array();
$read_ctr = 0;
$handle = @fopen("testfile.txt", "r");
if ($handle) {
while (!feof($handle)) {
$line = fgets($handle, 4096);
$fields = explode('*', $line);
$lines[] = $fields[1].$fields[0].$fie lds[2].$fi elds[3].$f ields[4];
$read_ctr++;
}
fclose($handle);
}
// this is a 'debug' line, comment out when everything works!
print_r($lines);
?>
Hope this helps.
<?php
$lines = array();
$read_ctr = 0;
$handle = @fopen("testfile.txt", "r");
if ($handle) {
while (!feof($handle)) {
$line = fgets($handle, 4096);
$fields = explode('*', $line);
$lines[] = $fields[1].$fields[0].$fie
$read_ctr++;
}
fclose($handle);
}
// this is a 'debug' line, comment out when everything works!
print_r($lines);
?>
Hope this helps.
I've not test TeRRef code above but it looks v.good and the method is v.similar to the one i'd use mainly the use of 'explode'.
The Object Orientated approach i said i'd consider before, is not the way to go for this, as the above example is much easier to read and understand.
Don't forget i've not really alter the line that writes the variables to the file, mainly because i didn't know how you wanted the name formatting e.g. did you want
$title $fName $lName
OR
$title.'*'.$fName.'*'.$lNa me
You also need to add age, gender, etc to the output
The Object Orientated approach i said i'd consider before, is not the way to go for this, as the above example is much easier to read and understand.
Don't forget i've not really alter the line that writes the variables to the file, mainly because i didn't know how you wanted the name formatting e.g. did you want
$title $fName $lName
OR
$title.'*'.$fName.'*'.$lNa
You also need to add age, gender, etc to the output
ASKER
TeRRef,
Thanks for the array and i am able to understand what it does.
Dan82, The output looks fine.
My question is that from that same form, can i call another from and then setup a button to call the array to read from the file....
In other words:
Create another form post2.php
and from my main form do:
<form method="post" action="http://localhost/post2.php" name="view">
<input type="submit" name="view" value="View file">
</form>
and then setup the array inside post2.php...
But i don't think you can have two forms definition? What do you guys think?
Thanks.
Thanks for the array and i am able to understand what it does.
Dan82, The output looks fine.
My question is that from that same form, can i call another from and then setup a button to call the array to read from the file....
In other words:
Create another form post2.php
and from my main form do:
<form method="post" action="http://localhost/post2.php" name="view">
<input type="submit" name="view" value="View file">
</form>
and then setup the array inside post2.php...
But i don't think you can have two forms definition? What do you guys think?
Thanks.
ASKER
Ok guys,
I think i figured out a way for displaying the button and jumping to a form by including this:
<?php
Name: $fname<br/>
Last Name: $lname <br/>
SSN: $ssn <br/>
Age: $age <br/>
Gender: $gender</br>
E-Mail: $email<br/>
Phone: $phone<br/>
";
echo '<form method="post" action="post2.php" name="goBack">
<input type="submit" name="view" value="View">
</form>
';
?>
in post2.php
i have what teRRef wrote:
<?php
$lines = array();
$read_ctr = 0;
$handle = @fopen("testfile.txt", "r");
if ($handle) {
while (!feof($handle)) {
$line = fgets($handle, 4096);
$fields = explode('*', $line);
$lines[] = $fields[4].$fields[0].$fie lds[2].$fi elds[3].$f ields[4];
//$fields[4] = $ssn
$read_ctr++;
}
fclose($handle);
}
// this is a 'debug' line, comment out when everything works!
print_r($lines);
?>
but here is my output:
Data captured:
Array ( [0] => male Mr rabbit rabbit 18 6123316775 lznaidi@hotmail.com [1] => ) ?>
any final hints.....
Thanks.....
I think i figured out a way for displaying the button and jumping to a form by including this:
<?php
Name: $fname<br/>
Last Name: $lname <br/>
SSN: $ssn <br/>
Age: $age <br/>
Gender: $gender</br>
E-Mail: $email<br/>
Phone: $phone<br/>
";
echo '<form method="post" action="post2.php" name="goBack">
<input type="submit" name="view" value="View">
</form>
';
?>
in post2.php
i have what teRRef wrote:
<?php
$lines = array();
$read_ctr = 0;
$handle = @fopen("testfile.txt", "r");
if ($handle) {
while (!feof($handle)) {
$line = fgets($handle, 4096);
$fields = explode('*', $line);
$lines[] = $fields[4].$fields[0].$fie
//$fields[4] = $ssn
$read_ctr++;
}
fclose($handle);
}
// this is a 'debug' line, comment out when everything works!
print_r($lines);
?>
but here is my output:
Data captured:
Array ( [0] => male Mr rabbit rabbit 18 6123316775 lznaidi@hotmail.com [1] => ) ?>
any final hints.....
Thanks.....
ASKER
Hi,
It seems like that the array function is just reading from the beginning of the file:
from post2.php
<?php
$lines = array();
$read_ctr = 0;
$handle = @fopen("test.txt", "r");
if ($handle) {
while (!feof($handle)) {
$line = fgets($handle, 4096);
$fields = explode('*', $line);
$gender = $fields[0];
$title = $fields[1];
$fname = $fields[2];
$lname = $fields[3];
$ssn = $fields[4];
$age = $fields[5];
$phone = $fields[6];
$email = $fields[7];
$lines[] = $fields[4].$fields[0].$fie
$read_ctr++;
}
fclose($handle);
}
// this is a 'debug' line, comment out when everything works!
print_r($lines);
I had the $fields match how the data are written to the file..
I mean this line:
$string = $gender."\t".$title."\t".$
I am missing a small detail ....i guess
ASKER
Hi,
I run out of points guys. Otherwise, i would have added some extra stuff.
Thanks much for all the help..
...............
It's not about the point, glad to help.
the form action is the page itself, so it's the same php file that does the job.
the difference between the 'is the page posted or not' is in this line:
if ($_SERVER['REQUEST_METHOD'
Hope this helps to understand