1

EDIT: Oops. Thanks all!

I have the following code:

<?php
    echo"
    <script type=\"text/javascript\">

    function validation() {

    var ok = 0;
    ";

    for ($i=1; $i<=10; $i++)
      {
        echo"   for (i=document.frmSurvey.q".$i.".length-1; i > -1; i--) {
                if (document.frmSurvey.q".$i."[i].checked) {
                    v = i; i = -1;
                }
            }
            if (i == -1) {
                ok = 1;
                document.getElementById(\"rq".$i."\").style.backgroundColor = '#901F39';
                document.getElementById(\"rq".$i."\").style.color = '#FFF';
                document.getElementById('mandall').style.backgroundColor = '#901F39';
                document.getElementById('mandall').style.color = '#FFF';
                return false;
            }";
        }


    echo "if (ok == 0) document.frmSurvey.submit();
    }
    </script>";
?>

This code is sitting within the <head> section of my page. However, it is merely echoing the text onto the page and not actually creating the Javascript.

I'm thinking I should be using a <header> option but I'm completely lost.

Any and all advise welcome!

Thanks,

H.

3
  • 2
    This should be obvious. You're closing your script tag straight after echoing an opening one. <script type=\"text/javascript\"></script> Commented Nov 29, 2012 at 22:51
  • why not just do all the looping in javascript? Commented Nov 29, 2012 at 22:53
  • I am suitably embarrassed!!!! Thanks all!!!! Commented Nov 29, 2012 at 22:58

5 Answers 5

4

Forget PHP:

<script type="javascript">

    function validation() {
        var ok = 0;

        for (var i=1 i<=10; i++){
            for (var j=document.frmSurvey['q'+i].length-1; j > -1; j--) {
                if (document.frmSurvey['q'+j][i].checked) {
                    v = j; j = -1;
                }
            }
            if (j == -1) {
                ok = 1;
                document.getElementById('rq'+i).style.backgroundColor = '#901F39';
                document.getElementById('rq'+i).style.color = '#FFF';
                document.getElementById('mandall').style.backgroundColor = '#901F39';
                document.getElementById('mandall').style.color = '#FFF';
                return false;
            }
        }

        if (ok === 0) {
            document.frmSurvey.submit();
        }
    }
</script>
4
  • Many thanks, this is very much appreciated. However, when I did this, the validation didn't work - hence why I was trying to pop it into a PHP loop. Commented Nov 29, 2012 at 23:00
  • You are using the same variable for the inside loop as the outside loop. outside loop counts up while inside loop counts down to 0 meaning on each outside loop iteration, after the inside loop, i will always be 0 (or -1 and return out when .checked). Commented Nov 29, 2012 at 23:03
  • Hmm, OK, modified so that the for (var is using p instead of i - still not working - yet with the PHP iterating this 10 times, works a treat. What am I missing (clearly tonight a lot as I missed the damn <script>! Commented Nov 29, 2012 at 23:08
  • I updated the answer to what should work based on your php code above. I don't know what you are doing with this. From reading the code it looks like you are looping over groups of radio buttons and if any of the buttons are checked, coloring the row background and returning out of the function. What I think you want is if none of the radio buttons are filled out, set backgroundcolor and return. The way it is written, the only way this would submit the form is if nothing was selected. Commented Nov 29, 2012 at 23:19
0

Just a long shot, but this is what I think you want/need.

<script type="javascript">
    function validation() {
        var err=0,
            ok,i,j;

        //loop through groups of radio buttons
        for (i=1 i<=10; i++){
            //reset ok for this loop
            ok=0;

            //loop through radio buttons in this group
            for (j=document.frmSurvey['q'+i].length-1; j >= 0; j--) {
                //if the button is checked...
                if (document.frmSurvey['q'+j][i].checked) {
                    //set ok to 1
                    ok=1;

                    //break out of loop because we know one is checked, no need to continue on to rest
                    break;
                }
            }

            //if there were no buttons checked
            if (o==0) {
                //color row background and text
                document.getElementById('rq'+i).style.backgroundColor = '#901F39';
                document.getElementById('rq'+i).style.color = '#FFF';
                //color something else background and text
                document.getElementById('mandall').style.backgroundColor = '#901F39';
                document.getElementById('mandall').style.color = '#FFF';

                //increment our error counter
                err++;
            }
        }

        //if there were no errors
        if(err==0){
            //submit the form
            document.frmSurvey.submit();
        } else {
            //tell them how many errors there were
            alert("There were "+err+" errors. Please fix them and try again.");
        }
    }
</script>

Loops through radio groups, then each radio button looking for any radio groups that don't have at least one checked value. If the group has no checked value, the background and text color is changed and an error counter is incremented. If there are no errors, the form is submitted else there is an alert saying how many errors there were.

1
  • Cheers Johathan, appreciated. Commented Nov 30, 2012 at 8:38
0

Why don't you put the JS code inside the <script> tags, without using it inside PHP tags. You can use the PHP tags, just for the variables that come from the server-side or PHP code.

0

You are closing your <script> tag in the third line:

<?php
    echo"
    <script type=\"text/javascript\"></script>

Remove the </script> here and it should work.

0

Replace

<script type=\"text/javascript\"></script>

With

<script type=\"text/javascript\">

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.