Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2002
    Posts
    43

    Unanswered: Javascript with PHP

    Can anyone tell me whats wrong with this code. It gets called from an onSubmit statement in the form code. The alert is shown, but the function always returns true. If I take the if statement out and just return false then it works properly. Any suggestions?

    <? echo("<script Language=\"JAVASCRIPT\">

    function ValidateForm()
    {
    alert (\"Test alert\");

    if (username==\"validuser\")
    {
    return true;
    }
    else
    {
    return false;
    }

    }
    </script>"); ?>

  2. #2
    Join Date
    Aug 2003
    Location
    Bologna - Italy
    Posts
    209
    If what you're saying is that you have a form with a field named "username", and you want to check for the value it contains, then you should write it this way:

    document.forms[0].elements["username"].value=="validuser"

    Bye!
    The only failure is not trying to do it.

  3. #3
    Join Date
    Sep 2003
    Location
    New Zealand
    Posts
    6
    where does the PHP come into it? Is the alert after the form has been processed or prior to the submit happening?

  4. #4
    Join Date
    Sep 2003
    Location
    Wisconsin, USA
    Posts
    34

    DOM

    sarah - This is a javascript question, unrelated to PHP.

    shores - You've got it right.

    cmptguru - You need to study how javascript works before trying to write PHP code that generates javascript Mainly study the DOM (Document Object Module), which is how you use javascript to interact with the html document.

    There are many ways to do this very thing depending on your target audience (how old their browser is and if it's IE or something else) and how your page is layed out.

    May I suggest
    http://www.w3schools.com/js/default.asp
    and
    http://www.w3schools.com/dhtml/default.asp

  5. #5
    Join Date
    Sep 2003
    Location
    New Zealand
    Posts
    6

    Re: DOM

    [SIZE=1]Originally posted by moku
    sarah - This is a javascript question, unrelated to PHP.
    Duh, I could see that! I was trying to find out from cmptrguru7 what exactly was going on so I take him/her a step further.

    Using Javascript & PHP together is one of the biggest mental hurdles for some coders I reckon.

  6. #6
    Join Date
    Sep 2003
    Location
    Wisconsin, USA
    Posts
    34

    sarahk

    Sorry sarah, I knew you realized that. Javascript + PHP -is- a pretty big challenge at first.

    I was just flying through some responses and I guess I made that one a little too short. ... should have put "sarah - You're right, this is a javascript problem"

    cmptguru: any questions / problems?

  7. #7
    Join Date
    Feb 2002
    Posts
    43
    I have solved my problem thank you for your help.

Posting Permissions

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