AndrewTsang
AndrewTsang

Reputation: 316

Cant submit form

Basically I've got a form with 5 radio buttons. No submit button. I want the form to run when the user clicks on any of the radio buttons, so this is what I did.

<input id="5" type="radio" name="star" onchange="this.form.submit();" <?php if ($row["star"] =="5") echo "checked";?> value="5"/>

a querystring is required for the form so I'm using a form action like this

<form name="submit" action="http://example.com/page.php?id=<?php echo $urlid;?>&title=<?php echo $title;?>" method="POST">

and my php is

if ($_POST["submit"]) {
    $rating = $_POST['star'];
    $title = $_GET['title'];
    $verification = ($_GET['verification']);
} else {
    //run the page like usual
}

After testing, I found that onclick, it runs the form action, but on the php side, it goes to "else" where is runs the page like usual instead. Any ideas?

Upvotes: 1

Views: 62

Answers (3)

brainless coder
brainless coder

Reputation: 6430

Since you are checking with -

if ($_POST["submit"]) { // this checks whether there is any item named 'submit' inside the POST or not
} else {
    //run the page like usual
}

The easiest would be to put a hidden item with name submit so that the check validates to true-

<form .. >
     ....
     <input type='hidden' name='submit' value='submit' />
</form>

Upvotes: 0

Jeemusu
Jeemusu

Reputation: 10533

Your PHP is checking if $_POST['submit'] contains a value. Your form does not contain a form element with the attribute name="submit", so therefore it fails and moves straight to the else statement.

If you want to check if the form was posted then you should instead check for:

if (!empty($_POST)) {}

or

if ($_SERVER['REQUEST_METHOD'] == 'POST') {}

Upvotes: 2

adeneo
adeneo

Reputation: 318182

The form element seems to have invalid attributes, missing a quote and space.
It's generally easier to write a little more code, and keep it clearer

<?php
   $url = "http://example.com/page.php?id=". $urlid ."&title=". $title;
?>

<form name="submit" action="<?php echo $url; ?>" method="POST">

Upvotes: 1

Related Questions