SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question When Invalid Code is typed - I want a message to pop up and say "Invalid Code"

    I have a security question set up at the end of a form.

    When the user submits it and is successful, it redirects to a "Thank You Page".

    When the user submits it and is NOT successful in typing in the right code, it goes to a "Code Invalid Page - Please Press the Back Button and Try Again".

    Instead of going to the "Invalid Page", I would like some text to appear in the current window (maybe right next to the field?) that will say invalid code, try again.

    Here is my code right now that shows the redirects: (I am using MathGuard)

    PHP Code:


    PHP Code:
    <?php





    /* first we need to require our MathGuard class */

    require ("ClassMathGuard.php");

    /* this condition checks the user input. Don't change the condition, just the body within the curly braces */

    if (MathGuard :: checkResult($_REQUEST['mathguard_answer'], $_REQUEST['mathguard_code'])) {

        echo (
    "<meta http-equiv=\"refresh\" content=\"0;URL=applicationcomplete.php\">"); //insert your code that will be executed when user enters the correct answer

    } else {

        echo (
    "<meta http-equiv=\"refresh\" content=\"0;URL=invalid.html\">"); //insert your code which tells the user he is spamming your website

        
    die();

    }

    ?>
    You can see the invalid.html URL where it goes if the code is incorrect.

    Any input would be appreciated. I am somewhat of a beginner with PHP, but I am getting better. Any help on this is appreciated.

    Thanks in advance,

    John

  2. #2
    SitePoint Enthusiast ZefQ's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    make an ajax request

    something like

    Code JavaScript:
    function createRequest() {
    	if (window.XMLHttpRequest) { // Mozilla, Safari,...
    		xhr = new XMLHttpRequest();
    		if (xhr.overrideMimeType) {
    			xhr.overrideMimeType('text/xml');
    		}
    	} 
    	else if (window.ActiveXObject) { // IE
    		try {
    			xhr= new ActiveXObject("Msxml2.XMLHTTP");
    		} 
    		catch (e) {
    			try {
    				xhr= new ActiveXObject("Microsoft.XMLHTTP");
    			} 
    			catch (e) {}
    		}
    	}
    	if (!xhr) {
    		alert('Cannot create XMLHTTP instance');
    		return false;
    	}
    	var str = "user=" + encodeURI( document.getElementById("foo").value ) + 
    				  "&comment=" + encodeURI( document.getElementById("bar").value );  //the fileds you want to send
     
    	xhr.onreadystatechange = readyState;
    	xhr.open('POST', "thepage.php", true);
    	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhr.setRequestHeader("Content-length", str.length);
        xhr.setRequestHeader("Connection", "close");
    	xhr.send(str);
    }
    }
     
    function readyState() {
    	if(xhr.readyState == 4) {
    		if(xhr.status == 200) {
    			if(xhr.responseText) {
                                 if(xhr.responseText == "false")
                                      document.getElementById('the_field_you_want').innerHTML = "error";
                                 else
                                      location.href ="applicationcomplete.php";
    			}
    		}
    		else {
    			alert("Error, Response status: " + xhr.status)
    		}
    	}
    }


    change the php to something like this

    PHP Code:
    if (MathGuard :: checkResult($_REQUEST['mathguard_answer'], $_REQUEST['mathguard_code'])) {



        echo (
    "true"); //insert your code that will be executed when user enters the correct answer



    } else {



        echo (
    "false"); //insert your code which tells the user he is spamming your website 
    sence i dont know the kod for the forum i cant make it any better but something like this.
    the code is not exakt but you might get the ide..
    good luck

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry - a lot confused on this.

    Can you make this a little bit more clear to me?

    Thanks!

  4. #4
    SitePoint Enthusiast ZefQ's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    read up on AJAX i have seen a tutorial here at sitepoint.

    i can see how it may seem comfusing if you dont know what it is

    basically you send the form without leaving the page and then you get back the response from the php page. based on what comes back you make diffrent thing happend.

    you also have top make an onsubmit() in the form to call the javascript that is going to do it

    or you can skip javascript and make is straight php

    on form.php where the code field is <? if(isset($_SESSION['err'])) echo $_SESSION['err']; ?>

    and in the check.php where the else statement is

    $_SESSION['err']= "msg";

    header("location: form.php" ); or
    echo "<script>history.go(-1)</script>";

    its not the same thing but you will be back on the form.php and have an error msg wher you want it

  5. #5
    SitePoint Enthusiast ZefQ's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just saw something is it a simple math question that is the code then javascript can check it without an XMLRequest

  6. #6
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry to be a pest - could you code what I already have posted on the first post with what you just posted?

    If not, I understand and I can play around with it.

    Thanks again!!!!

  7. #7
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ZefQ View Post
    just saw something is it a simple math question that is the code then javascript can check it without an XMLRequest
    Yes it is a simple math question. Can you explain further?

  8. #8
    SitePoint Enthusiast ZefQ's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    dont know what i ment but i guess
    PHP Code:
    <?php

    /* first we need to require our MathGuard class */



    require ("ClassMathGuard.php");



    /* this condition checks the user input. Don't change the condition, just the body within the curly braces */



    if (MathGuard :: checkResult($_REQUEST['mathguard_answer'], $_REQUEST['mathguard_code'])) {



        echo (
    "<meta http-equiv=\"refresh\" content=\"0;URL=applicationcomplete.php\">"); //insert your code that will be executed when user enters the correct answer



    } else {


        
    $_SESSION['err']= "there is smething wrong with this field!"
        
    echo ("<script>history.go(-1)</script>"); //insert your code which tells the user he is spamming your website


    }

    ?>

  9. #9
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So all I do is copy and paste what you have in that code and it should work?

    Do I need to modify anything else?

    Thanks for your help - I know this is a pain.

  10. #10
    SitePoint Enthusiast ZefQ's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you need to have this

    <? if(isset($_SESSION['err'])) echo $_SESSION['err']; ?>

    somewhere on the form page

    and if you dont have session automated in the php.ini.
    you need to have this on top of both pages

    session_start();

    otherwise it should work

    oh and one more thing the history.go(-1) might just ask you if you want to send the post again in that case try history.go(-2)

  11. #11
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I get this error:

    syntax error, unexpected T_ECHO

    From this code:
    PHP Code:
    <?php



    /* first we need to require our MathGuard class */







    require ("ClassMathGuard.php");







    /* this condition checks the user input. Don't change the condition, just the body within the curly braces */







    if (MathGuard :: checkResult($_REQUEST['mathguard_answer'], $_REQUEST['mathguard_code'])) {







        echo (
    "<meta http-equiv=\"refresh\" content=\"0;URL=applicationcomplete.php\">"); //insert your code that will be executed when user enters the correct answer







    } else {





        
    $_SESSION['err']= "there is smething wrong with this field!"

        
    echo ("<script>history.go(-1)</script>"); //insert your code which tells the user he is spamming your website





    }



    ?>
    It is coming from this line here in particular:

    $_SESSION['err']= "there is smething wrong with this field!"

  12. #12
    SitePoint Enthusiast ZefQ's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah you need to close that line with ;

    "there is something wrong whit this field!"

    should be
    "there is something wrong whit this field!";

  13. #13
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!

    The only problem is when the code is invalid, no message comes up. The page stays where it is if it's invalid. When it is valid, no problem - submits everything no problem.

    Any help is appreciated.

    Thank you!

    - John

  14. #14
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK - it does work.

    But - the invalid code comes up automatically now. Here is what I have in the form code:

    PHP Code:
    <div style="color: red; background-color: white; font-family: Georgia, 'Times New Roman', Times, serif; font-size: 14px;">
    <? if(isset($_SESSION['err'])) echo $_SESSION['err']; ?>
    </div>

  15. #15
    SitePoint Enthusiast ZefQ's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah you have to kill the session also
    <? if(isset($_SESSION['err'])) echo $_SESSION['err']; session_destroy(); ?>

    and you probably cant use that javascript cuz the page maybe need to reload

    so if it dont work replace <script> ... </script>
    header("location: thepage.php")

  16. #16
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That worked - but what it does is refreshes the page and the data the user enters is cleared.

    Any ideas?

    Thanks again - you are a HUGE help.

  17. #17
    SitePoint Enthusiast ZefQ's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah as i sayd AJAX is something to read up on coz then you dont have to leave the page so all is at it was before...

    but if you want strikt php u can save all post in session variables

    i dont know what the <input> name is but lets say u have a field where you supose to enter your name..

    Code HTML4Strict:
    <input type="text" name="first_name" value="<? if(isset($_SESSION['fName']) echo $_SESSION['fName']; ?>" />

    ond so on you get the idea..
    but remember to dont kill the session until the very end

    and so in the else colum on the check.php or what its caled
    $_SESSION['fname'] == $_POST['first_name'];

  18. #18
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks again.

    Would jquery work with this?

  19. #19
    SitePoint Enthusiast ZefQ's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    idont know, i have never used it but i cant see why not...


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
  •