Users Selecting cost by checkbox using PHP, javascript or jquery b4 update db

I have an issue trying to populate a price list for use in order processing. Would appreciate your help and any advice. The page should allow a user to either select the set cost of a service charge ($10.01 for brake pads) using CHECKBOX OR type in a CUSTOM PRICE amount for the customer. Test data ($10.01 for brake pads) is ok to the screen from the db, now just trying different ways to get form input field to function in CHECKBOX OR CUSTOM PRICE scenario. I’m relatively experienced with PHP but not with jquery. I’ve found some other similar code marked ‘PHP method’ and ‘JQuery method’ as possible options. Once visually set on screen, the input will be treated the same and saved as fields, added for total cost etc. I’m looking for the simplest way to implement while at bare knowledge of javascript and jquery & learning both.

The partially working link is as shown:

from the database, currently assigning each value to order before processing form. On submit, would reassign vars when input=“” or ==“0”;

$row_rsActiveServices = mysql_fetch_assoc($result);
$ordpatch=$row_rsActiveServices['bus_patch'];
$ordplug=$row_rsActiveServices['bus_plug'];
$ordalignment=$row_rsActiveServices['bus_alignment'];
$ordmuffler=$row_rsActiveServices['bus_muffler'];

PHP method {{{{

<input type="checkbox" name="chk[]" value="<? $row_rsActiveServices['bus_patch']; ?>" <?php if(isset($_POST['chk']['A'])) echo 'checked="checked"'; ?>/>A
<input type="checkbox" name="chk[]" value="<? $row_rsActiveServices['bus_plug']; ?>" <?php if(isset($_POST['chk']['B'])) echo 'checked="checked"'; ?>/>B
<input type="checkbox" name="chk[]" value="<? $row_rsActiveServices['bus_alignment']; ?>" <?php if(isset($_POST['chk']['C'])) echo 'checked="checked"'; ?>/>C
<input type="checkbox" name="chk[]" value="<? $row_rsActiveServices['bus_muffler']; ?>" <?php if(isset($_POST['chk']['D'])) echo 'checked="checked"'; ?>/>D

<input type="checkbox" name="chk[]" value="A" <?php if(isset($_POST['chk']['A'])) echo 'checked="checked" *echo '; ?>/>A

<!-- repeat this code for each service, we assume that servicesid is unique for each service: -->
<input <?php if (!(strcmp($row_rsActiveServices['showinfooter'],"y"))) {echo "checked=\\"checked\\"";} ?> name="checkbox[<?php echo $row_rsActiveServicess['servicesid']; ?>]" type="checkbox">
<input type="hidden" name="checkbox_mark[<?php echo $row_rsActiveServices['servicesid']; ?>]" value="1">

JQUERY METHOD: Perhaps use .show if checkbox "checked {{{

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("p").click(function(){
    $(this).hide();
  });
});
</script>

}}}

JAVASCRIPT METHOD: Using a button rather than checkbox to change the value of an element
{{{{

<p id="demo">
JavaScript can change the content of an HTML element.
</p>

<script>
function myFunction()
{
x=document.getElementById("demo");  // Find the element
x.innerHTML="Hello JavaScript!";    // Change the content
}
</script>

<button type="button" onclick="myFunction()">Click Me!</button>

}}}}} END JAVASCRIPT METHOD

LAST STEP, UPDATING THE DB
Gather the information:

if(isset($_POST['checkbox_mark'])){
  mysql_select_db($database_db, $db);
  
  // the value (servicesid) is passed in hidden input's key, therefore
  // the value of the input itself is irrelevant ($dummy)
  foreach ($_POST['checkbox_mark'] as $value => $dummy) { 
    $option = isset($_POST['checkbox'][$value]) ? 'y' : 'n';
    $query = "UPDATE services SET showonwebsite='$option' WHERE servicesid = $value"; 
    $Result = mysql_query($query, $db) or die(mysql_error());
  } 
}

I’m lost. What’s your actual question?

I’m seeking a best method that would assign a database field to a html input if checkbox is checked. I’m indifferent to whether the best way is php within html or an approach i’m less familiar with (javascript or jquery). Perhaps ‘onclick’, etc.

For instance, <input name=“servpatch” would be assigned value of
<? $row_rsActiveServices[‘bus_patch’]; ?> when user clicks checkbox <input type=“checkbox” name=“boxpatch” value=“ON” />

Thanks for any help with this.

“When the user clicks”… this tells you that you want Javascript to handle it.

PHP can pre-load the javascript variables during “Parse Time” as I call it (PHP is parsing the file). Alternately, you can use AJAX to retrieve them as-needed.
Because you want the page to respond to user input in HTML, you need Javascript in some form.

So; you can simply have PHP output the variables into the script…


<script type='text\\javascript'>
var bus_value = <?php echo $row['bus_value"]; ?>
</script>

That said, all it sounds like you’re trying to do is interpret whether the user has checked a checkbox or filled in a value. I’m not sure you need anything that fancy.

So… Let me give you the logic flow as I see it.

Form Logic:

form start
foreach Service (or While New Row from DB)
Echo name, checkbox, textfield, hidden field with field names assigned as keys to chk and tfield, fields arrays.
endforeach
Submit button
form end

Form Processor Logic:
foreach $_POST[‘fields’] as key => value
if chk[key] is set, or (tfield[key] is not empty and tfield[key] is numeric)
process service
endif
endforeach