dqqyp90576 2014-02-28 21:51
浏览 116

如何$ _POST从PHP类别列表函数中选择值?

I have a category list as a PHP function. I called this function in a form to display the options. However I cannot get the form to post the selected option to my database. I have searched SO and online but most questions relate to the values being a select option, which would be easier. However my list is quite long and may change. The other values from the form are sent to the database correctly, apart form the pictures which I am working on, however I cannot find a way to insert the selected value from the drop down list within the form.

This is my simple sql query to send information from the form to the database:

if (isset($_POST['submit']))
{
    $title = $_POST['title'];
    $subtitle = $_POST['subtitle'];
    $description = $_POST['description'];
    $category= $_POST['category'];
    $pic1 = $_POST['pic1'];
    $pic2 = $_POST['pic2'];
    $pic3 = $_POST['pic3'];
    $pic4 = $_POST['pic4'];
    $pic5 = $_POST['pic5'];
    $item_location = $_POST['location'];
    $items_wanted = $_POST['wanted'];
    mysql_query("INSERT INTO items_available (title, subtitle, description, category, image_path1, image_path2, image_path3, image_path4, image_path5, item_location, items_wanted) VALUES ('$title', '$subtitle', '$description', '$category', '$pic1', '$pic2', '$pic3', '$pic4', '$pic5', '$item_location', '$items_wanted')")or die(mysql_error());
    header('Location: prompt.php?x=8');
}

This is the code for the field for category selection from my form which is a drop down list:

<div class="field">
    <label for="category">Category:</label>
    <select id=\"category\" name=\"category\" class=\"searchBox\">
        <?php createCategoryList(); ?>
    </select>
</div>

This is the code for the category list function:

//Creates Category <option>'s for search bar
function createCategoryList(){
if( ctype_digit($_GET['category']) ){ $x = $_GET['category']; }else{ $x = 999; }
echo "<option>All Categories</option>";
$i=0;
while(1){
    if(numberToCategory($i)=="Category Does Not Exist"){
        break;
    }else{
        echo " <option value=\"$i\" ";
        if($i==$x){echo ' SELECTED ';}
        echo " > ";
        echo numberToCategory($i);
        echo "</option>";
    }
    $i++;
}
}

//Category Number to String
function numberToCategory($n){
switch($n){
case 0:
    $cat = "Antiques";
    break;
case 1:
    $cat = "Art";
    break;
case 2:
    $cat = "Automotive";
    break;
case 3:
    $cat = "Baby";
    break;
case 4:
    $cat = "Books";
    break;
case 5:
    $cat = "Business & Industrial";
    break;
case 6:
    $cat = "Cameras & Photo";
    break;
case 7:
    $cat = "Clothing & Accessories";
    break;
case 8:
    $cat = "Collectibles";
    break;
case 9:
    $cat = "Computers";
    break;
case 10:
    $cat = "Crafts";
    break;
case 11:
    $cat = "DVD's & Movies";
    break;
case 12:
    $cat = "Electronics";
    break;
case 13:
    $cat = "Health & Beauty";
    break;
case 14:
    $cat = "Home & Garden";
    break;
case 15:
    $cat = "Jewelry & Watches";
    break;
case 16:
    $cat = "Music";
    break;
case 17:
    $cat = "Pet Supplies";
    break;
case 18:
    $cat = "Services";
    break;
case 19:
    $cat = "Sports & Outdoors";
    break;
case 20:
    $cat = "Sports Memorabilia & Cards";
    break;
case 21:
    $cat = "Tools & Home Improvement";
    break;
case 22:
    $cat = "Toys & Hobbies";
    break;
case 23:
    $cat = "Video Games";
    break;
case 24:
    $cat = "Other";
    break;
default:
    $cat = "Category Does Not Exist";
}

Currently the numerical value of the selected option is passed i.e if I select the baby option from the drop down list, then the number 3 is entered into my database. How do I replace this numerical value with it's corresponding value as it appears on the drop down list?

All and any answers are gratefully appreciated.

  • 写回答

2条回答 默认 最新

  • dongyi1111 2014-02-28 21:59
    关注

    just use echo " <option value=\"numberToCategory($i)\" "; in place of echo " <option value=\"$i\" ";

    Other suggestions :

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?