Announcement

Collapse
No announcement yet.

php/javascript question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • php/javascript question

    Hello,

    I'm building a website with an input form. The form has a lot of javascript checks, which display errormessages in dynamic html. These checks are performed onblur (for each field) and onsubmit (for the entire form, a function CheckForm is called).

    Now, I would like to send the data to a PHP script.
    I currently have action="sendmail.php", which directs to an error page and asks the user to press back to return to the form: all the dhtml errormessages are still show.

    But, can I avoid the user having to press back by not redirecting to a new page if CheckForm says there are errors?
    (If I redirect the user to the form page, the page reloads and the dhtml errormessages are not shown )

    An option might be to have an onload function, which checks the referrer (how can I do this?). If the referrer is the php script: call CheckForm.

    Bear in mind that the page is part of a Typo3 CMS, so I'm limited in sending parameters to pages.

    Thanks!


    Jorg
    Last edited by VJ; 23 March 2006, 03:41.
    pixar
    Dream as if you'll live forever. Live as if you'll die tomorrow. (James Dean)

  • #2
    Originally posted by VJ
    An option might be to have an onload function, which checks the referrer (how can I do this?). If the referrer is the php script: call CheckForm.
    Ok, this seems to work...

    Just for more customisation, can I set the referrer value in my PHP script?


    Jörg
    pixar
    Dream as if you'll live forever. Live as if you'll die tomorrow. (James Dean)

    Comment


    • #3
      Why are you sending the form to another page? wouldn't it just be better to validate and show the errors on the actual form page.

      Comment


      • #4
        Originally posted by dbdg
        Why are you sending the form to another page? wouldn't it just be better to validate and show the errors on the actual form page.
        It is the result of the submit button...

        The validation and display of errors happens on the actual form page, but when one press the submit button, the php script is loaded. I found it the easiest to just use header(location: ...) to get back to the original form page.
        Only problem is that it then needs to differentiate between loading normally (don't check for errors yet) and loading when returning from the php script (check for errors so that they are displayed in dhtml).

        (I didn't really find how to only have the php script be called when there are no errors )


        Jörg
        pixar
        Dream as if you'll live forever. Live as if you'll die tomorrow. (James Dean)

        Comment


        • #5
          Add a get value into the url, not secure but it will get you started. Header could then be header("Location: page.php?Success=True");

          On the php page you then check to see if the get value is set and whether or not it equals the expected response.

          PHP Code:
          if(isset($_GET['Success']) && ($_GET['Success'] == True)) {

          // What you want to happen on returning from the php script


          Or you could use one of the fields from $_SERVER to check for the referer. $_SERVER['HTTP_REFERER'] is not however consistant between user agents.

          If you change the form action to <?php echo $_SERVER['PHP_SELF']; ?> the page could validate itself.
          Last edited by dbdg; 23 March 2006, 07:11.

          Comment


          • #6
            Originally posted by dbdg
            Add a get value into the url, not secure but it will get you started. Header could then be header("Location: page.php?Success=True");

            On the php page you then check to see if the get value is set and whether or not it equals the expected response.
            You mean as an alternative to using the referer to see how the user reaches this page?
            (bear in mind: I'm a PHP newbie )

            PHP Code:
            if(isset($_GET['Success']) && ($_GET['Success'] == True)) {

            // What you want to happen on returning from the php script


            This goes in the body of the website, right?

            Can I then simply call a javascript function?



            Jörg
            pixar
            Dream as if you'll live forever. Live as if you'll die tomorrow. (James Dean)

            Comment


            • #7
              Originally posted by VJ
              You mean as an alternative to using the referer to see how the user reaches this page? (bear in mind: I'm a PHP newbie )
              Yeah, you can use Referer but it isn't consistant. Problem with using a get value however is that anyone with any know how will see what you are doing.

              Try Referer and see what happens, means you would check for

              PHP Code:
              <?php if(isset($_SERVER['HTTP_REFERER']) && ($_SERVER['HTTP_REFERER'] == "nameofpage.php")) { ?>
              Originally posted by VJ
              This goes in the body of the website, right?

              Can I then simply call a javascript function?
              Can go where ever you want, if you need to output javascript or html you just drop out of php.

              PHP Code:
              <?php if(isset($_GET['Success']) && ($_GET['Success'] == True)) { ?>

              Standard javascript of html here
               
              <?php ?>

              Comment


              • #8
                Why would you submit the form if there are errors?

                You are doing something like:

                Code:
                <form action="sendmail.php" onSubmit="return checkForm()">
                right?

                If the checkForm returns false, the form will not be submitted.

                I think the cleanest would be to do all your error checking in the checkForm function, and not have some function called for each field with the onBlur handler.

                Jan

                Comment


                • #9
                  Code:
                  <form action="sendmail.php" onSubmit="return checkForm()">
                  Well, it looks likes this:
                  Code:
                  <form name="form1" method="post" action="sendmail.php" onsubmit="return CheckForm()"  >
                  If the checkForm returns false, the form will not be submitted.
                  Nope: sendmail.php is called regardless of the returnvalue.

                  Ideally, I would want it to behave as you say: don't submit when there are errors (so no reload, no need t press a back button, ...) and submit when there aren't any errors.

                  Jörg
                  Last edited by VJ; 23 March 2006, 14:21.
                  pixar
                  Dream as if you'll live forever. Live as if you'll die tomorrow. (James Dean)

                  Comment


                  • #10
                    Could you post your code?

                    Comment


                    • #11
                      Originally posted by jms
                      Could you post your code?
                      I should still clean it up, but this is the function:
                      Code:
                      function CheckForm() {
                      
                      	tempbool = CheckField('naam');
                      	tempbool = CheckField('voornaam') && tempbool;
                      	tempbool = CheckField('adres')  && tempbool;
                      	tempbool = CheckField('postnummer')  && tempbool;
                      	tempbool = CheckField('stad')  && tempbool;
                      	tempbool = CheckBevestigen(true) && tempbool;
                      	tempbool = CheckProdukten() && tempbool;
                              tempbool = CheckDatum() && tempbool;
                      
                      	if (document.getElementById('cookies').checked) {
                      		cookieForms('save', 'form1');
                      		}
                      	event.ReturnValue=tempbool;
                      		
                      }
                      I have tried adding 'return tempbool' at the end of the function, but it makes no difference.

                      Each of the CheckField functions returns values true or false.


                      Jörg
                      pixar
                      Dream as if you'll live forever. Live as if you'll die tomorrow. (James Dean)

                      Comment


                      • #12
                        Here is the CheckField function (to give an idea). Fieldnames are in Dutch, as is the error message.
                        Code:
                        function CheckField(veld) {
                        
                        s=document.getElementById(veld).value;
                        if (veld=='naam') document.getElementById('e_'+veld).innerHTML="naam"
                          else if (veld=='voornaam') document.getElementById('e_'+veld).innerHTML="voornaam"
                          else if (veld=='adres') document.getElementById('e_'+veld).innerHTML="adres"
                          else if (veld=='postnummer') document.getElementById('e_'+veld).innerHTML="postnummer"
                          else if (veld=='stad') document.getElementById('e_'+veld).innerHTML="stad"
                        
                        if (s=='') { document.getElementById('e_'+veld).innerHTML+=" moet ingevuld worden"; return  false;} 
                        	  else { document.getElementById('e_'+veld).innerHTML="*"; return true;}
                         return s;
                        }
                        Jörg
                        pixar
                        Dream as if you'll live forever. Live as if you'll die tomorrow. (James Dean)

                        Comment


                        • #13
                          Just tested further: it is only in firefox that a return value false is ignored (and the sendmail script is called). In IE, everything goes as expected.


                          Jörg
                          pixar
                          Dream as if you'll live forever. Live as if you'll die tomorrow. (James Dean)

                          Comment


                          • #14
                            I think the best way to do this is have the form submit to itself.

                            Check to see if a blank for is being displayed or errors exist and the form should be redisplayed.

                            If there are no errors then you can process the data.
                            Gigabyte GA-K8N Ultra 9, Opteron 170 Denmark 2x2Ghz, 2 GB Corsair XMS, Gigabyte 6600, Gentoo Linux
                            Motion Computing M1400 -- Tablet PC, Ubuntu Linux

                            "if I said you had a beautiful body would you take your pants off and dance around a bit?" --Zapp Brannigan

                            Comment


                            • #15
                              Originally posted by VJ
                              Here is the CheckField function (to give an idea). Fieldnames are in Dutch, as is the error message.
                              Code:
                              function CheckField(veld) {
                              
                              s=document.getElementById(veld).value;
                              if (veld=='naam') document.getElementById('e_'+veld).innerHTML="naam"
                                else if (veld=='voornaam') document.getElementById('e_'+veld).innerHTML="voornaam"
                                else if (veld=='adres') document.getElementById('e_'+veld).innerHTML="adres"
                                else if (veld=='postnummer') document.getElementById('e_'+veld).innerHTML="postnummer"
                                else if (veld=='stad') document.getElementById('e_'+veld).innerHTML="stad"
                              
                              if (s=='') { document.getElementById('e_'+veld).innerHTML+=" moet ingevuld worden"; return  false;} 
                              	  else { document.getElementById('e_'+veld).innerHTML="*"; return true;}
                               return s;
                              }
                              Jörg

                              Are you sure that this function always will return true or false?

                              Also you should change Event.ReturnValue to only a standard return val. In any case you've prolly typed it wrong, it should be Event.returnValue (lowercase r).

                              Jan
                              Last edited by jms; 24 March 2006, 01:21.

                              Comment

                              Working...
                              X