dongxia2068 2015-12-11 08:33
浏览 59
已采纳

php - 从表单中检查外部数组

I've created a form in html. in this form you can specify the operating system. The form looks like this:

<input type="hidden" name="toshibaproduct" value="001"/>
<input type="hidden" name="toshibamerk" value="toshiba"/>
<input type="hidden" name="toshibamodel" value="Sattelite A100-510"/>
Operating system <select name="beschikbaarheid" value="true">
    <option value="xp">Windows XP</option>
    <option value="vista">Windows Vista</option>
    <option value="linux">Linux</option>
</select>
Aantal: <input type="text" size=2 maxlength=3 name="toshibaaantal" value="0"/>
<input type="hidden" name="toshibaprijs" value="999.99"/>
<input type="image" src="bestel.jpg" border=0 value="bestellen"/>

I've also created an external function which i have included in the page which looks like this:

<?php
function beschikbaarheid($merk, $os)
{
    $beschikbaar = array(
        "Toshiba" => array("xp" => true, "vista" => false, "linux" => true),
        "Acer" => array("xp" => true, "vista" => true, "linux" => true),
        "Hp" => array("xp" => true, "vista" => false, "linux" => true));
    return ($beschikbaar[$merk][$os]);
}
?>

My plan is to let the form check in the external function if an operating system is available or not, and if not return a message that it's sold out. if it's available it should just continue with the rest of the script (which works btw, but it not included here).

Thing is, i don't know how to make the form check the array in the external function. I am also wondering if it's a problem that only the operatings system is checked in an external function and not the rest of the form like the quantity for instance.

  • 写回答

3条回答 默认 最新

  • dongliang1223 2015-12-11 09:50
    关注

    Updated HTML

    <head>
        <!-- include jQuery -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    </head>
    
    <input type="hidden" name="toshibaproduct" value="001" />
    <input type="hidden" name="toshibamerk" value="toshiba" />
    <input type="hidden" name="toshibamodel" value="Sattelite A100-510" />
    Maker
    <select name="maker" value="true" id="maker">
        <option value="toshiba">Toshiba</option>
        <option value="acer">Acer</option>
        <option value="hp">Hp</option>
    </select>
    Operating system 
    <select name="beschikbaarheid" value="true" id="operationSystem">
        <option value="xp">Windows XP</option>
        <option value="vista">Windows Vista</option>
        <option value="linux">Linux</option>
    </select>
    Aantal: <input type="text" size=2 maxlength=3 name="toshibaaantal" id="quantity" value="0" />
    <input type="hidden" name="toshibaprijs" value="999.99" />
    <input type="button" value="bestellen" id="submitData" />
    
    <span id="messages"></span>
    

    Ajax

    <!-- Ajax -->
    <script type="text/javascript">
    $('#submitData').click(function(){
        var selectedOs = $('#operationSystem').find('option:selected').val();
        var selectedMaker = $('#maker').find('option:selected').val();
        var selectedQuantity = $('#quantity').val();
        if(selectedQuantity == 0 || selectedQuantity == ''){
            $('#messages').text('Please provide valid information');
            return false;
        }
       // You can perform an ajax request using the .ajax() method
        $.ajax({
            type: 'POST',
            url: 'beschikbaarheid.php', // This is the url that will be requested
            data: {operation_system: selectedOs, maker: selectedMaker, quantity: selectedQuantity},
            success: function(data){ 
                if(data){
                    $('#messages').text('Available');
                }else{
                    $('#messages').text('Not available');
                }
            },
        });
    
    });
    </script>
    

    Server-side script (beschikbaarheid.php)

    <?php
    if(isset( $_POST['quantity'] )) {
        $operatingSystem  = $_POST['operation_system'];
        $maker  = $_POST['maker'];
        $quantity  = $_POST['quantity'];
        echo beschikbaarheid($operatingSystem, $maker, $quantity);
    }
    function beschikbaarheid($operatingSystem, $maker, $quantity)
    {
        // I did't used the quantity section
        // Please use it if needed
        // Also this structure is not much prefered, I just used this becasuse you might have some out put like this.
        $beschikbaar = array(
            "toshiba" => array ("xp" =>false, "vista" => false, "linux" => true), 
            "hp" => array ("xp" =>true, "vista" => false, "linux" => true),
            "acer" => array ("xp" =>true, "vista" => false, "linux" => true)
        ); 
        return $beschikbaar[$maker][$operatingSystem];
    }
    ?>
    

    There are different ways to achieve this, I hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错