MySQL Forums
Forum List  »  PHP

Re: Select drop down list is only displaying first choice results
Posted by: Douglas Moxley
Date: September 10, 2017 08:31PM

Hi Peter,

Do you do any freelance work on the side? You could probably solve my problem in an hour. I've worked on this for days, read and read until my eye bled about forms and select options but I cannot get my script to function properly displaying any other entry than the first one, i.e., if will only retrieve data from the first entry. Using $row['blah_blah'] works just fine but when I attempt to loop to display a different user to the screen it doesn't work. I'm pasting a working copy for a single $row entry below.

Thanks,
Doug


// Database: players
// Rows: 3
// Columns: id, first_name, last_name
// Entries:
// # Name Type
// 5 Jim Palmer
// 6 Brooks Robinson
// 8 Cal Ripken



<html>
<head>

<!-- Download the below jquery.js or link to a copy -->
<script type="text/javascript" src="jquery.js"></script>

<script type='text/javascript'>
$(function() {
$('#player').change(function() {
selectedOption = $("option:selected", this)
$('input[name=first_name]').val( selectedOption.data('first_name') );
$('input[name=last_name]').val( selectedOption.data('last_name') );
});
});
</script>

</head>
<body>
</body>
</html>

<?php

require('dbconnect.php'); // This file contains credentials

// Create Connection
$dbLink = new mysqli($DBHOST, $DBUSER, $DBPASS, $DBNAME);

// Declarations
$first_name = mysqli_real_escape_string($dbLink, $_GET['first_name']);
$last_name = mysqli_real_escape_string($dbLink, $_GET['last_name']);

// Query the database
$query = "SELECT * FROM players";
$result = mysqli_query($dbLink, $query);

$row = mysqli_fetch_array($result);

echo "<option>" . $row{'player'} . "</option>";

// Close php session
?>

<!-- Align output -->
<html>
<body>
<table>
<tr>

<!-- Form with embedded Select -->
<form action="test.php" method="get" name=player" enctype="multipart/form-data">
<select id="player">
<option value="">Select a Player to Display</option>
<option value=""
data-first_name="<?php echo $row['first_name']; ?>"
data-last_name="<?php echo $row['last_name']; ?>"
><?php echo $row['first_name'], " " , $row['last_name']; ?></option>
</select>
<td><strong>First Name: </strong><br /><input type="text" name="first_name"></td />
<td><strong>Last Name: </strong><br /><input type="text" name="last_name"><td />
</form>
</tr>
</table>
</body>

</html>

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.