Jump to content

filling a form with database information


andyonline2011

Recommended Posts

hi everyone,

Im trying to make a checkout page, where if the user is logged in, the address information is called from the database and fills the form automatically, my problem is i don't know how to fill the php variables with the result of my sql query..sorry to be a complete noob but can someone explain how i would do this?

<?php
if (!defined('WEB_ROOT')
    || !isset($_GET['step']) || (int)$_GET['step'] != 1) {
exit;
}
require_once 'loginfunctions.php';
checkCustomerLogin();
$errorMessage = ' ';
?>

<?php
// get the customer id from the session.
$cu_id = $_SESSION['cu_id']
?>
<?php
//connect to server
$mysqli = mysqli_connect("localhost", "root", "", "onlinestore");
//get default shipping and payment address
$mysql = "SELECT cu_id,od_shipping_first_name,od_shipping_last_name,od_shipping_address1,od_shipping_address2,od_shipping_phone,od_shipping_city,od_shipping_state,od_shipping_postal_code FROM tbl_customer WHERE $cu_id = cu_id";
$result = mysqli_query($mysql,$mysqli);
$row = mysql_fetch_assoc($result);
//populate form with values ?
$txtShippingFirstName = 'test';
$txtShippingLastName = 'test';
$txtShippingAddress1 = 'test';
$txtShippingAddress2 = 'test';
$txtShippingPhone = 'test';
$txtShippingState = 'test';
$txtShippingCity = 'test';
$txtShippingPostalCode = 'test';
?>

<script language="JavaScript" type="text/javascript" src="library/checkout.js"></script>
<table width="550" border="0" align="center" cellpadding="10" cellspacing="0">
    <tr> 
        <td>Step 1 Of 3 : Please Confirm Shipping And Payment Information </td>
    </tr>
</table>
<p id="errorMessage"><?php echo $errorMessage; ?></p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>?step=2" method="post" name="frmCheckout" id="frmCheckout" onSubmit="return checkShippingAndPaymentInfo();">
    <table width="550" border="0" align="center" cellpadding="5" cellspacing="1" class="entryTable">
        <tr class="entryTableHeader"> 
            <td colspan="2">Shipping Information</td>
        </tr>
        <tr> 
            <td width="150" class="label">First Name</td>
            <td class="content"><input name="txtShippingFirstName" type="text" class="box" id="txtShippingFirstName" value="<?php echo $txtShippingFirstName ?>" size="30" maxlength="50"></td>
        </tr>
        <tr> 
            <td width="150" class="label">Last Name</td>
            <td class="content"><input name="txtShippingLastName" type="text" class="box" id="txtShippingLastName" value="<?php echo $txtShippingLastName ?>" size="30" maxlength="50"></td>
        </tr>
        <tr> 
            <td width="150" class="label">Address1</td>
            <td class="content"><input name="txtShippingAddress1" type="text" class="box" id="txtShippingAddress1" value="<?php echo $txtShippingAddress1 ?>" size="50" maxlength="100"></td>
        </tr>
        <tr> 
            <td width="150" class="label">Address2</td>
            <td class="content"><input name="txtShippingAddress2" type="text" class="box" id="txtShippingAddress2" value="<?php echo $txtShippingAddress2 ?>" size="50" maxlength="100"></td>
        </tr>
        <tr> 
            <td width="150" class="label">Phone Number</td>
            <td class="content"><input name="txtShippingPhone" type="text" class="box" id="txtShippingPhone" value="<?php echo $txtShippingPhone ?>" size="30" maxlength="32"></td>
        </tr>
        <tr> 
            <td width="150" class="label">Province / State</td>
            <td class="content"><input name="txtShippingState" type="text" class="box" id="txtShippingState" value="<?php echo $txtShippingState ?>" size="30" maxlength="32"></td>
        </tr>
        <tr> 
            <td width="150" class="label">City</td>
            <td class="content"><input name="txtShippingCity" type="text" class="box" id="txtShippingCity" value="<?php echo $txtShippingCity ?>" size="30" maxlength="32"></td>
        </tr>
        <tr> 
            <td width="150" class="label">Postal / Zip Code</td>
            <td class="content"><input name="txtShippingPostalCode" type="text" class="box" id="txtShippingPostalCode" value="<?php echo $txtShippingPostalCode ?>" size="10" maxlength="10"></td>
        </tr>
    </table>
    <p> </p>
    <table width="550" border="0" align="center" cellpadding="5" cellspacing="1" class="entryTable">
        <tr class="entryTableHeader"> 
            <td width="150">Payment Information</td>
            <td><input type="checkbox" name="chkSame" id="chkSame" value="checkbox" onClick="setPaymentInfo(this.checked);"> 
                <label for="chkSame" style="cursor:pointer">Same as shipping information</label></td>
        </tr>
        <tr> 
            <td width="150" class="label">First Name</td>
            <td class="content"><input name="txtPaymentFirstName" type="text" class="box" id="txtPaymentFirstName" size="30" maxlength="50"></td>
        </tr>
        <tr> 
            <td width="150" class="label">Last Name</td>
            <td class="content"><input name="txtPaymentLastName" type="text" class="box" id="txtPaymentLastName" size="30" maxlength="50"></td>
        </tr>
        <tr> 
            <td width="150" class="label">Address1</td>
            <td class="content"><input name="txtPaymentAddress1" type="text" class="box" id="txtPaymentAddress1" size="50" maxlength="100"></td>
        </tr>
        <tr> 
            <td width="150" class="label">Address2</td>
            <td class="content"><input name="txtPaymentAddress2" type="text" class="box" id="txtPaymentAddress2" size="50" maxlength="100"></td>
        </tr>
        <tr> 
            <td width="150" class="label">Phone Number</td>
            <td class="content"><input name="txtPaymentPhone" type="text" class="box" id="txtPaymentPhone" size="30" maxlength="32"></td>
        </tr>
        <tr> 
            <td width="150" class="label">Province / State</td>
            <td class="content"><input name="txtPaymentState" type="text" class="box" id="txtPaymentState" size="30" maxlength="32"></td>
        </tr>
        <tr> 
            <td width="150" class="label">City</td>
            <td class="content"><input name="txtPaymentCity" type="text" class="box" id="txtPaymentCity" size="30" maxlength="32"></td>
        </tr>
        <tr> 
            <td width="150" class="label">Postal / Zip Code</td>
            <td class="content"><input name="txtPaymentPostalCode" type="text" class="box" id="txtPaymentPostalCode" size="10" maxlength="10"></td>
        </tr>
    </table>
    <p> </p>
    <table width="550" border="0" align="center" cellpadding="5" cellspacing="1" class="entryTable">
      <tr>
        <td width="150" class="entryTableHeader">Payment Method </td>
        <td class="content">
        <input name="optPayment" type="radio" id="optPaypal" value="paypal" checked="checked" />
        <label for="optPaypal" style="cursor:pointer">Paypal</label>
        <input name="optPayment" type="radio" value="cod" id="optCod" />
        <label for="optCod" style="cursor:pointer">Cash on Delivery</label></td>
      </tr>
    </table>
    <p> </p>
    <p align="center"> 
        <input class="box" name="btnStep1" type="submit" id="btnStep1" value="Proceed >>">
    </p>
</form>

Link to comment
Share on other sites

Because I dont have time to look through your code ill just type a quick example.:

<?php
$query = "SELECT * FROM customer_info";
$res = mysql_query($query);
$row = mysql_fetch_array($res);

echo "<input type=\"text\" name=\"name\" value=\"".$row['name']."\" />";
echo "<input type=\"text\" name=\"adress\" value=\"".$row['adress']."\" />";

?>

 

understand?

Link to comment
Share on other sites

//get default shipping and payment address
$mysql = "SELECT cu_id, od_shipping_first_name, od_shipping_last_name, od_shipping_address1,
                 od_shipping_address2, od_shipping_phone, od_shipping_city, od_shipping_state,
                 od_shipping_postal_code
          FROM tbl_customer
          WHERE cu_id = $cu_id";
$result = mysqli_query($mysql, $mysqli);
$address = mysql_fetch_assoc($result);

//populate form with values ?
$txtShippingFirstName  = $address['od_shipping_first_name'];
$txtShippingLastName   = $address['od_shipping_last_name'];
$txtShippingAddress1   = $address['od_shipping_address1'];
$txtShippingAddress2   = $address['od_shipping_address2'];
$txtShippingPhone      = $address['od_shipping_phone'];
$txtShippingCity       = $address['od_shipping_city'];
$txtShippingState      = $address['od_shipping_state'];
$txtShippingPostalCode = $address['od_shipping_postal_code'];

 

You should add some validation/error handling logic to that code. For example, what if the value of $_SESSION['cu_id'] does not exist in the database?

Link to comment
Share on other sites

thanks for replies!

I've tried both suggestions, and no success with either. :shrug:mike, i was ok up until i tried to echo the results within my form,i could'nt work the syntax out.with all the backslashes in your example it looks like what i've seen when a php display block is used,im not using one so i think thats the problem.

mjdamato, i tried what you posted but am getting errors:

Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\wamp\www\store\include\shippingAndPaymentInfo.php on line 22

and

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in C:\wamp\www\store\include\shippingAndPaymentInfo.php on line 23

as for validation, i agree, but that form is only accessible after a successful login,so there should always be a value for cu_id.

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.