Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top