简体   繁体   中英

Storing id into database table throw there name

I'm working on small project for data entry. let assume a table named material fields
id , name , visible , position

and other table named received_material fields
id , material_id , quantity , supplier

and one simple html form for receiving materials, structure here

<form method="post" action="<?php echo SERVER['PHP_SELF'] ?>">
    <label for="date">date</label>
    <input type="text" name="date" />

    <label for="name">Material Name</label>
    <input type="text" id="name" name="name" />

    <label for="quantity">Weight</label>
    <input type="text" name="quantity" />

    <label for="supplier">Supplier</label>    
    <input type="text" name="supplier" />

    <input type="submil" value="submit"/>
</form>

<script>
    $('#name').autocomplete({source : 'supplier_chk.php'});
</script>

the supplier_check.php Script here...

<?php include 'includes/session.php'; ?>
<?php include 'includes/functions.php'; ?>

<?php if(!logged_in()) {
    redirect_to('login.php');
}
?>
<?php if ( logged_in_role('administrator') || logged_in_role('store') ) { ?>
<?php echo msg(); ?>

<?php 

$search = escape_string($_GET['term']);
$sql = "SELECT id, supplier_name FROM `supplier`
        WHERE `supplier_name` LIKE '%$search%'";
$result = mysqli_query($connection,$sql);
confirm_query($sql);

$json = [];
    while ( $row = mysqli_fetch_assoc($result) )
    {
        $json[] = $row['supplier_name'];
    }
    echo json_encode($json);

}

?>

I'm using JQuery UI For Name Autocomplete. fetching material name throw ajax method. it'll easily autocomplete material name but i've to store material id instead material name.. please help how to do this action using jquery/javascript or php.

add hidden field before name input give id to get element..

here html example

<form method="post" action="<?php echo SERVER['PHP_SELF'] ?>">
    <label for="date">date</label>
    <input type="text" name="date" />

    <label for="name">Material Name</label>
    <input type="hidden" id="id" name="id" />
    <input type="text" id="name" name="name" />

    <label for="quantity">Weight</label>
    <input type="text" name="quantity" />

    <label for="supplier">Supplier</label>    
    <input type="text" name="supplier" />

    <input type="submil" value="submit"/>
</form>


<script>
$('#name').autocomplete({
    minLength: 0,
    source : 'material_chk.php',
    focus: function( event, ui ) {
    $( "#name" ).val( ui.item.label );
    return false;
    },
    select: function( event, ui ) {
    $( "#name" ).val( ui.item.label );
    $( "#id" ).val( ui.item.id );

    return false;
    }
});
</script>

in material_chk.php

<?php 
$search = escape_string($_GET['term']);
$size = isset($_GET['size'])? escape_string($_GET['size']): null;
$sql = "SELECT * FROM `materials`
        WHERE `name` LIKE '%$search%'";
$result = mysqli_query($connection,$sql);
confirm_query($sql);
$json = [];
    while ( $row = mysqli_fetch_assoc($result) )
    {
        $json[] = array("id"=>$row['id']),"label"=>$row['item_name']);
    }
    echo json_encode($json);
}

I'm note sure what you're asking, but I do have a tip. This is a general tip, not just restricted to suppliers, but I will use them as an example.

I would use a better solution: a dropdown list with all the supplier names. The value of each list item is the supplier ID. So you directly submit the supplier ID.

<select id="suppliers" name="supplierSelector">
  <option value="1">Philips</option>
  <option value="2">Sony</option>
  <option value="3">Samsung</option>
  <option value="4">Akai</option>
</select>

You could additionally put a search text input below the dropdown list. Anything typed into that search box will restrict the dropdown list. This can be done in Javascript.

But basically my solution is to let the user select the correct supplier ID using their names. in PHP that would be:

$sql    = 'SELECT id, supplier_name FROM supplier';
$result = mysqli_query($connection,$sql);

echo '<select id="suppliers" name="supplierSelector">'.PHP_EOL;

while ($supplier = mysqli_fetch_assoc($result))
{ 
  extract($supplier);
  echo '<option value="'.$id.'">'.$supplier_name.'</option>'.PHP_EOL;
}

echo '</select>'.PHP_EOL;

The added advantage of this method is that the form cannot be submitted without a valid supplier. In general you should try to restrict the input to what you know is allowed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM