PHP Linking?

kim_foxx

Member
Messages
84
Reaction score
0
Points
6
Im using a simple mail script as a template but instead of echoing the message i want it to link to another page so i can keep my layout and css

The code at the moment which i want to change is
PHP:
$to      = 'dadadada@hotmail.com';
$subject = "Message from " . $_POST['name'];
$message = $_POST['comment'];
$headers = "From: " . $_POST['email'] . "\r\n" .
    "Reply-To: ".$_POST['name'] . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
echo "Your comment has been sent! Thanks!";
}


and the form:

HTML:
<form method="post">

    <p>Name:</p>
    <p><input type="text" name="name"></p>
    <p>Email Address:</p>
    <p><input type="text" name="email"></p>
    <p>Comment/Suggestion:</p>
    <p><textarea cols="40" name="comment"></textarea></p>

    <p><img src="captcha.php"/></p>
    <p>lease enter the image text:</p>

        <p><input type="text" name="code">

      
      <input type="submit" value="Submit Form" />
      <input type="hidden" name="form_submitted" value="1"/>
      </form></p>
 
Last edited:

spadija

New Member
Messages
48
Reaction score
1
Points
0
I'm not entirely clear on what you are trying to do or how you're trying to do it, but you could replace the echo with outputting your page like this.
PHP:
<?php
$to      = 'dadadada@hotmail.com'; 
$subject = "Message from " . $_POST['name']; 
$message = $_POST['comment']; 
$headers = "From: " . $_POST['email'] . "\r\n" . 
    "Reply-To: ".$_POST['name'] . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 
mail($to, $subject, $message, $headers); 
?>
<html>
    <!-- page goes here -->
</html>
<?php
}

A better way would probably be to set the "action" attribute of the form tag so that clicking submit sends you to another page, then adding the mailing script into this page.
HTML:
<form action="/page/to/link/to.php" method="post">
...
</form>
 

kim_foxx

Member
Messages
84
Reaction score
0
Points
6
The form action is what i would like to do but would it be better to have it all on the same page since the captcha needs to be validated?

If i do it the <html> way i need to copy and paste the whole page again inbetween the tags. It works but it just get messy so wondering if there is a cleaner of to do it.

Basically what i want is a form and then when they press submit they either get a message about incorrect capctha or one with mail has been sent successfully while keeping the website layout because if echo is used the way it is below there is no webpage layout.

At the very start of the page i have
PHP:
<?php
    session_start();
    if ($_POST['form_submitted'] != '1') {
?>


Then after some html i have

PHP:
<?php } else if ($_POST[form_submitted] == 1) { ?>

<?php
//Encrypt the posted code field and then compare with the stored key

if(md5($_POST['code']) != $_SESSION['key'])

{
  
  
  echo "It seems you entered an invalid Captcha key. Please go back and try again.";

}else{
session_unset();
session_destroy();
// Send

$to      = 'xxx@hotmail.com';
$subject = "Get-Stepping - " . $_POST['name'];

$message = $_POST['message'];
$headers = "From: " . $_POST['email'] . "\r\n" .

    "Reply-To: ".$_POST['name'] . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

echo "Your comment has been sent! Thanks!";

}
?>
<?php }  

?>
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
The best thing is to use include or include_once, as I mentioned earlier. If the form validation fails (including captcha validation), include the form, having it display which fields failed and filling the default input values with the values the user entered. If the validation succeeds, include the form handler.

PHP:
if (($errors = validate($_POST, <field validation information>)) {
    include('path/to/form/handler');
} else {
    include('path/to/form');
    // either have the form consult the global "$errors" to mark 
    // which fields failed validation, or do something like:
    printForm($errors);
}
 
Last edited:

kim_foxx

Member
Messages
84
Reaction score
0
Points
6
PHP:
 include('path/to/form/handler');
} else {
    include('path/to/form');

Can the path/to simply link to a .html page?
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Yes, but in that case it's better to use readfile().

In this case, I recommend turning the form into a PHP script to handle default values and error message display. For example,

PHP:
<?php
// includes scripts that define class TextInput, class TextareaInput &c.
include('FormInputs.php');
// defines class Form
include('Form.php');

$form = new Form();
$form->add(new TextInput('name', 'Name'));
$form->add(new TextInput('email', 'Email Address'));
$form->add(new TextareaInput('comment', 'Comments'));
...
$form->add(new SubmitButton('Submit Comment'));
echo $form;
or:
PHP:
<?php
$types = array(
  'checkbox' => 'input type="checkbox"',
  'hidden' => 'input type="hidden"',
  ...
  'text' => 'input',
  'textarea' => 'textarea'
);
$inputs = array(
  'name' => array('label' => 'Name', 'value' => ''),
  'email' => array('label' => 'Email Address', 'value' => ''),
  ...
  'submit' => array('type' => 'submit', 'value' => 'submit comment')
);
?><form ...>
  <?php foreach ($inputs as $name => $info) { ?>
    <?php if (isset($info['label'])) { ?>
      <label for="<?php echo $name ?>"><?php echo $info['label'] ?>:</label>
    <?php } ?>
    <<?php if (isset($info['type'])) {echo " type='$info[type]'";} ?> name="<?php echo $name" value="<?php echo isset($_REQUEST[$name]) ? $_REQUEST[$name] : $info['value'] ?>" />
    <span id="<?php echo $name, '_err' ?>" class="<?php if (!isset($errors[$name])) {echo 'hidden'} ?>">
      <?php if (isset($errors[$name]) { echo $errors[$name] } ?>
    </span>
  <?php } ?>
</form>

Note that you don't need a hidden form_submitted input. You can simply test whether a certain input from the form is defined:
PHP:
if (isset($_POST['comment']) {
    ...
}
If you really want a special input for the test, use the submit button.
 
Top