SitePoint Sponsor

User Tag List

Results 1 to 25 of 25
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Embedding variables

    Hi all

    I have managed to pull some items from a database and create links on that page. The user can select a product link then when they click on a link they are show the product on the next page. On that page I have an email form. I would like to embed the product name into the message of the email or have it as a hidden field but still embedded into the message body of the email so it displays in the message when the recipent sees it. How can I put the below variable which displays the product name into the message body along with any message the user rights into the textarea?

    $row['ProductName']

    The only parameters you can have are

    mail (to, subject, body, headers)

    $message = $_REQUEST['message'] ;

    the above requests the message the user writes in the text area but I am not sure how to embed $row['ProductName'] into this?

    Hope this makes sense. Still a novice to php so any help would be great thanks

    Rob

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    1 put the product name in a hidden form field
    PHP Code:
    echo '<form method=POST>' .  PHP_EOL ;
    echo 
    '<input type=hidden id=product_name value = "' 
              
    $row['product_name']. '" >' 
    2 Do some kind of validation on that value, or cleanse it then add it to your message
    PHP Code:
    $product_name cleansed$_POST['product_name']) ;

    //Concatenate the values;

    $message $_REQUEST['message'
                  . 
    ' This user was commenting on ' $product_name 
    Line breaks added for readability

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi thanks for that info

    Sorry I have another query, below is some asp its an if statement and it says it redirects the user to the previous page if they click the cancel button. How would I do the same in php? I seem to have problems with php page redirects.

    if(Request.Form("Cancel")<>"") then
    response.Redirect("send.asp")
    end if

    <input type="Submit" value="Cancel" name="Cancel" class="button" />

    Thanks

    Rob

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The PHP equiv is
    PHP Code:
    header('Location: ' $place ) ; 
    nb Make sure absolutely nothing is output to the browser before that header line, or you will get a nasty warning.
    header

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you

    sorry probably going to sound dumb does that not re-direct the user to another page the minute they load that page? How do I link that in with the form so if they hit the button 'cancel' it takes them to that re-direct page?

    Thanks

    Rob

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I meant that you would detect the Cancel button being pressed, then use the header, which was missing a dot in my example btw.
    PHP Code:
    <?php
    // see what PHP thinks your form is sending
    var_dump$_POST );

    if( 
    $_POST['submit'] === "Cancel" ){
    header'Location:' .  $place );
    exit();
    }
    I think it is something like that, I never try catching which submit is pressed as it is not very reliable ( users can hit the 'enter' key ).

  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks again

    So is the below correct

    <?php
    // see what PHP thinks your form is sending
    var_dump( $_POST );

    if( $_POST['submit'] == "Cancel" ){
    header( 'Location:' . $gift.php );
    exit();
    }
    ?>

    The above code is held at the top of the page as you said, my email form is set up as below. For some reason I still cannot get the cancel button to take me to the gift.php page.

    PLease see form below

    <?php
    function spamcheck($field)
    {
    //filter_var() sanitizes the e-mail
    //address using FILTER_SANITIZE_EMAIL
    $field=filter_var($field, FILTER_SANITIZE_EMAIL);

    //filter_var() validates the e-mail
    //address using FILTER_VALIDATE_EMAIL
    if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return TRUE;
    }
    else
    {
    return FALSE;
    }
    }

    if (isset($_REQUEST['email']))
    {//if "email" is filled out, proceed

    //check if the email address is invalid
    $mailcheck = spamcheck($_REQUEST['email']);
    if ($mailcheck==FALSE)
    {
    echo "You did not fill out your email address correctly!";
    echo "<form method='post' action='contact.php'>";
    echo "<div>";
    echo "<p><label for='Email Address'>Email/label><input name='email' type='text' /></p>";
    echo "</div>";
    echo "<div>";
    echo "<p><label for='Name'>Product/label><input name='subject' type='text' size='50' value='" . 'ID(' . $row['ID'] . ')' . '&nbsp;' . $row['ProductName'] . '&nbsp;' . $row['BrandName'] . "' READONLY /></p>";
    echo "</div>";
    echo "<div>";
    echo "<p><label for='Comments'>Message/label>";
    echo "<textarea name='message' rows='15' cols='40' >";
    echo "</textarea></p>";
    echo "</div>";
    echo "<div class='submit'>";
    echo "<input type='submit' value='Submit' /><input type='submit' value='Cancel' />";
    echo "</div>";
    echo "</form>";


    }
    else
    {//send email
    $email = $_REQUEST['email'] ;
    $subject = $_REQUEST['subject'] ;
    $message = $_REQUEST['message'];
    mail("rob@hotmail.com", "Product: $subject",
    $message, "From: $email" );
    echo "<p><h3>Thank you for contacting us we will get back to you as soon as possible.</h3></p>";

    }
    }
    else
    {//if "email" is not filled out, display the form
    echo "<form method='post' action='contact.php'>";
    echo "<div>";
    echo "<p><label for='Email Address'>Email/label><input name='email' type='text' /></p>";
    echo "</div>";
    echo "<div>";
    echo "<p><label for='Name'>Product/label><input name='subject' type='text' size='50' value='" . 'ID(' . $row['ID'] . ')' . '&nbsp;' . $row['ProductName'] . '&nbsp;' . $row['BrandName'] . "' READONLY /></p>";
    echo "</div>";
    echo "<div>";
    echo "<p><label for='Comments'>Message/label>";
    echo "<textarea name='message' rows='15' cols='40' >";
    echo "</textarea></p>";
    echo "</div>";
    echo "<div class='submit'>";
    echo "<input type='submit' value='Submit' /><input type='submit' value='Cancel' />";
    echo "</div>";
    echo "</form>";

    }





    ?>

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    header'Location:' $gift.php ); 
    That line is wrong.

    Either;
    PHP Code:
    header'Location: gift.php' ); 
    Or
    PHP Code:
    $file 'gift.php' ;

    header'Location:' $file ); 
    Assuming of course that gift.php is in the same folder as the rest of this stuff.

    You would make life easier for yourself if, when you face a problem you create a mini version of it and test it all out instead of trying to get so many things working at the same time.
    PHP Code:
    <?php
    // see whats going on
    var_dump($_POST);
    echo 
    '<hr />' PHP_EOL ;

    if( 
    $_POST['submit'] == "Cancel" )
    echo 
    'REDIRECTING, bye!' ;

    ?>
    <form action = "" method=POST>
    <input type="submit" value="Cancel" />
    </form>
    Nobody wants to read through all your div tags to work out what is wrong with a form element, or just your typo on header(Location).

    Can you wrap code in PHP tags too? [ php ] code [ /php ] (remove spaces when you do it)
    Divide and conquer.

    HTH

  9. #9
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks again for your help, sorry I am a newbie when it comes to this and should as you say try it in little chunks.

    Thanks for the help above I will give that a try.

    Cheers

    Rob

  10. #10
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi I put the code into a simple page and created a second page for the cancel but to redirect to.

    So here is test.php

    <?php $file = 'post.php' ;
    echo '<hr />' . PHP_EOL ;
    if( $_POST['submit'] == "Cancel" )
    //echo 'REDIRECTING, bye!' ;
    header( 'Location:' . $file );
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>


    <form action="test.php" method="post">
    <input type="submit" value="Cancel" />
    </form>

    </body>
    </html>


    Here is post.php

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>

    Hello world!!

    </body>
    </html>

    If still does not seem to want to work.

    Sorry to be a pain

    Thanks

    Rob

  11. #11
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <input type="submit" name="submit" value="Cancel" />
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  12. #12
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks to you both I just got that to work and thanks so much to cups especially for your patience

    Many thanks

    Rob

  13. #13
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Two quick things:

    1. You should put:

      PHP Code:
      exit(); 
      After your header redirect. Otherwise it will continue parsing the rest of the file, which could be bad depending what else is there. Of course make sure you wrap that if block in braces before doing that.
    2. The URL you put in the Location header should be a full, absolute URL. Browsers aren't guaranteed to support relative URLs, as you have now.

  14. #14
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks I will put that in.

    Cheers

    Rob

  15. #15
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi I have yet another query

    How to I put a line break in this code so there is a line break after the Product before the message?

    $message = $_REQUEST['product'] . $_REQUEST['message'] ;


    If I put it in like below it just prints the <br /> in with the text.

    $message = $_REQUEST['product'] . "<br />" . $_REQUEST['message'] ;

    Many thanks again

    Rob

  16. #16
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
     $message $_REQUEST['product'] . "<br />\r\n" $_REQUEST['message'] ; 
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  17. #17
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the quick response, that made it drop to the next line but for some reason still display the <br /> and it displayed it twice so <br /><br />

    Any ideas?

  18. #18
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry ignore me on that one, that was me just being stupid, that worked great!!

    Thanks again you have been a great help, sorry this must be pretty annoying for you.

  19. #19
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    No, not at all. Just HTH.

    Glad it worked out for you, don't be stranger.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  20. #20
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent cheers :-)

  21. #21
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi SilverBulletUK

    I have created a login form for my website and the session works fine. I just want to create a logout hyperlink that will kill the session. I am having trouble pinning this down on the web any ideas?

    Thanks

    Rob

  22. #22
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I assume you set some session variable like $_SESSION['loggedIn'] = true; when you login first and then check this on pages to see if user is logged in or something familiar. So you could do something like this:

    HTML
    Code:
    <a href="mypage.php?logout">Logout</a>
    PHP Code:
    if (isset($_GET['logout']))
    {
        
    $_SESSION['loggedIn'] = false// or just unset the variable
        // unset($_SESSION['loggedIn']);


  23. #23
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the code

    Just to clarify do I need to put the above together on every page? And where you have mypage.php should that be login.php which is my login page?

    Many thanks

    Rob

  24. #24
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rredford View Post
    Thanks for the code

    Just to clarify do I need to put the above together on every page? And where you have mypage.php should that be login.php which is my login page?

    Many thanks

    Rob
    mypage.php is the page that link is located to. And optimal would be that you have so called "front controller" that takes care of the executions and includes the stuff on every page. But you could also add the link on every page you want to display the logout link.

  25. #25
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks again for your reply, I have sorted it now.

    Thanks again :-)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •