Hi guys,

Im really hoping someone can shed some light and point me in the right direction, I have a form that works just fine but now im trying to make it so that the customer details section of the form can be automatically filled in by selecting from a drop down a previously submitted customer that is in the database.

I have been kind of in a round about way been able to get the dropdown to populate from the database using dreamweavers many tools and can even get some of the data to transfer into the fields however it only populates the first database entry and doesnt allow it to be changed.

Any help would be greatly appreciated.

Hi.

I stay away from Dreamweaver's overrated tools like the plague... so I won't be able to help you with those.

But I can show you how to do this in PHP code:

<?php
// Establish a DB connection
$dbLink = new mysqli('localhost', 'usr', 'pwd', 'db_name');
if(mysqli_connect_errno()) {
    printf("Database connection failed: %s", mysqli_connect_error());
    exit;
}

// Initialize an array containing posible
// inital values for the form fields
$initalValues = array(
    'name' => '',
    'email' => '',
    'whatever' => ''
);

// Check if a previous user has been selected
// from the dropdown.
($previousID = @$_POST['PreviousCustomer']) or $previousID = null;
if($previousID) {
    // Get the data for this customer.
    $customerID = $dbLink->real_escape_string($_POST['PreviousCustomer']);
    $sql = "SELECT name, email, whatever FROM customer WHERE id = {$customerID}";
    $result = $dbLink->query($sql);

    if($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        $initalValues = $row;
    }
    else {
        user_error("The given customer id did not match any customer.", E_USER_NOTICE);
    }

    @$result->close();
}

// Open the form
echo '<form id="formID" action="otherFile.php" method="post">';

// Print the <select> box.
// Note that I use JavaScript to alter the action
// of the <form> element when an option is selected, 
// so that it reloads this page rather than go on to 
// the submission page, and then submit it.
echo '  <select name="PreviousCustomer"
            onchange="if(this.value != \'\') {
                        document.getElementById(\'formID\').action=\'?\';
                        document.getElementById(\'formID\').submit();
                     }"
        >';
echo '    <option value=\'\'>- Select -</option>';

// Print a list of previous customers as
// options for the <select>
$sql = "SELECT id, name FROM customer";
$result = $dbLink->query($sql);
while($row = $result->fetch_assoc()) {
    $selected = ($row['id'] == $previousID ? 'selected="selected"' : '');
    echo "<option value='{$row['id']}' {$selected}>{$row['name']}</option>";
}

// Close the <select> and print the rest of the form
echo '</select><br />';
echo <<< HTML
    Name: <input type="text" name="name" value="{$initalValues['name']}" /><br />
    Email: <input type="text" name="email" value="{$initalValues['email']}" /><br />
    Whatever: <input type="text" name="whatever" value="{$initalValues['whatever']}" /><br />
    <input type="submit" />
</form>
HTML;

// Close the database link
$dbLink->close();
?>

Thanks for replying I can't seem to get the code you supplied to work, I'm getting parse error with the "echo <<< HTML" line??

That's weird. I copied that code directly from my test server, where it ran fine.

Which version of PHP are you using?
Could you post the exact error here?

Try removing the space between the <<< and HTML.
Making it: echo <<<HTML

Parse error: parse error in C:\wamp\www\powercart\orderentry.php on line 372

// Close the <select> and print the rest of the form
echo '</select><br />';
echo <<<HTML   
	Name: <input type="text" name="name" value="{$initalValues['contact']}" /><br />    
	Email: <input type="text" name="email" value="{$initalValues['email']}" /><br />    
	Whatever: <input type="text" name="whatever" value="{$initalValues['company']}" /><br />    
	<input type="submit" />
</form>
HTML;

Ok so I attached the file I'm working with perhaps it can give a better illustration of what it is im trying to do, the customer database has details for customers already entered so instead of having to re-enter everytime i'm trying to get a dynamic dropdown with the company name in it and when you select it, it fills in the customer fields so the order can be submitted essentially.

I really appreciate all your help.

Ok.

The problem is that you have a bunch of spaces after the heredoc delimiters.
There must be absolutely nothing behind either the echo <<< HTML , nor the HTML; .

Not even a single space.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.