baig772
baig772

Reputation: 3498

how to concatenate a message

I am submitting a form which first enteres the data into db and then composes a message coming from the form fields. I want the functionality that if a user resubmits the form (2 or 3 times), there should be three messages composed in the same variable?

below is how i am creating my message variable

if(isset($_REQUEST['save'])){
$mesg = "<strong>Logging Off at </strong> " . date("d F Y h:i:s A");
$mesg .= "<BR><BR>";
$mesg .= "<strong>Project</strong>: " . $prj_name;
$mesg .= "<BR>";
$mesg .= "<strong>Deliverable</strong>: ". $dlvrbl_name;


$mesg .= "<BR>";
$mesg .= "<strong>Time spent</strong>: " . $time_spent;
$mesg .= "<BR>";
$mesg .= "<strong>Percentage</strong>: " . $percent . "%";
$mesg .= "<BR>";
$mesg .= "<strong>Comments</strong>: " . $comments;
$mesg .= "<BR><BR>";
$mesg .= "Regards,<BR>".$employee;
}

its working fine for the 1 time form submission, but what if i need 2 or 3 messages in the same variable? thanks in advance

Upvotes: 0

Views: 2616

Answers (2)

Kermit
Kermit

Reputation: 34062

How about using an array using a counter?

$i = 0;
$mesg[$i] = "<strong>Logging Off at </strong> " . date("d F Y h:i:s A");
$mesg[$i] .= "<BR><BR>";
$mesg[$i] .= ...

Then for the next message:

$i++;
$mesg[$i] = "<strong>Logging Off at </strong> " . date("d F Y h:i:s A");
$mesg[$i] .= "<BR><BR>";
$mesg[$i] .= ...

When you're done, you can use implode or manipulate it how you would like.

Also, I would recommend you use <br /> instead of <br>, and depending on the application, all you may need is a new line \n.

Upvotes: 1

nathan
nathan

Reputation: 5506

You can use a session variable, for example $_SESSION['log']:

session_start();

// ... 

if (isset($_REQUEST['save'])) {
    $message = isset($_SESSION['log']) ? $_SESSION['log'] . '<br>' : '';
    $message .= "<strong>Logging Off at </strong> " . date("d F Y h:i:s A");
    // ...
    $_SESSION['log'] = $message;
}

Upvotes: 1

Related Questions