0

Any ideas on if and how I can use Mysqli and queries in combination with a PHP switch statement to populate textboxes based on which option of an HTML select box is currently selected? So far I have only been able to get a textbox populated on the initial page load ONLY, and this was ONLY through the default case of the PHP switch statement. My work so far is as follows:

$mysql = new mysqli($db_host, $db_username, $db_password, $db_name)

switch("Not sure what to put here")
{
    case "corresponds with first <option> of select box but not sure how to do this" :
       $result = $mysql->query(---placeholder for SQL query---);

       while ($row = $result->fetch_object())
       {
          $queryResult = $row->---placeholder for column name---;
       }
       $textboxValue = $queryResult;
       break;
}
8
  • doesn't really have anything to do with mysql. Are you posting this information to your page or is it an AJAX call? Commented Sep 6, 2012 at 19:02
  • It does have to do with mysql. The textbox gets populated with data from the DB, and I'm not using AJAX. Commented Sep 6, 2012 at 19:04
  • @programm3r - you're going to have to add more information, like example output. Commented Sep 6, 2012 at 19:07
  • You can use php's echo '<html elements>'; to shape your html page based off of your mysqli results. For example, if($row['isActive'] == 0) { echo '<b> isActive value is 0</b>'; } else { echo '<i>isActive value is 1</i>'; } Commented Sep 6, 2012 at 19:11
  • @andrewsi The only output is just a textbox that gets populated on initial page load ONLY. I'm not sure what else you want me to provide. Commented Sep 6, 2012 at 19:11

1 Answer 1

0

It looks like you will want to use javascript/jquery and an ajax call to a php class that will make database calls and return the data you need to store to the textbox.

Here is an example of an ajax call. You can copy the javascript code and edit it to call your own php class instead.

http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first (yes, it is w3schools.. but this code gets the job done)

Replace the xmlhttp.open("GET","ajax_info.txt",true); link to the php page you want to work with, i.e. xmlhttp.open("GET","loadTextBox.php?id=3",true);

More info: http://www.w3schools.com/ajax/ajax_xmlhttprequest_create.asp

Sign up to request clarification or add additional context in comments.

9 Comments

in your main page that the user sees and chooses a html select option, that page will contain your ajax call. It will also take the returned data from the ajax call (xmlhttp.responseText), and set that to the text of your textbox. You'll want another php page that is a simple php page that the ajax method gets. example, loadTextBox.php , you can add $_GET variables onto the call in ajax, so it could say loadTextBox.php?id=4. Use echo on the loadTextBox.php page to print out the results, which are returned in the xmlhttp.responseText
If it is still confusing, follow the second link for the w3schools tutorial with examples.
I'm confused what goes in the loadTextBox.php page. Are you saying just the script from the w3schools link goes in that page?
You will want to write php code that executes a mysql query to retrieve data from the database. the data will then be echo'd out on the loadTextBox.php page, which will be returned in the xmlhttp.responseText variable.
In the loadTextBox.php page I put in the MySQL query stuff. In the main page I changed the onclick part of the select tag to say onclick = loadDoc() and also added the code from w3schools.com/ajax/tryit.asp?filename=tryajax_first but changed to loadDoc(). I changed document.getElementById("myDiv").innerHTML=xmlhttp.responseText; to document.getElementById("textbox").innerHTML = xmlhttp.responseText; and changed xmlhttp.open("GET","ajax_info.txt",true); to xmlhttp.open("GET","loadTextBox.php?id=3",true); and tried using 4 too, but the textbox doesn't get populated. Any ideas?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.