Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder Andy92's Avatar
    Join Date
    Nov 2005
    Location
    Horsham, West Sussex, UK
    Posts
    363
    Thanks
    7
    Thanked 1 Time in 1 Post

    Javascript in php

    Hi there,

    Im my private message system that i am working on, i have a form where the person that wants to send a private message types in the recipient username, subject and message.

    When they click send it goes to a javascript validate form, and any errors come up with javascript alerts.

    But, im trying to do validate weather the reciepent username enterd is valid. Ive put the php in my validation form as this...

    Code:
    $SQL2 = "SELECT * from USERS WHERE USERNAME = '<script type='text/javascript'>
    <!--
    document.sendmail.recipient.value
    //-->
    </script>'"; 
        $result2 = @mysql_query( $SQL2 ); $row2 = @mysql_fetch_array( $result2 );
        if ("<script type='text/javascript'>
    <!--
    document.sendmail.recipient.value
    //-->
    </script>" == $row2[USERNAME]) {
        } else {
        echo "{ alert ( 'The recipient username you have enterd doesnt exist!' ); valid = false; }";
        }
    But where i have put...

    Code:
    <script type='text/javascript'>
    <!--
    document.sendmail.recipient.value
    //-->
    </script>
    Its not searching the database for the reciepient username that the person tryed in, its saying that the whole javascript above is the username.

    How can i get it so that it puts in what the person typed in the recipient field into the...

    SELECT * from USERS WHERE USERNAME = "--RECIPIENT FIELD--"


  • #2
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Don't mix JS into php. It doesn't work.

    Read up on POST values. This is where you'll find the data you want to process.

    $SQL2 = "SELECT * from USERS WHERE USERNAME = '".$_POST['recipient']."'";

    Ref: http://www.php.net/manual/en/reserve...variables.post
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.

  • #3
    Regular Coder Andy92's Avatar
    Join Date
    Nov 2005
    Location
    Horsham, West Sussex, UK
    Posts
    363
    Thanks
    7
    Thanked 1 Time in 1 Post
    Yes, but i wanted it to go and check if the user existed, live, and if it doesnt then it would display an alert message.

    But, to be honest i could do it another way.

    I could let the form submit, and then if the user doesnt exist, then i could load the data back into the form fields.



    Thanks!

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,037
    Thanks
    2
    Thanked 316 Times in 308 Posts
    Since PHP is executed server side, the php code you have in the page you posted is executed when that page is requested by the browser.

    To do what you want, verify information entered in a form against information located on the server, without refreshing the page, you must use AJAX techniques - http://w3schools.com/ajax/default.asp (replace any references to .asp as the server side scripting at that link with your equivalent .php code.)
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    New to the CF scene
    Join Date
    Aug 2007
    Location
    Kolkata,India
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can't mix up JavaScript & PHP, what you can do is use Ajax, or provide JS with a array of valid users!

  • #6
    New Coder
    Join Date
    Feb 2007
    Location
    NM. USA
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Yes, AJAX is the way to go if you are looking for live responses. It is not nearly as scary as it sounds. Basically you want one PHP script that takes the username as a variabe (GET is easiest) all it will output is true/false or 1/0 etc. You call this page from within your Javascript (see the link above fore more details on exact call) and then display the error or move on as you would like to.

    Its becoming very popular with username checking to give instant feedback.


    _________________________
    "Insanity is hereditary - you get it from your children." Sam Levenson
    Web Development Company – Projects (Download professional web icons for your site Compare stock image agencies)

  • #7
    Regular Coder Andy92's Avatar
    Join Date
    Nov 2005
    Location
    Horsham, West Sussex, UK
    Posts
    363
    Thanks
    7
    Thanked 1 Time in 1 Post
    Thanks guys, but i have it all sorted out now.



  •  

    Posting Permissions

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