Jump to content

Recommended Posts

So I'm making my first real web site and I have decided to use a lot of PHP. Most of it is great and works flawlessly. But this one last script I have been messing with for days and still can't get it to work fully.

 

What I am trying to do here is have a sort of in page text editor, but just to let me open the contents of one file and save them to another. I have a file that gets lines put into by another script that submits the contents of a form into a single line with custom tags separating the parts. Specifically, in the format of "Quote the user submitted<name>username<email>email address" and I have very similar code in another script to cut out each part I want and display it, and that script works great. but in the new script, I am running these things many times, so a set of functions seemed appropriate. Later, I would even like to separate them into another file and include them where I need them.

 

In short, the script gets the lines I submitted on the previous page, writes them to the end of the new file, and then I want it to send a conformation email to all the authors and display the submitted quotes back to the page for me to see and double check. The reading and writing to the file part works, the mailing and displaying part doesn't. I suspect there is something wrong with my functions or the way I am calling them, because they seem to output nothing. I even have failed email attempts in my inbox that have my layout, but no info, or email address.

 

This is PHP 5 on I believe a Linux server. I don't know much about the environment, it is some hosting the owner is letting me have for this site.

 

Here is the script:

<?php
//Load lines submitted to script
$lines = $_POST["accept"];

//open file to save to
$file = "fakefact.txt";
$fp=fopen($file, 'a');
//append the new lines to the end
fwrite($fp, $lines);
fclose($fp);

//record the time and date to display later
$todayis = date("l, F j, Y, g:i a") ;

//this just cuts the fact from the line
function get_fact($quote){
//Find where the fact ends
$stop=strpos($quote,"<name>");
//cut out the fact
$fact=substr($quote,0,$stop);
$fact="\"".$fact."\"";
return $fact;
}

//this cuts the author name from the line
function get_name($quote){
//find where name starts and stops
$start=strpos($quote,"<name>");
$stop=strpos($quote,"<email>");
$stop=$stop-$start;
//cut out name and remove tag
$name=substr($quote,$start,$stop);
$name=str_replace("<name>","",$name);
return $name;
}

//this cuts the email address from the line
function get_email($quote){
//find email tag
$email=strstr($quote,"<email>");
//remove tag
$email=str_replace("<email>","",$email);
return $email;
}

//remove everything to make plaintext
function remove_newlines($quote) {
$quote=strip_tags($quote);
$quote=str_replace("\n","",$quote);
$quote=str_replace("\r","",$quote);
$quote=stripslashes($quote);
return $quote;
}

//loop through each line and send an email to the author
for($i = 0; $i < count($lines); $i++) {

//get the name and clean it
$name=get_name($lines[$i]);
$name=remove_newlines($name);

//get the fact and clean it
$fact=get_fact($lines[$i]);
$fact=remove_newlines($fact);

//get the email and clean it
get_email($lines[$i]);
$email=remove_newlines($email);

//write the message body
$message="Congratulations! Your fact has been accepted to TamedRacingDriver.com, please take a look for yourself.\n
Name : ".$name."\n
Fact : ".$fact."\n
See if at http://www.tamedracingdriver.com/fakefact.php \n
";

//write the headers and subject line
$headers="From: TamedRacingDriver.com<admin@tamedracingdriver.com>";
$subject="Fact accepted to TamedRacingDriver.com!";
//send the email
mail($email, $subject, $message, $headers);
}
?>

<h2>Facts added!</h2>

<p>
<b>Date:</b> <?php echo $todayis ?>
<br />

<b>The following facts were added:</b><br />
<?php
//loop each line and output the facts
for($i = 0; $i < count($lines); $i++) {
$fact=get_fact($lines[$i]);
$fact=remove_newlines($fact);
echo $fact;
} ?>

<br><br><b>Emails have been sent to inform the submitters.</b>
<br><br>
<a href="index.php">Done</a>

Link to comment
https://forums.phpfreaks.com/topic/84959-solved-broken-functions-no-output/
Share on other sites

<?
$lines = $_POST["accept"];

Your HTML did not send a input named "accept".

 

You must refer to each of them by name.

 

<input type="text" name="name" size=25> --- $_POST["name"];

<input type="text" name="email" size=25> --- $_POST["email"];

<textarea name="fact" rows="3" cols="40"></textarea> --- $_POST["fact"];

Nope, I have code like that where the user puts in that data, but here it is already put into lines that are all in one textarea. So 'accept' has all the lines that I am confirming are good in one array. I can verify this because it appends the other file, "fakefact.txt" with all those lines as it should.

Finally I figured out what was going wrong. When I read from a file, it makes an array with each line being the next slot in it. BUT when I read from $_POST, it just makes one big string. So I should just need to explode it after I write it to the file, so the rest of the script will work.

 

I guess that is the double edged sword of not declaring variables, your not always sure how they will be made.

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.