0

I'm trying to run a MySQL query depending on what HTML selectbox option is selected and then take the query result and output it to an HTML textbox but to no avail. I'm using the switch statement with the superglobal $_GET. I'm trying to set the value of an HTML textbox using Javascript inside of a PHP block (using echo), but it's not working. The textbox is not being populated (still blank). Could this be because the PHP is running before the textbox is getting initialized or is there something else I'm missing? I know for sure that the DB login info is correct, and the query actually works. The code is below.

<?php
---db login info here---

switch($_GET['string'])
{
    case "another string":
        $query = mysql_query("SELECT field1 FROM table1 WHERE id = '1'");
        $row = mysql_fetch_row($query);
        break;
}

echo "<script type='text/javascript'>";
echo "document.getElementById('textbox').value = '$row[0]'";
echo "</script>";   
?>
4
  • 4
    why are you outputting javascript in order to update an input value that php can handle without js involved? Commented Aug 29, 2012 at 15:59
  • yea...what Jakub said...why not populate the input value directly with PHP? Commented Aug 29, 2012 at 16:04
  • I am new to PHP, so I'm not sure how to populate the input value directly with PHP. Any help would be appreciated. Commented Aug 29, 2012 at 16:16
  • fwiw (going with your existing technique), in order to be thoroughly helpful we should see the HTML of the input you are trying to modify (specifically, does it have id="text box" attribute? Commented Aug 29, 2012 at 16:32

1 Answer 1

2

Couple things:

  1. you can verify that the PHP part is working by checking the "source" of the site when you visit it in the browser (does the <script> part look like you expect?)

  2. Note that the "textbox" must occur earlier in the document than this script, as written

  3. If the element in question is a <textarea>, it does not have a value attribute, and you need to use innerHTML or similar.

  4. There may be bigger-picture issues with how this is being accomplished (is this ajax? all one request? why not simply author the text box with php?)

edit, do it with PHP

If it is your code which authors the <input>, just do it with php in the first place (then you get the added benefit of no js dependency:

switch($_GET['string'])
{
    case "another string":
        $query = mysql_query("SELECT field1 FROM table1 WHERE id = '1'");
        $row = mysql_fetch_row($query);
        $textboxValue = $row[0];
        break;
}
?>
// ...
<input type="text" name="foo" id="textbox" value="<?php echo $textboxValue;?>" />
Sign up to request clarification or add additional context in comments.

18 Comments

These are all good points. The script seems fine when looking at the "source." The HTML textbox is initialized before this PHP script. I'm not using a textarea. I'm using a textbox. I'm not using Ajax. I'm not sure what you mean by "all one request" and "author the textbox with php."
What I mean is, do you have access to the part of your script which does create the input? That's clearly the right place to set it's value.
You'll probably want to use htmlentities on that.
I do. It's just <input id = "textbox" type = "text" readonly = "readonly">. Are you saying to set the value here? I'm not sure how to get the query result and set it to the value attribute here.
Yes. Just move your query block to somewhere above where you make the input. I have demonstrated in (an edit to) my answer how to do it.
|

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.