+ Reply to Thread
Results 1 to 7 of 7

Thread: PHP Linking?

  1. #1
    kim_foxx is offline x10Hosting Member kim_foxx is an unknown quantity at this point
    Join Date
    Apr 2010
    Posts
    31

    PHP Linking?

    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 Code:
    $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 Code:
    <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 by kim_foxx; 05-29-2010 at 01:02 AM.

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP Linking?

    What you want to do isn't clear. You can include one script (any file, really) in another using include, include_once, require or require_once.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    spadija's Avatar
    spadija is offline x10Hosting Member spadija is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    48

    Re: PHP Linking?

    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 Code:
    <?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 Code:
    <form action="/page/to/link/to.php" method="post">
    ...
    </form>

  4. #4
    kim_foxx is offline x10Hosting Member kim_foxx is an unknown quantity at this point
    Join Date
    Apr 2010
    Posts
    31

    Re: PHP Linking?

    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 Code:
    <?php
        session_start
    ();
        if (
    $_POST['form_submitted'] != '1') {
    ?>

    Then after some html i have

    PHP Code:

    <?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 by kim_foxx; 05-29-2010 at 11:48 PM.

  5. #5
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP Linking?

    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 Code:
    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 by misson; 05-30-2010 at 01:48 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  6. #6
    kim_foxx is offline x10Hosting Member kim_foxx is an unknown quantity at this point
    Join Date
    Apr 2010
    Posts
    31

    Re: PHP Linking?

    PHP Code:
     include('path/to/form/handler');
    } else {
        include(
    'path/to/form'); 
    Can the path/to simply link to a .html page?

  7. #7
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP Linking?

    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 Code:
    <?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 Code:
    <?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 Code:
    if (isset($_POST['comment']) {
        ...

    If you really want a special input for the test, use the submit button.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Linking
    By bluewhale in forum Free Hosting
    Replies: 2
    Last Post: 03-22-2010, 05:24 AM
  2. linking to outside?
    By EAGarcia in forum Graphics & Webdesign
    Replies: 4
    Last Post: 01-22-2008, 02:46 PM
  3. linking
    By jtaah75 in forum Free Hosting
    Replies: 1
    Last Post: 12-03-2007, 07:51 PM
  4. Oh the linking...
    By sikandar in forum Free Hosting
    Replies: 1
    Last Post: 09-02-2007, 02:23 AM
  5. id linking
    By yantrapark in forum Free Hosting
    Replies: 1
    Last Post: 08-10-2006, 02:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers