PHP单选按钮提交表单

时间:2013-02-06 12:06:54

标签: php post radio-button submit

我一直在创建一个网站,允许用户对其他用户上传的图片进行评分。我的代码工作得很好。向用户显示图像,标记图像的按钮,10个用于选择评级的单选按钮和提交按钮(均在同一表单中)。

我的问题是,当用户点击单选按钮时,我想删除提交按钮并处理表单。这个问题是标志按钮(图像按钮)也在提交表单。我的代码如下:

HTML

<form name="ratebox" action="Box.php?bid=<?php echo $boxId ?>" method="post">
    <table style="margin: 0 auto;">
        <tr>
            <td colspan="2"><img src="img/boxes/<?php echo $boxId ?>.png" title="<?php echo $boxName ?>" height="350" width="350"></td>
        </tr>
    <tr>
    <input
        type="image"
        name="flag_submit"
        src="img/Flag.png"
        onmouseover="this.src='img/Flag_Click.png'"
        onmouseout="this.src='img/Flag.png'"
        height="30"
        width="30"
        />
        </td>
    </tr>
    <tr>
        <td colspan="2" style="text-align:center;">
            1<input type="radio" name="rdoRate" value="1" >
            2<input type="radio" name="rdoRate" value="2" >
            3<input type="radio" name="rdoRate" value="3" >
            4<input type="radio" name="rdoRate" value="4" >
            5<input type="radio" name="rdoRate" value="5" >
            6<input type="radio" name="rdoRate" value="6" >
            7<input type="radio" name="rdoRate" value="7" >
            8<input type="radio" name="rdoRate" value="8" >
            9<input type="radio" name="rdoRate" value="9" >
            10<input type="radio" name="rdoRate" value="10" > 
        </td>
    </tr>
    <tr><td colspan='4' align='center'><input type='submit' name='rate_submit' value='Rate'></td></tr>
</table>
</form>

PHP

if (isset($_POST['flag_submit_x']))
{
        //Code to process the flagged image
}

if (!empty($_POST['rate_submit']))
{
        //Code to process the rated image
}

当按下单选按钮并检索已按下的单选按钮的值时,有什么方法可以提交表单吗?

3 个答案:

答案 0 :(得分:2)

是的,您需要使用Javascript。使用jQuery,

$('input[type=submit]').click(function(){
     return false;
});

$('input[type=radio]').click(function(){
    $('form').submit();
});

您需要学习如何使用$ .ajax()与PHP之间传输信息。 Read this.

答案 1 :(得分:0)

当然有,为什么没有为每个按钮附加一个js函数,并让它将值发送到服务器ajax或常规。

function submitRate(image_id, rating){
//here you would do the magic:)
//maybe grab other form fields or just the rating
}

并在按钮onclick="submitRate()"

答案 2 :(得分:0)

1)删除提交输入。

2)使表单提交为false:

<form id="ratebox" name="ratebox" action="" method="" onSubmit="return false;">

3)将邮政编码放入提交页面:

$.post("Box.php?bid=<?php echo $boxId ?>", $("#ratebox").serialize());
相关问题