Jump to content

Auto updating form text fields


englishder

Recommended Posts

Here goes.

I am trying to retrieve info. from mySql database and update 3 textfields.

I also have 4 selects on the form that need populating during the running of the program. My problem is using console I can see the info - Customer name - but it does not appear in the text field. The onchange request in the DIV container seems to be where it fails. Here are the files I am using:

 

1. add_an_order.php

 

<html>
<head>

<title>Add an Order</title>
     
<link href = "css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="include/jquery-1.11.1.min.js"></script>

</head>

<body>
    
<div id="header"><img src="images/logo.png" /></div>

<div id="nav">
    
    <div id="nav_wrapper">
        <ul>
            <li><a href="index.php">Orders</a></li><li>
            <a href="customers.php">Customers</a></li><li>
            <a href="contacts.php">Contacts</a></li><li>
            <a href="batteries.php">Batteries</a></li><li>
            <a href="#">Queries</a>
            </li>
        </ul>
    </div>
    
</div>

<div id="main">

<?php

echo '<h3 id="menOpt">Add an Order</h3><hr>';

// Check for a form submission.
/*
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        
    $errors = array();
    
    $required_fields = array('customer_name', 'customer_address', 'city', 'telephone');
    foreach ($required_fields as $fieldname) {
        if (!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
            $errors[] = strtoupper($fieldname);
        }
    }
    
    if (empty($errors)) {
            
        include ('include/dbconnect.php');
        
        $name = mysqli_real_escape_string($con, trim(strip_tags($_POST['customer_name'])));
        $contact = mysqli_real_escape_string($con, trim(strip_tags($_POST['contact'])));
        $address = mysqli_real_escape_string($con, trim(strip_tags($_POST['customer_address'])));
        $city = mysqli_real_escape_string($con, trim(strip_tags($_POST['city'])));
        $telephone = mysqli_real_escape_string($con, trim(strip_tags($_POST['telephone'])));
        $telephone = preg_replace('/[^0-9]/', '', $telephone);
        
        if (isset($_POST['deepc'])) {
            $deepc = 1;
        } else {
            $deepc = 0;
        }
        
        if (isset($_POST['problemc'])) {
            $problemc = 1;
        } else {
            $problemc = 0;
        }
        
        $problem = mysqli_real_escape_string($con, trim(strip_tags($_POST['problem'])));
        
        if (isset($_POST['blacklist'])) {
            $blacklist = 1;
        } else {
            $blacklist = 0;
        }
        
        if (isset($_POST['pickup'])) {
            $pickup = 1;
        } else {
            $pickup = 0;
        }
        
        
        $query = "INSERT INTO customers (
                    customer_name, contact, customer_address, city, telephone, deep_cycle,
                    problem_customer, problem, blacklist, pickup)
                  VALUES ('$name', '$contact', '$address', '$city', '$telephone', $deepc,
                    $problemc, '$problem', $blacklist, $pickup)";
        
        $r = mysqli_query($con, $query);
        
        if (mysqli_affected_rows($con) == 1) {
                
            echo '<p class="success">Customer has been successfully added.</p>';
            
        } else {
                
            $message = "Could not add customer.";
            $message .= "<br />" . mysqli_error($con);
            
        }
        
        mysqli_close($con);
        
    } else {  // We have errors.
        
        $message = count($errors) . " error(s) on the form.";
        
    }
    
} // End: if ($_SERVER['REQUEST_METHOD'] == 'POST').

*/

// Leave PHP and display the form.
?>

<?php if (!empty($message)) {
    echo '<p class="error">' . $message . '</p>';
} ?>
<?php
// Output list of fields that have errors.
if (!empty($errors)) {
    echo '<p class="error">';
    foreach ($errors as $error) {
        echo $error . '<br />';
    }
    echo '</p>';
}

?>

<div class="container">
    <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
        <fieldset>
            <label>Business: <span class="important"> *</span><select name="customer_name" id="customer_name" onchange="request_fill(this.value)"
                value="<?php if (isset($_POST['customer_name'])) echo $_POST['customer_name']; ?>" ></select></label>
            <label>Address: <input type="text" name="address" id="add_a"
                value="<?php if (isset($_POST['customer_address'])) echo $_POST['customer_address']; ?>" /></label>
            <label>City: <input type="text" name="city" id="city"
                value="<?php if (isset($_POST['city'])) echo $_POST['city']; ?>" /></label>
            <label>Phone: <input type="text" name="telephone"
                value="<?php if (isset($_POST['telephone'])) echo $_POST['telephone']; ?>" /></label>

            <label>Manufacturer: <span class="important"> *</span><select name="manufacturer" id="manufacturer" onchange="request_mod(this.value)"
                value="<?php if (isset($_POST['manufacturer'])) echo $_POST['manufacturer']; ?>" ></select></label>

            <label>Model: <span class="important"> *</span><select name="model" id="model" onchange="form_yr(this.value)"
                value="<?php if (isset($_POST['model'])) echo $_POST['model']; ?>" ></select></label>
            
            <label>Year: <span class="important"> *</span><select name="battery" id="bat_disp"
                value="<?php if (isset($_POST['battery'])) echo $_POST['year'] ?>" ></select></label>

            <label>Quantity: <span class="important"> *</span><input type="text" name="quantity" id="quantity"
                value="<?php if (isset($_POST['quantity'])) echo $_POST['quantity']; ?>" /></label>
            <label>Warranty ? <input type="checkbox" name="warranty"
                <?php if ($_POST['warranty']) echo " checked"; ?> /></label>
            <label>Exchange ? <input type="checkbox" name="exchange"
                <?php if ($_POST['exchange']) echo " checked"; ?> /></label>
            <input type="submit" name="submit" value="Add Order" /> or
                    <a href="index.php">Cancel</a>
        </fieldset>
    </form>
</div>

<script type="text/javascript" src="include/functions.js"></script>

<script>
$(document).ready(function() {

    $('.container').hide();
    $('.container').fadeIn(1000);

    request_cust();
    request_man();

});
</script>

<?php include ('include/footer.php');

 

 

 

 

 

2. request_fill

 

 

 

function request_fill() {

    var cust2 = 'customer';
    var ad = document.getElementById("add_a");
    
    var hr = new XMLHttpRequest();
    hr.open("POST", "battery_parser.php", true);
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    hr.onreadystatechange = function() {

        if(hr.readyState == 4 && hr.status == 200) {

            var dataArray = hr.responseText;

            ad.innerHTML = dataArray[1];
            
        }
    }

    hr.send("&cust2="+cust2);  
}

 

 

 

3. battery_parser.php

 

 

 

<?php

 

include_once("include/dbconnect.php");

 

if (isset($_POST['cust2'])) {
        $cust2 = $_POST['cust2'];
        $sql = "SELECT customer_address FROM customers WHERE customer_id = 2";
        $query = mysqli_query($con, $sql);
        $dataString = '';
        while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
            $add = $row["customer_address"];
            $dataString = $add;  
        }
        mysqli_close($con);
    
        echo $dataString;
        exit();

    } else {
        exit();
    }

?>

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.