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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Using PHP Validation with JavaScript

    Hi,

    A little problem I am facing. What is happening is that a user selects a radio button and then a JavaScript displays the appropriate fieldset with more options for the user to pick.

    Upon submitting the form, there is a PHP validation script that is run and if there is an error the page reloads with the error message, but my fieldset that was displayed on the prior page does not show unless I click the radio button again (The radio button is marked already on this page).

    My question is:
    How do I get my PHP validation to recognize the JavaScript and leave the users selection checked and shown if there is an error? Any help would be much appreciated. Thanks.

    The code that I need to say something:

    <?php
    if ($errors) {
    print "<p class='stdfnt_red'>&nbsp;&nbsp;There are errors in the form.<br>&nbsp;&nbsp;Please correct the fields marked in red</p>";

    if ($_POST['style'] == 'calsecrx' || 'calsecrx2' || 'calsecrx3' || 'calsecrx3b' || 'calsecrx4' || 'calsecrx4c') {
    Display the fieldset with an error;
    }
    if ($_POST['style'] == 'calsecrx4b') {
    Display the fieldset with an error;
    }
    }
    ?>
    Last edited by apowers20; 07-07-2005 at 09:50 PM.

  • #2
    Registered User
    Join Date
    Apr 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript/PHP validation

    Hey,

    This keeps the radio button still selected after an alert is displayed for a
    wrong entry in the form. The script at the bottom handles the error message displayed by php while the page was still loading. Hope this helps!

    <html lang="en">
    <head>
    <script type="text/javascript">
    var errorinmiddle = 0;
    var selectedvalue;

    function error(message, selval) {
    alert(message);
    errorinmiddle = 1;
    selectedvalue = selval;
    }
    </script>
    </head>
    <body >
    <?php
    $calsecrx = array("calsecrx1", "calsecrx2", "calsecrx3", "calsecrx4");

    if (isset($_REQUEST[formradio]) && $_REQUEST[formradio] != "calsecrx3") {
    echo "<p>&nbsp;&nbsp;There are errors in the form.<br>&nbsp;&nbsp;Please correct the fields marked in red</p>";

    if($_REQUEST[error] == 0) {
    echo "<script type='text/javascript'>";
    echo "error('You selected ".$_REQUEST[formradio]." when you need to select calsecrx3', '".$_REQUEST[formradio]."');";
    echo "</script>";
    }
    }
    ?>
    <form method="post" name="userform">
    <input type="hidden" name="error" value="0">
    <? foreach($calsecrx as $key => $value) { ?>
    <? if(isset($_REQUEST[formradio]) && $_REQUEST[formradio] == $value) { ?>
    <? if($_REQUEST[error] == 1) { ?>
    <input type="radio" name="formradio" value="<?=$value?>" id="<?=$value?>" checked/><font color="#FF0000"><?=$value?></font><br>
    <? } else { ?>
    <input type="radio" name="formradio" value="<?=$value?>" id="<?=$value?>" checked/><?=$value?><br>
    <? } ?>
    <? } else { ?>
    <input type="radio" name="formradio" value="<?=$value?>" id="<?=$value?>"/><?=$value?><br>
    <? } ?>
    <? } ?>
    <input type="submit" value="submit"/>
    </form>
    <script type="text/javascript">
    if(errorinmiddle) {
    errorinmiddle = -1;
    window.document.userform.error.value = 1;
    window.document.userform.submit();
    } else if (errorinmiddle == -1) {
    errorinmiddle = 0;
    window.document.userform.error.value = 0;
    window.document.userform.submit();
    }
    </script>
    </body>
    </html>


    kent


  •  

    Posting Permissions

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