admin管理员组

文章数量:1414614

I am having a form in PHP. I used both onclick and onsubmit to call a javascript function. On onclick i have called the rangeValidator() and onsubmit i have called formValidator(). I am checking the condition and if that is true then it is redirecting to the page which in form action.

Code:

<form action="javascript:check();" name="frm" id="frm" onsubmit="return formValidator()">
.....
     <input type="submit" onclick="return rangeValidator()">
</form>

Javascript function:

<script type="text/javascript">
function rangeValidator()
{
   ......
}

function formValidator()
{
    var s=confirm("Are you sure about all the filled in details?");

    if(s==true)
    {
        return true;
    }
    else
    {
        return false;
    }
}

Now what i want is that, On submit it should check above condition, if that condition is true i want to check the other condition & respectively it should redirect to the page depending on true or false i.e. I want to change the action of the form to two of pages depending on value true or false of condition() function.

To change the action I have coded as below.

function check()
{
    if(confirm('Do you want to see PF share?'))
    {
        url='add_sal_success.php';
        document.addsalsuccess.action = url;
    }
    else
    {   
                url='add_sal_success_wo.php';
            document.addsalsuccess.action = url;
    }
}

I am having a form in PHP. I used both onclick and onsubmit to call a javascript function. On onclick i have called the rangeValidator() and onsubmit i have called formValidator(). I am checking the condition and if that is true then it is redirecting to the page which in form action.

Code:

<form action="javascript:check();" name="frm" id="frm" onsubmit="return formValidator()">
.....
     <input type="submit" onclick="return rangeValidator()">
</form>

Javascript function:

<script type="text/javascript">
function rangeValidator()
{
   ......
}

function formValidator()
{
    var s=confirm("Are you sure about all the filled in details?");

    if(s==true)
    {
        return true;
    }
    else
    {
        return false;
    }
}

Now what i want is that, On submit it should check above condition, if that condition is true i want to check the other condition & respectively it should redirect to the page depending on true or false i.e. I want to change the action of the form to two of pages depending on value true or false of condition() function.

To change the action I have coded as below.

function check()
{
    if(confirm('Do you want to see PF share?'))
    {
        url='add_sal_success.php';
        document.addsalsuccess.action = url;
    }
    else
    {   
                url='add_sal_success_wo.php';
            document.addsalsuccess.action = url;
    }
}
Share Improve this question edited Sep 3, 2013 at 11:15 Anand Jaju asked Sep 3, 2013 at 10:41 Anand JajuAnand Jaju 4583 gold badges12 silver badges28 bronze badges 1
  • If you need to execute rangeValidator() every time before the form is submitted, you may have to add it to the form's event handler too, as somebody can simply hit enter on an input field and the onclick handler is never fired. – insertusernamehere Commented Sep 3, 2013 at 10:48
Add a ment  | 

1 Answer 1

Reset to default 3

Try this use only onsubmit="return check()"

Based on you condition you can set the action

function check()
{
    var f = document.getElementById("frm");
     f.setAttribute('method',"post");
    if(confirm('Do you want to see PF share?'))
    {

         f.setAttribute('action',"yourpage.php");
    }
    else
    {   

         f.setAttribute('action',"yourANOTHERpage.php");
    }
    return true;
}

本文标签: jqueryHow to control form action depending on condition in javascriptStack Overflow