0

Ok below I will link a screenshot to show you what I'm working with so far.

In the left box I was to fill it with a list of 'EventIDs' which are the primary key on my database table.

When one of these IDs is selected I want the 'EventName' from the same table to appear in the box to the right.

https://gyazo.com/79e5e49bd288838c8ce1ae54fe245494

The problem so far is that the Event name is showing in the left select box and I'm not sure how to make it fetch data from the table when selected and to output in the text box. Hope that makes sense

Here is my code:

<!DOCTYPE html>
<html>
<head> </head>
<body>

<?php include 'login.php';?>

<select name="EventID">
<option value="">Select Event ID</option>

<?php

$result1 = mysqli_query($dbconnect, "SELECT EventID, EventName FROM event");

       while($row1 = mysqli_fetch_assoc($result1)) {
       ?>

<option value="<?php echo $row1['EventID']; ?>"><?php echo $row1['EventName']; ?></option>
<?php
// End while loop.
}
?>
</select>

Event Name: <input name="EventName" type="text">

</body>
<html>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Xander
  • 991
  • 1
  • 13
  • 32
  • You'll need to have a `form` submit, or make an AJAX request with the selected data. (or I think) I'm not really clear on what data you are trying to display but if it isn't on the DOM then you'll need to make a request to the server for the data. – chris85 May 03 '16 at 18:55
  • Use AJAX for that http://stackoverflow.com/questions/5298401/basic-php-and-ajax – Aman Rawat May 03 '16 at 19:00
  • @chris85 So if I created it in a form and when the user clicks submit the EventName that relates to the ID selected would appear? That would be fine aswell if you know how, basically I just want to display data that relates to the ID that the user selects from the left box – Xander May 03 '16 at 19:05
  • What displays currently? Isn't `` the name? – chris85 May 03 '16 at 19:07
  • @chris85 Yeah but I want the select box to be populated with the IDs. (EventID). Then when the user selects one of the IDs from the box, the relating EventName will appear in the box on the right. Does that make sense? cause I'm doubting myself now and not sure if i'm being stupid – Xander May 03 '16 at 19:09
  • Start by learning some HTML http://www.html-form-guide.com/php-form/php-form-tutorial.html – RiggsFolly May 03 '16 at 19:10
  • @AmanRawat The OP is struggling with `
    ` Do you really think that AJAX is what he needs to struggle with????
    – RiggsFolly May 03 '16 at 19:14

1 Answers1

1
<!DOCTYPE html>
<html>
<head> </head>
<script>
function choice1(select) {
    document.getElementById("EventName").value = select.options[select.selectedIndex].value;
}
</script>
<body>

<?php include 'login.php';?>

<select name="EventID" onchange="choice1(this)">
<option value="">Select Event ID</option>

<?php

$result1 = mysqli_query($dbconnect, "SELECT EventID, EventName FROM event");

       while($row1 = mysqli_fetch_assoc($result1)) {
       ?>

<option value="<?php echo $row1['EventName']; ?>"><?php echo $row1['EventID']; ?></option>
<?php
// End while loop.
}
?>
</select>

Event Name: <input name="EventName" id="EventName" type="text">

</body>
<html>
Jah
  • 986
  • 5
  • 26
  • This OP does not even know about `
    ` and you think javascript is a good idea for his first port of call????
    – RiggsFolly May 03 '16 at 19:12
  • true but it's some where for him to start, not sure what he really wants to achieve, besides show selected to input. – Jah May 03 '16 at 19:19
  • @RiggsFolly I have used form for my Adding to database, just didn't know if it was necessary here or not. – Xander May 03 '16 at 19:51
  • @Jah Yes thank you! Almost there, the only problem remaining is that the box on the left is still populated with Event names, I wan't the IDs from 'EventID' table to be listed instead. – Xander May 03 '16 at 19:54
  • listed where? maby this? change EventName to EventID – Jah May 03 '16 at 20:03
  • @Jah Hmm, the problem is now that when I select an ID, the box on the right displays the ID aswell. I want the right box to display the EventName name still – Xander May 03 '16 at 20:12
  • check my answer i made adjustments. – Jah May 03 '16 at 20:36
  • @Jah I already tried that way and it didn't work but when I changed the .text to .value it worked@!! Thank you :D legend – Xander May 03 '16 at 20:46
  • @Ethan Bristow no problem. – Jah May 03 '16 at 20:47