1

I was wondering in php web page, like

<form method = "post" action = ?>
   Age: <input type = "text" name="age">
   Submit: <input type="submit">
</form>

for example, if the input age larger than 18, then go to page: a.php; else goto b.php

How can we fulfil that, in this current page or do it in the next forwarding page?

More specifically,

  1. if we want to do it in current page,

what I think is action="function()", and inside the function, we do the judgement to go to a.php or b.php. But what is the correct way to write the action and function()? And I saw using onclick="..." as well, what is the difference?

  1. if we want to do it in the next page,

we can write action="c.php" and do the judgement in the c.php page. If so, how to do that in the c.php page?

btw, which way is a common used way?

Thanks, Eve

2
  • 2
    whats the problem of going to the same page, and handling it according to the age value? Commented Aug 7, 2013 at 14:18
  • the second one would be recommended you check for the condition on the second page and then do what ever action you require Commented Aug 7, 2013 at 14:20

6 Answers 6

1

Best way to always submit to the same page.

<form method = "post" action = 'c.php'>
   Age: <input type = "text" name="age">
   Submit: <input type="submit">
</form>

in c.php

$age = $_POST['age'];    
if ($age > 18) 
{
   //do something
} else {
    //do something else
}
Sign up to request clarification or add additional context in comments.

Comments

1

you could change the action attribut with jQuery like:

$( formSelector ).onsubmit(function() {
    if (parseInt($( inputSelector )).val() > 18) {
        $( formSelector ).attr('action', 'over18.php');
     } else {
        $( formSelector ).attr('action', 'under18.php');
     }
}

but it would be better to make it like this:

form action="switchValue.php"

in that file..

if ((int)$_POST['age'] > 18) {
    include("over18.php");
} else {
    include("under18.php");
}

Comments

1

This should be what you are looking for.

Page b.php
<?
if ($_GET['age'] > 18) 
{
    //do some thing
} else {
   //do some thing
}
?>

<form action="b.php">
   Age: <input type = "text" name="age">
   Submit: <input type="submit">
</form>

1 Comment

Did the answer help you?
0
<?
if ($_GET['age'] > 18) 
{
    $page = 'a.php';
} else {
    $page = 'b.php';
}
include $page;
?>

<form>
   Age: <input type = "text" name="age">
   Submit: <input type="submit">
</form>

6 Comments

how will he get age, before submitting?
where are you redirecting the user to page a or b
@amalmurali without action attribute form posts to itself.
action attribute is required for HTML to be valid. However, it can be empty, so form will post to itself. Also, <? usually only works if shorttags are enabled - <?php is the standard way (and yes I know about php 5.4... )
But the server will give an error: Notice:Undefined index: age @Orangepill
|
0

Submit the form to itself by leaving the action attribute empty:

<?php
if(isset($_POST['formSubmit'])) { //if form was submitted

    $age = $_POST['age'];

    if ($age > 18) 
    {
        header('Location: a.php');
    } else {
        header('Location: b.php');
    }

}
?>

<form method = "post" action"">
   Age: <input type = "text" name="age">
   Submit: <input type="submit" name="formSubmit">
</form>

Comments

0

Most likely you want to do it both in the current and in the target page.

Client side check

In the current page, you can use a javascript with a library like jQuery. Add an onclick event to your submit input, calling a function like:

<script type="text/javascript">
   $("input[type=submit]").click(function(event) {
     event.preventDefault(); // avoid normal action, actually not needed but useful sometimes
     if (parseInt($('input[type=text]').val()) < 18)
        $('form').attr('action', 'b.php');
     else $('form').attr('action', 'a.php');
     $('form').submit(); // due to the previous event.preventDefault()
});
</script>

That should do the trick. I suggest you to give ids to your inputs and change the above jQuery selectors accordly.

Server side check

Anyway, people could have javascript disabled or handly modified, so you can add a check even server side. For example, in a.php you can add:

if ((int)$_REQUEST['age'] < 18)
header('Location: b.php?age='.$_REQUEST['age']);

or anything better.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.