2013-02-06 55 views
0

我一直在创建一个网站,允许用户对其他用户上传的图片进行评分。我的代码工作得很好。用户会看到图像,一个标记图像的按钮,10个单选按钮来选择他们的评分和一个提交按钮(都在同一个表单中)。PHP RadioButton提交表格

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

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 
} 

有什么办法当按下单选按钮,我可以提交表单和检索的价值已按下的单选按钮?

+0

在HTML中你需要一个按钮来发送数据。你需要为此使用js – nowhere

+0

我正在使用PHP和HTML一起使用。 –

回答

2

是的,你需要使用JavaScript。使用jQuery,

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

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

您需要了解如何使用$ .ajax()将信息传输到PHP或从PHP传输信息。 Read this.

+0

我将如何处理和检索在PHP代码中发送的值? –

+0

@GlenRobson查看更新 –

+0

我试过这种方法,我无法让它工作。你能提供一个例子吗? –

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()"

+0

我如何将信息从js发送到PHP以及如何处理和检索PHP中的值? –

+0

在您的submitRate()发送ajax调用到后端php脚本并返回结果 – phpalix

0

1)删除提交输入。

2)使表单提交虚假:

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

3)将后置代号为提交页面:

$.post("Box.php?bid=<?php echo $boxId ?>", $("#ratebox").serialize()); 
+0

这不会工作,因为我在同一个表单中使用图像按钮(标记按钮)来提交请求。 –