douwen2072 2014-04-29 19:14
浏览 27
已采纳

在下面的其他选择中使用表单选择

I am currently busy creating a very small, insanely basic document management system, that we plan to use to manage our Documentation for software/hardware troubleshooting in the company.

I know or have read about that what I want to accomplish here can be done with AJAX/jquery and possibly other stuff as well... but I am trying to keep it as simple as possible. And on that subject I also read that this or something similar that I tried cannot be done using php alone.

My other option is to create 3 seperate pages, 1 for each selection, and then on last page you can add/upload a word document to the database with all the required fields.

Trying to keep it on one page if possible... Any suggestions on how to accomplish this without using fancy AJAX or JQUERY codes?

Here is the code i have so far... I have not added the actual form tags yet... as I first want to see what suggestions you guys come up with :)

    <h2 class="gap-1">Select Software</h2>
<p>
<select name="software">
        <option selected>Select Software/Application</option>
            <?php
$host = "****";
$db_user = "****";
$db_pass = "****";
$db = "****";
            $link=mysql_connect($host, $db_user, $db_pass) or die ("Error connecting to mysql server: ".mysql_error());
            mysql_select_db($db, $link) or die ("Error selecting specified database on mysql server: ".mysql_error());

            $query1="SELECT * FROM software";
            $result1=mysql_query($query1) or die ("Query to get data from software Table failed: ".mysql_error());

while ($row=mysql_fetch_array($result1)) {
$software_id=$row["software_id"];
$software_name=$row["software_name"];
    echo "<option value=\"$software_id\">$software_name</option>";
}

            ?>
</select>
</p>
<br />
<h2 class="gap-1">Link to Error/Problem</h2>
<p>
<select name="software">
        <option selected>Select Error/Problem</option>
            <?php
$host = "****";
$db_user = "****";
$db_pass = "****";
$db = "****";
            $link=mysql_connect($host, $db_user, $db_pass) or die ("Error connecting to mysql server: ".mysql_error());
            mysql_select_db($db, $link) or die ("Error selecting specified database on mysql server: ".mysql_error());

            $query1="SELECT * FROM errors WHERE software_id = 3";
            $result1=mysql_query($query1) or die ("Query to get data from software Table failed: ".mysql_error());

while ($row=mysql_fetch_array($result1)) {
$error_id=$row["error_id"];
$error_discription=$row["error_discription"];
    echo "<option value=\"$error_id\">$error_discription</option>";
}

            ?>
</select>
</p>
<br />
<h2 class="gap-1">Add Document</h2>
<p>
<input type="file">
</p>

If I do the seperate page thing, I'll have one page to select the Software, and another to select the error, and then on the last page upload doc to database and add the software and error id where necessary.

  • 写回答

1条回答 默认 最新

  • duanqiechui2378 2014-04-29 19:49
    关注

    First, as I said in a comment already, mysql_ functions are deprecated and may stop working in later PHP releases, so you should be using PDO for new development.

    But doing what you want is as simple as using javascript to submit the form on change of the selection:

    <form name='formname' action='theSamePage.php' method='get'>
      <select name='whatever' onChange='document.formname.submit();'>
    

    And getting the WHERE clause right in your SQL. If the previous selection is not set, you don't even run the SQL or print the next dropdown's options, and it does, you do.

    Also, as you loop through the options from the db to print them out, check if the current option from the db equals the one received in the request (if its set) and if it does, instead of printing just <option value='x'>x</option> print <option value='x' selected='true'>x</option> to keep the option selected when the page refreshes.

    Take the selected out of <option selected>Select Error/Problem</option> since that will make the blank option always selected. If no selection is chosen yet, the first one will always be seen as selected anyway by the way the browser works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)