简体   繁体   中英

returning mysql results to into php string

I am trying to print a string out to html when doing a MySQL query. It seems to work and when it echos it out it gives me error. The code is :

$result = mysqli_query($con,'SELECT * FROM vehicle_type ORDER BY cat_id desc');

while($row = mysqli_fetch_array($result)) {
$category = $row['category'];
$count++;



$vehicle_types .= "<a href='#' onclick='document.getElementById('vehicle_types').value='$count';$('#exec').show();$('#coach').hide();$('#minibus').hide();$('#limos').hide();vehicle_type_name.innerHTML = 'Vehicle type selected : Executive vehicle';vehicle_selected.innerHTML = ''; ' class='btn new btn-primary'>$category</a>";

}

when I echo out the result it gives me :

 <a href='#' onclick='document.getElementById('vehicle_types').value='1';$('#exec').show();$('#coach').hide();$('#minibus').hide();$('#limos').hide();vehicle_type_name.innerHTML = 'Vehicle type selected : Executive vehicle';vehicle_selected.innerHTML = ''; ' class='btn new btn-primary'>Minibus</a>

When it should give me :

<a href="#" onclick="document.getElementById('vehicle_type').value='1';$('#exec').show();$('#coach').hide();$('#minibus').hide();$('#limos').hide();vehicle_type_name.innerHTML = 'Vehicle type selected : Executive vehicle';vehicle_selected.innerHTML = ''; " class="btn new btn-primary">Executive cars</a>

Thanks

I suggest moving all those onClick statements to a function for better readability.

Also, be consistent with your use of jQuery.

$vehicle_types .= '<a href="#" onclick="vehicleType_onClick(\'' . $count . '\')" 
                   class="btn new btn-primary">' . $category . '</a>';

<script>
function vehicleType_onClick(count){
    $('#vehicle_types').val(count);

    $('#exec').show();
    $('#coach').hide();
    $('#minibus').hide();
    $('#limos').hide();
    $('input[name=vehicle_type_name']).html('Vehicle type selected : Executive vehicle');
    $('input[name=vehicle_selected']).html(''); 
}
</script>

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