Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Newbie how to style php

  1. #1
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Newbie how to style php

    I have some php code that I would like to style and ideally stay on the same page and not go to another page. But I know nothing about php, it is for a captcha. I would like the code for a div and some way of naming the div so that I can style it, I want the div to remain on the same page when I tried to give the div an id, I developed an php error. Which frightened me, so I removed it.

    PHP Code:
    if (!$resp->is_valid) {
        
    // What happens when CAPTCHA entered incorrectly ...$redirect = "../thanks.html";  
        // What happens when CAPTCHA entered incorrectly ...
    die ("<h3>Error</h3>The reCAPTCHA wasn't entered correctly. Go back and try it again." .
             
    "(reCAPTCHA said: " $resp->error ")");
       } else {
        
    // Successful captcha, so continue on with the email ...
      


  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    You should be ok with;

    Code:
    if (!$resp->is_valid) { 
        // What happens when CAPTCHA entered incorrectly ...$redirect = "../thanks.html";   
        // What happens when CAPTCHA entered incorrectly ... 
    die ("<div id='message'><h3>Error</h3>The reCAPTCHA wasn't entered correctly. Go back and try it again.</div>" . 
             "(reCAPTCHA said: " . $resp->error . ")"); 
       } else { 
        // Successful captcha, so continue on with the email ... 
      }
    Notice I used id='message' rather than id="message" - the quotes probably broke it.
    You can also use id=\"message\" as long as you put a backslash before each quote to escape them

    and the CSS;
    Code:
    #message {
    background:yellow;
    color:black;
    padding:10px;
    border:1px solid black;
    }
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    He/she could also initiate the calling with single quotes rather than double quotes as the variable is being concocted anyway.

    Could you be more clear about the "go to another page"? Is the address of the page changing or are you not getting all of the code you except to see? In what you've posted for code there is no reason for it to be loading another page so we'd need more code to troubleshoot that.
    Corrections to my coding/thoughts welcome.

  4. #4
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Yes, the page is going to another url.
    I thought that if I put it in an error div it would stay just below the original recaptcha. Thanks for the tip on single quotes, I will try that.

  5. #5
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I tried to do it again but alas, it threw up an error again.
    The error is on the closing div line so it is in the wrong place.
    PHP Code:
    if (!$resp->is_valid) {
        
    // What happens when CAPTCHA entered incorrectly ...$redirect = "../thanks.html";  
        // What happens when CAPTCHA entered incorrectly ...
    die ("<div id='captcha'><h3>Error</h3>The reCAPTCHA wasn't entered correctly. Go back and try it again." .
             
    "(reCAPTCHA said: " $resp->error . </div>")");
       } else {
        
    // Successful captcha, so continue on with the email ...
      


  6. #6
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Quotes in the wrong place, you can't have html in php. Please include your error message in future postings it will help identifying the problem.

    PHP Code:
    if (!$resp->is_valid) {
        
    // What happens when CAPTCHA entered incorrectly ...$redirect = "../thanks.html";  
        // What happens when CAPTCHA entered incorrectly ...
    die ("<div id='captcha'><h3>Error</h3>The reCAPTCHA wasn't entered correctly. Go back and try it again." .
             
    "(reCAPTCHA said: " $resp->error "</div>)");
       } else {
        
    // Successful captcha, so continue on with the email ...
      

    If you dont want it to redirect we will need to see the code, or you can try adding // in front of a line with header("location: $redirect"); that is a guess though, could break something...
    Corrections to my coding/thoughts welcome.

  7. #7
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re

    There is a lot of code to add, but I will do so here: This is the process.php which processes the form.

    PHP Code:
    <?php  
    session_start
    ();

    require_once(
    'recaptchalib.php');
      
    $privatekey "Removed";
      
    $resp recaptcha_check_answer ($privatekey,
                                    
    $_SERVER["REMOTE_ADDR"],
                                    
    $_POST["recaptcha_challenge_field"],
                                    
    $_POST["recaptcha_response_field"]);

    // Get variables from your form ...  
    $username $_POST['name'];
    $useremail $_POST['email'];
    // Who the form gets sent to ...  
    $recipient "removed";
    $subject "Website Form";  

    // Where to go after the user submits the form ...  
    $redirect "../thankyou.html";  

    if (!
    $resp->is_valid) {
        
    // What happens when CAPTCHA entered incorrectly ...$redirect = "../thanks.html";  
        // What happens when CAPTCHA entered incorrectly ...
    die ("<div id='captcha_error'>The reCAPTCHA wasn't entered correctly. Go back and try it again." .
             
    "(reCAPTCHA said: " $resp->error ")</div>");
       } else {
        
    // Successful captcha, so continue on with the email ...
      
    }

    $message "
    ================================
    Name:  
    $username
    Email:  
    $useremail
    -----------------------------------------
    $fmessage
    ================================
    "
    ;  

    $extraheaders "From: $useremail\n\nReply-To: $useremail\n\n";  

    if (! 
    mail($recipient$subject$message$extraheaders))
      echo 
    "Mail did not send for some reason.";  

    header("Location: ".$redirect);  

    ?>
    This is the form iteself.
    PHP Code:
    <form id="form" action="process.php" enctype="multipart/form-data" method="post" name="form_to_email">
    <fieldset>
    <legend>Enquiry Form</legend>
    <p><label for="name"><span>Name</span></label><input id="name" name="name" size="35" type="text" /></p>
    <p><label for="email"><span>Email</span></label><input id="email" name="email" size="35" type="text" /></p>
    </fieldset><fieldset>
    <p><label for="message"><span>Message</span></label><textarea id="message" cols="40" name="message" rows="8"></textarea></p>
    </fieldset><fieldset>All fields Required </fieldset>
    <script type="text/javascript">
            var RecaptchaOptions = {
                theme : 'clean',
                tabindex : 0
            };
    </script><noscript><p>Please enable javascript in your browser to post this form</p></noscript>
    <?php
    require_once('recaptchalib.php');
    $publickey "Removed";
    echo 
    recaptcha_get_html($publickey);
    ?>
    <p><input id="submit" class="submit" name="submit" type="submit" value="Submit" /></p></form>
    And this is the general recaptcha.lib
    PHP Code:
    <?php
     
    Copyright (c2007 reCAPTCHA -- http://recaptcha.net
     
    AUTHORS:
     *   
    Mike Crawford
     
    *   Ben Maurer
    /**
     * The reCAPTCHA server URL's
     */
    define("RECAPTCHA_API_SERVER""http://www.google.com/recaptcha/api");
    define("RECAPTCHA_API_SECURE_SERVER""https://www.google.com/recaptcha/api");
    define("RECAPTCHA_VERIFY_SERVER""www.google.com");

    /**
     * Encodes the given data into a query string format
     * @param $data - array of string elements to be encoded
     * @return string - encoded request
     */

    function _recaptcha_qsencode ($data) {
            
    $req "";
            foreach ( 
    $data as $key => $value )
                    
    $req .= $key '=' urlencodestripslashes($value) ) . '&';

            
    // Cut the last '&'
            
    $req=substr($req,0,strlen($req)-1);
            return 
    $req;
    }

    /**
     * Submits an HTTP POST to a reCAPTCHA server
     * @param string $host
     * @param string $path
     * @param array $data
     * @param int port
     * @return array response
     */
    function _recaptcha_http_post($host$path$data$port 80) {

            
    $req _recaptcha_qsencode ($data);

            
    $http_request  "POST $path HTTP/1.0\r\n";
            
    $http_request .= "Host: $host\r\n";
            
    $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
            
    $http_request .= "Content-Length: " strlen($req) . "\r\n";
            
    $http_request .= "User-Agent: reCAPTCHA/PHP\r\n";
            
    $http_request .= "\r\n";
            
    $http_request .= $req;

            
    $response '';
            if( 
    false == ( $fs = @fsockopen($host$port$errno$errstr10) ) ) {
                    die (
    'Could not open socket');
            }

            
    fwrite($fs$http_request);

            while ( !
    feof($fs) )
                    
    $response .= fgets($fs1160); // One TCP-IP packet
            
    fclose($fs);
            
    $response explode("\r\n\r\n"$response2);

            return 
    $response;
    }
    /**
     * Gets the challenge HTML (javascript and non-javascript version).
     * This is called from the browser, and the resulting reCAPTCHA HTML widget
     * is embedded within the HTML form it was called from.
     * @param string $pubkey A public key for reCAPTCHA
     * @param string $error The error given by reCAPTCHA (optional, default is null)
     * @param boolean $use_ssl Should the request be made over ssl? (optional, default is false)

     * @return string - The HTML to be embedded in the user's form.
     */
    function recaptcha_get_html ($pubkey$error null$use_ssl false)
    {
        if (
    $pubkey == null || $pubkey == '') {
            die (
    "To use reCAPTCHA you must get an API key from <a href='https://www.google.com/recaptcha/admin/create'>https://www.google.com/recaptcha/admin/create</a>");
        }
        
        if (
    $use_ssl) {
                    
    $server RECAPTCHA_API_SECURE_SERVER;
            } else {
                    
    $server RECAPTCHA_API_SERVER;
            }

            
    $errorpart "";
            if (
    $error) {
               
    $errorpart "&amp;error=" $error;
            }
            return 
    '<script type="text/javascript" src="'$server '/challenge?k=' $pubkey $errorpart '"></script>

        <noscript>
              <iframe src="'
    $server '/noscript?k=' $pubkey $errorpart '" height="300" width="500" frameborder="0"></iframe><br/>
              <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
              <input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>
        </noscript>'
    ;
    }
    /**
     * A ReCaptchaResponse is returned from recaptcha_check_answer()
     */
    class ReCaptchaResponse {
            var 
    $is_valid;
            var 
    $error;
    }
    /**
      * Calls an HTTP POST function to verify if the user's guess was correct
      * @param string $privkey
      * @param string $remoteip
      * @param string $challenge
      * @param string $response
      * @param array $extra_params an array of extra variables to post to the server
      * @return ReCaptchaResponse
      */
    function recaptcha_check_answer ($privkey$remoteip$challenge$response$extra_params = array())
    {
        if (
    $privkey == null || $privkey == '') {
            die (
    "To use reCAPTCHA you must get an API key from <a href='https://www.google.com/recaptcha/admin/create'>https://www.google.com/recaptcha/admin/create</a>");
        }

        if (
    $remoteip == null || $remoteip == '') {
            die (
    "For security reasons, you must pass the remote ip to reCAPTCHA");
        }

        
            
    //discard spam submissions
            
    if ($challenge == null || strlen($challenge) == || $response == null || strlen($response) == 0) {
                    
    $recaptcha_response = new ReCaptchaResponse();
                    
    $recaptcha_response->is_valid false;
                    
    $recaptcha_response->error 'incorrect-captcha-sol';
                    return 
    $recaptcha_response;
            }

            
    $response _recaptcha_http_post (RECAPTCHA_VERIFY_SERVER"/recaptcha/api/verify",
                                              array (
                                                     
    'privatekey' => $privkey,
                                                     
    'remoteip' => $remoteip,
                                                     
    'challenge' => $challenge,
                                                     
    'response' => $response
                                                     
    ) + $extra_params
                                              
    );

            
    $answers explode ("\n"$response [1]);
            
    $recaptcha_response = new ReCaptchaResponse();

            if (
    trim ($answers [0]) == 'true') {
                    
    $recaptcha_response->is_valid true;
            }
            else {
                    
    $recaptcha_response->is_valid false;
                    
    $recaptcha_response->error $answers [1];
            }
            return 
    $recaptcha_response;

    }

    /**
     * gets a URL where the user can sign up for reCAPTCHA. If your application
     * has a configuration page where you enter a key, you should provide a link
     * using this function.
     * @param string $domain The domain where the page is hosted
     * @param string $appname The name of your application
     */
    function recaptcha_get_signup_url ($domain null$appname null) {
        return 
    "https://www.google.com/recaptcha/admin/create?" .  _recaptcha_qsencode (array ('domains' => $domain'app' => $appname));
    }

    function 
    _recaptcha_aes_pad($val) {
        
    $block_size 16;
        
    $numpad $block_size - (strlen ($val) % $block_size);
        return 
    str_pad($valstrlen ($val) + $numpadchr($numpad));
    }
    //Mailhide and Credits Removed//
    What I am trying to find out how to do, is that if there is an error with the recaptcha, I want the error displayed underneath the recaptcha, at the moment it goes to a new url. Been searching the web for days now, I cant write php code or pay anyone to write code for me. But I thought if I put the code in a div it should stay underneath the recaptcha.

  8. #8
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Are you getting an error message still or just redirected?

    The redirect is

    header("Location: ".$redirect);
    so make that

    PHP Code:
    //header("Location: ".$redirect); 
    Corrections to my coding/thoughts welcome.

  9. #9
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Its OK I dont think you understand, sometimes I want the page to redirect to the thanks page. But if the code is wrong I just want the errror message to remain underneath the recaptcha box.

  10. #10
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Oh, okay try this.

    PHP Code:
    <?php  
    session_start
    ();

    require_once(
    'recaptchalib.php');
      
    $privatekey "Removed";
      
    $resp recaptcha_check_answer ($privatekey,
                                    
    $_SERVER["REMOTE_ADDR"],
                                    
    $_POST["recaptcha_challenge_field"],
                                    
    $_POST["recaptcha_response_field"]);

    // Get variables from your form ...  
    $username $_POST['name'];
    $useremail $_POST['email'];
    // Who the form gets sent to ...  
    $recipient "removed";
    $subject "Website Form";  

    // Where to go after the user submits the form ...  
    $redirect "../thankyou.html";  

    if (!
    $resp->is_valid) {
        
    // What happens when CAPTCHA entered incorrectly ...$redirect = "../thanks.html";  
        // What happens when CAPTCHA entered incorrectly ...
    ?>
    <div id='captcha_error'>The reCAPTCHA wasn't entered correctly. Go back and try it again.<br />(reCAPTCHA said: <?php echo $resp->error ?>
    <form id="form" action="process.php" enctype="multipart/form-data" method="post" name="form_to_email">
    <fieldset>
    <legend>Enquiry Form</legend>
    <p><label for="name"><span>Name</span></label><input id="name" name="name" size="35" type="text" /></p>
    <p><label for="email"><span>Email</span></label><input id="email" name="email" size="35" type="text" /></p>
    </fieldset><fieldset>
    <p><label for="message"><span>Message</span></label><textarea id="message" cols="40" name="message" rows="8"></textarea></p>
    </fieldset><fieldset>All fields Required </fieldset>
    <script type="text/javascript">
            var RecaptchaOptions = {
                theme : 'clean',
                tabindex : 0
            };
    </script><noscript><p>Please enable javascript in your browser to post this form</p></noscript>
    <?php
    require_once('recaptchalib.php');
    $publickey "Removed";
    echo 
    recaptcha_get_html($publickey);
    ?>
    <p><input id="submit" class="submit" name="submit" type="submit" value="Submit" /></p></form>
    <?php
    exit();
       } else {
        
    // Successful captcha, so continue on with the email ...
      
    }

    $message "
    ================================
    Name:  
    $username
    Email:  
    $useremail
    -----------------------------------------
    $fmessage
    ================================
    "
    ;  

    $extraheaders "From: $useremail\n\nReply-To: $useremail\n\n";  

    if (! 
    mail($recipient$subject$message$extraheaders))
      echo 
    "Mail did not send for some reason.";  

    header("Location: ".$redirect);  

    ?>
    Corrections to my coding/thoughts welcome.

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
  •