Jump to content

populate Combobox from SQL - Im going round in circles here,


MrScabby

Recommended Posts

I really have tried for two days to solve this with a multitude of examples, but for the life of me I cannot get this ********* thing to work...!

 

Its so frustrating - I have a combobox called 'selectedUser' on a HTML form, the form loads, I call the populate.php file(below) but for some reason it fails to select the combobox to insert the option/values (well thats what I suspect is the problem)

Below is the current version of what must be 4 or 5 re-writes I have done in the attempt to do this. I'm here because they ALL fail!!!!!

I now the connect is working and getting the $row's - I changed the PHP code to echo the values out on the background. That worked, so I changed it to populate the combobox and nothing??????

 

<?php
    echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    $link = mysql_connect('localhost','xxxxxxx','xxxxxxx') or die(mysql_error());
    mysql_select_db('xxxxxxxx',$link) or die(mysql_error());
    
    $sql = "SELECT profile_name FROM profiles";
    $rs = mysql_query($sql) or die(mysql_error());
    
    $selectbox = '<select name=\'selectedUser\'>';
    
    while ($row = mysql_fetch_assoc($rs)) {
    $selectbox.='<option value=\"' . $row['profile_name'] . '\">' . $row['profile_email'] . '</option>';
    }

    $selectbox.='</select>';
    mysql_free_result($rs);
    
    echo $selectbox;
?>

 

Restore my faith in myself folks, point something out I haven't seen becuase I have ltterally examined every character but I think I am now code blind and will never  see it!!!!

Edited by MrScabby
Link to comment
Share on other sites

I only changed that to email recently to see if it would change anything, if I change this back to 'profile_name' it makes do difference...

 

$selectbox.='<option value=\"' . $row['profile_name'] . '\">' . $row['profile_name'] . '</option>';

 

this is how I have it now, still no change in output, combobox does not populate.

Edited by MrScabby
Link to comment
Share on other sites

Ok, so I have done as you said, opening the error console beforehand I then reloaded the page and displayed errors in the error console.

I get:

Timestamp: 26/06/2015 20:14:22
Error: SyntaxError: expected expression, got '<'
Source File: http://www.xxxxxxx.co.uk/xxx/deleteUser.php
Line: 207
Source Code:
<select name="selectedUser"><option value="Bugs Bunny">Bugs

then if I click on the error url in the error console it takes me to a line in the source html as follows,

<select name="selectedUser"><option value="Bugs Bunny">Bugs Bunny</option><option value="Donald Duck">Donald Duck</option><option value="Mickey Mousse">Mickey Mousse</option></select></script>

The above line is NOT a part of the HTML, it was highlighted and inserted after the line the error listed, so I take it it is the output -

To get that to work I had to change the selection line to read as follows:-

$selectbox = '<select name="selectedUser">';

for reference the table is populated with

 

'profile_name'

Bugs Bunny

Donald Duck
Mickey Mouse

Edited by MrScabby
Link to comment
Share on other sites

This line is built wrong.  It's hard to tell cause you didn't use the proper forum code tags to post the code (tisk tisk).

// Was this
$selectbox.='<option value=\"' . $row['profile_name'] . '\">' . $row['profile_email'] . '</option>';

// Should be this
$selectbox.='<option value="' . $row['profile_name'] . '">' . $row['profile_email'] . '</option>';

You're using single quotes to open the string but for some reason you tried escaping the double quotes within the string. 

Link to comment
Share on other sites

ok done, I now have some output only it's on the form background,

the output is correct, ie

Bugs Bunny

Donald Duck

Mickey Mouse

only its wrote on the forms background not the combobox whos name='selectedUser'

 

ghads, life just shouldn't be this difficult.......................mind it is the very first time I have done this so maybe it needs to be painful eh!

Edited by MrScabby
Link to comment
Share on other sites

Couldn't tell you any more without your current complete code, including the form code.  Code from the view source would help too, since I have no idea what "it's outputing in the form background" means.  My guess is you have an echo on the <option> tag as you were testing, instead of assigning it to $selectbox.

Link to comment
Share on other sites

Can you explain how the selection process works for the combobox?

 

Ok so we are building a string with the correct instructions for populating the combobox,

So what do I have to do with the string - assuming the string has been built correctly.

 

does simply echo'ing the string execute the string, eg select combobox, add options one by one,,.....

the last line is echo $selectbox, this appears to work in that the string when echoed does list out the names as required just not where we need them, ie it does the names on the backdrop.

 

However it seems to me that the selection of the combobox is not working>...

 

don't quite understand the relationships here ....

 

Just an idea,,,,,,

Could this happen when the php is executed PRIOR to the combobox's construction.....

Am I calling the PHP script too early......hmmm

Edited by MrScabby
Link to comment
Share on other sites

Assuming you're still assigning the option tags to $selectbox .= like your original post, wherever you put this line

 echo $selectbox;

Is where it's going to display.  So if that line is not within the exact spot in the html you want it to show then you need to move it further down the line to where you do want it to show.  Php echo() outputs it's contents at the exact point you have the echo.

Link to comment
Share on other sites

sorry for not coming right back to you, I was so tired I had to sleep - when I returned to it in the morning I solved it.

lol ain't it always the same,

...and so you do not think I messed you about and way of a thanks I will explain HOW the next day people seem to get their eureka moment. As I recently discovered how it works and Im so amazed with it I want to tell folks.

 

Im studying Open University at the moment and I noticed something kept on happening,

I would read a question - think about it, know damn well I had read it and at the time I read it  I understood it but for the life of me I couldn't work out the problem.

Try, try and try again there was no way you was going to get the answer - it was almost like I was searching in the wrong draw of a filing cabinet.

In the end I would get pissed with the whole thing and take the dog for a walk or go into the garden and weed - any job that doesn't require thinking....

when I came in and read the question again BINGO! it would drop into place - It was almost as if I WAS thinking about it all the time even though to me I had thrown all thoughts about the issue out of my mind (that part of it is important, do NOT think about it)

Later in the week I came across a video on YouTube about 'Super conciousness' and it was the very thing that I had found out myself.

apparently it works like this

You consist of 10% of your brain, that is the cognitive processing goes on in the 10% of our brains. the other 90% is a bit of a mystery but they think its mostly autonomous functions like walking, moving, heart, lungs etc

The 10% doesn't have a very good access to memory, it can recollect recent memories but if you take in a large amount of information in a short time span then a lot of short term memory is shunted into long term memory.

The 90% has VERY limited cognitive functions but one thing it can do and do very well is memory retrieval, And apparently it is very very good at this.

so this is how you use it.

 

You read your question, the answer or methodology needs to be already in your head ie you read it and understood it.

You go do some menial task for an hour,

while your doing this task your brain is searching for any relevant memories and pulling them into your short term memory.

You return to the question and BINGO! you get your answer.

 

Try it - I have on multiple occasions and it really does work.

YouTube the term 'Super Conciousness'

 

oh and thanks dude for your time.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.