0

I am trying to make a small form that lets the user pick one element from 3 different radiobutton lists to set one element as the users active element (that will be stored to MySQL). Somehow along the way it does not work and I can not seem to figure out why, perhaps someone of you can see what I did wrong?

HTML:

<form name="activeForm1" method="post">
<fieldset data-role="controlgroup">
    <div class="ui-radio">
        <input type="radio" name="active" value="1" id="1">
        <label for="1"></label></input>
    </div>
    <div class="ui-radio">
        <input type="radio" name="active" value="2" id="2">
        <label for="2"></label></input>
    </div>
    <div class="ui-radio">
        <input type="radio" name="active" value="3" id="3">
        <label for="3"></label></input>
    </div>
</fieldset>
<div data-role="footer">
    <input type="submit" href="#" onclick="setActive(1)"/>
</div>
</form>

JavaScript / Ajax call

function setActive(formid) 
    {
            $.ajax(
            {
                type:'POST',
                url:'active.php',
                data:$('#activeForm'+formid).serialize(),
                success:function(response)
                {
                }
            }
        );
    }

PHP code:

session_start();
include('connectToDb.php');
$id = $_SESSION['id'];

if (isset($_POST['active']))
{
    $formValue = $_POST['active'];
    mail('[email protected]','Test',$formValue,'From: [email protected]');
    mysql_query(/* UPDATE MySQL */);
    header("Location: main.php");
}
else
{
    mail('[email protected]','Test','No data recieved!','From: [email protected]');
}

So it works up until the if (isset($_POST['active'])) but then mails me that no data was recieved. I already have 2 similar forms on the same page and they are way bigger and has no problems running. Can't figure out what I did wrong here.

4
  • 1
    Are you making sure that at least one of the radio buttons is checked before clicking the submit button? Commented Jul 27, 2012 at 6:26
  • Could you please add a var_dump($_POST['active']) ? Commented Jul 27, 2012 at 6:27
  • why href attribute to input element?? Commented Jul 27, 2012 at 6:31
  • @asprin no. Dunno how to do it.. yet :) Commented Jul 27, 2012 at 6:39

3 Answers 3

2

Wrong code :

  data:$('#activeForm'+formid).serialize(),

#activeForm is not an id, it is the name of the form tag,

Correct the form tag to,

<form name="activeForm1" id="activeForm1" method="post">
Sign up to request clarification or add additional context in comments.

2 Comments

Damn..How did i miss that? Nice catch
Aaaaah! That makes perfect sense! Cheers! Been bugging me for hours now, can't believe I missed that!
2

Replace following line

data:$('#activeForm'+formid).serialize(),

with

data: $('form[name="activeForm'+formid+'"]').serialize(),

Comments

0

change

<input type="submit" href="#" onclick="setActive(1)"/>

to

<input type="button" href="#" onclick="setActive(1)"/>

and then it should work

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.