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('my@mail.com','Test',$formValue,'From: dummy@mail.com');
    mysql_query(/* UPDATE MySQL */);
    header("Location: main.php");
}
else
{
    mail('my@mail.com','Test','No data recieved!','From: dummy@mail.com');
}

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

3 に答える 3

2

不正なコード :

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

#activeFormIDではなく、フォームタグの名前です。

form タグを次のように修正します。

<form name="activeForm1" id="activeForm1" method="post">
于 2012-07-27T06:31:19.667 に答える
2

次の行を置き換えます

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

data: $('form[name="activeForm'+formid+'"]').serialize(),
于 2012-07-27T06:31:51.160 に答える
0

変化する

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

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

そして、それはうまくいくはずです

于 2012-07-27T06:26:58.463 に答える