如果选择了PHP选项,则在其他选择中无法再次选择所选选项

时间:2015-01-05 19:33:46

标签: php mysql

您好我的名字是帕特里克,这是我的第一个问题,对不起,但我的PHP不是很好。可能有更多的改进,但这篇文章是针对问题的。 (但也欢迎改进)

问题:

你可以选择一个由2个怪物组成的团队//怪物是从数据库中选择的

问题是:如果你选择1个怪物我怎么能解决你不能在选项2上选择相同的怪物?

PHP代码:

2个汇款按钮的操作

<?php
session_start();
include("header.php");

if(!isset($_SESSION['uid'])){
    echo "You must be logged in to view this page!";
}else{
    if (isset($_POST['save'])) {
    if ($_POST['save'] == 'keuze4') {

        $fuelQuery4 = sprintf("UPDATE user_team SET `m_keuze4` = '%s' WHERE `id`='".$_SESSION['uid']."'  ",
                        mysql_real_escape_string($_POST['option4']));
        $Result = mysql_query($fuelQuery4);
            if($Result){
            echo 'Team is aangepast!';
            }


    } elseif ($_POST['save'] == 'keuze5'){

        $fuelQuery5 = sprintf("UPDATE user_team SET `m_keuze5` = '%s' WHERE `id`='".$_SESSION['uid']."'  ",
                        mysql_real_escape_string($_POST['option5']));   
        $Result = mysql_query($fuelQuery5);
            if($Result){
            echo 'Team is aangepast!';
            } 
    }

    echo '';}   

?>

获取怪物表单数据库并将其放入选择列表

<?php
 $get=mysql_query("SELECT * FROM user_monsters WHERE `id`='".$_SESSION['uid']."' ORDER BY usid ASC");
$option4 = '';
 while($row = mysql_fetch_assoc($get))
{
  $option4 .= '<option value = "'.$row['usid'].'">'.$row['usid'].' - '.$row['monster'].' - '.$row['type'].'</option>';
}
?>

显示所选项目

<?php

 $k4 = mysql_query("


SELECT user_team.m_keuze4, user_monsters.usid, user_monsters.monster, user_monsters.type, user_monsters.attack, user_monsters.defense
FROM user_team
INNER JOIN user_monsters
ON user_team.m_keuze4=user_monsters.usid
ORDER BY user_monsters.type;

 ");
 while($row4 = mysql_fetch_assoc($k4))
 {
 $k4_1 = ''.$row4['m_keuze4'].' - '.$row4['monster'].' - '.$row4['type'].' - '.$row4['attack'].' - '.$row4['defense'].'';
 }

?>

选项5与4的代码相同:

    <?php
 $get=mysql_query("SELECT * FROM user_monsters WHERE `id`='".$_SESSION['uid']."' ORDER BY usid ASC");
$option5 = '';
 while($row = mysql_fetch_assoc($get))
{
  $option5 .= '<option value = "'.$row['usid'].'">'.$row['usid'].' - '.$row['monster'].' - '.$row['type'].'</option>';
}
?>

<?php

 $k5 = mysql_query("


SELECT user_team.m_keuze5, user_monsters.usid, user_monsters.monster, user_monsters.type, user_monsters.attack, user_monsters.defense
FROM user_team
INNER JOIN user_monsters
ON user_team.m_keuze5=user_monsters.usid
ORDER BY user_monsters.type;

 ");
 while($row5 = mysql_fetch_assoc($k5))
 {
 $k5_1 = ''.$row5['m_keuze5'].' - '.$row5['monster'].' - '.$row5['type'].' - '.$row5['attack'].' - '.$row5['defense'].'';
 }

?>

表格

<form action="team.php" method="post">

 <select name="option4"> 
<?php echo $option4; ?>
</select><br><br>Keuze 4  
<?php 
 echo $k4_1;
  ?><br><br>
<input type="submit" name="save" value="keuze4"/>
</form>

<form action="team.php" method="post">
 <select name="option5"> 
<?php echo $option5; ?>
</select><br><br>Keuze 5  
<?php 
 echo $k5_1;
  ?><br><br>
<input type="submit" name="save" value="keuze5"/>
</form>

1 个答案:

答案 0 :(得分:2)

在php中,你可以选择最好的选项:

if (isset($_POST['save'])) {
  if (filter_input(INPUT_POST,'option4') == filter_input(INPUT_POST,'option5')){
    echo "Sorry. You can't select the same monster twice";
  }else{
    //your db insert logic goes here
  }
}

在提交表单之前,还应包含一些javascript来提醒用户,这是一个好主意。这个例子使用jQuery

$('[name="option4"],[name="option5"]').change(function(){
   if  ($('[name="option4"]').val() == $('[name="option5"]').val()){
      alert('you already chose that monster, please choose another');
   }
});

表格

<form action="team.php" method="post">

    <select name="option4"> 
        <?php echo $option4; ?>
    </select><br><br>Keuze 4  
    <?php
    echo $k4_1;
    ?><br><br>
    <input type="submit" name="save" value="keuze4"/>
</form>                                 <!-- remove this line-->

<form action="team.php" method="post">  <!-- and    this line-->
    <select name="option5"> 
        <?php echo $option5; ?>
    </select><br><br>Keuze 5  
    <?php
    echo $k5_1;
    ?><br><br>
    <input type="submit" name="save" value="keuze5"/>
</form>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<script>
    $(function () {
        $('[name="option4"],[name="option5"]').change(function () {
            if ($('[name="option4"]').val() == $('[name="option5"]').val()) {
                alert('you already chose that monster, please choose another');
            }
        });
    });
</script>

2个汇款按钮的操作

 if (isset($_POST['save'])) {
        if (filter_input(INPUT_POST, 'option4') == filter_input(INPUT_POST, 'option5')) {
            echo "Sorry. You can't select the same monster twice";
        } else {
            if ($_POST['save'] == 'keuze4') {

                $fuelQuery4 = sprintf("UPDATE user_team SET `m_keuze4` = '%s' WHERE `id`='" . $_SESSION['uid'] . "'  ", mysql_real_escape_string($_POST['option4']));
                $Result = mysql_query($fuelQuery4);
                if ($Result) {
                    echo 'Team is aangepast!';
                }
            } elseif ($_POST['save'] == 'keuze5') {

                $fuelQuery5 = sprintf("UPDATE user_team SET `m_keuze5` = '%s' WHERE `id`='" . $_SESSION['uid'] . "'  ", mysql_real_escape_string($_POST['option5']));
                $Result = mysql_query($fuelQuery5);
                if ($Result) {
                    echo 'Team is aangepast!';
                }
            }
        }
    }

再次修改

Demo Fiddle js