I am using jQuery to change the content of <p> tags as the user types in numbers, as it is a dynamic price calculator.
Once they have completed filling in their data, they would submit the form that the data is in. The form points to another file to do the PHP and database work. However, when the data goes into the database, the values are set at '0', which is their default and do not go into the database as how the user has put it in.
Here is the jQuery code:
[CODE]$(document).ready(function() {
// Array of costs for the various consumables in our office.
var costs = {
single: 0.04,
double: 0.06,
a4_sheet: 0.005,
a4_coated_sheet: 0.007,
a3_sheet: 0.007,
a3_coated_sheet: 0.015,
us_sheet: 0.03,
cd_disc: 0.19,
cd_disc_labels: 0.2,
cd_case: 0.28,
address_labels: 0.05,
inkjet_labels_2: 0.05,
inkjet_labels_4: 0.01
}
// Using the .keyup() method on the following ID's
$("#single, #double, #a4_sheet, #a4_coated_sheet, #a3_sheet, #a3_coated_sheet, #us_sheet, #cd_disc").keyup(function(){
var value = $(this).val(), // Sets the value of the input box that is being used.
attr = "p#"+$(this).attr('id'), // Sets the id for later use. e.g. p#single.
cost = costs[$(this).attr('id')], // Sets the correct cost variable.
sum = value * cost,
total = 0; // Do the math.
$(attr).text(sum.toFixed(2)); // Update the text of the attr variable we set earlier.
var tmpVal=0;
$(".cost").each(function(){
var tmp = $(this).text();
tmpVal += parseFloat(tmp);
});
//console.log("tmpVal :" + tmpVal);
//easier to use in php
$('#total').text(tmpVal.toFixed(2));
//$("#total").html(tmpVal.toFixed(2));
}).keyup();
});
[/CODE]
This is the form that is used: (The jQuery is included into the file as the dynamic text works fine). (I was only testing with the first 3, hence why they're the only ones with the PHP variables!)
[HTML]<form class='centeredtext' action="test.php" method="post" accept-charset="utf-8">
<input type="hidden" name="add" value="true" />
<table width="100%">
<thead>
<tr>
<th class='centeredtext' width="40%">Type</th>
<th class='centeredtext' width="30%">Quantity</th>
<th class='centeredtext' width="200px">Cost (£)</th>
</tr>
</thead>
<tbody class='centeredtext'>
<tr>
<td><label for="single">Single sided colour print</label></td>
<td><input name="single" type="text" id="single" /></td>
<td>
<?php $line1 = '<p class="cost" id="single"></p>';
echo $line1 ?></td>
</tr>
<tr>
<td><label for="double">Double sided colour print</label></td>
<td><input name="double" type="text" id="double" /></td>
<td><?php $line2 = '<p class="cost" id="double"></p>';
echo $line2;
?></td>
</tr>
<tr>
<td><label for="a4_sheet">A4 Sheet</label></td>
<td><input name="a4_sheet" type="text" id="a4_sheet" /></td>
<td><?php $line3 ='<p class="cost" id="a4_sheet"></p>';
echo $line3;
?></td>
</tr>
<tr>
<td><label for="a4_coated_sheet">A4 Coated Sheet</label></td>
<td><input name="a4_coated_sheet" type="text" id="a4_coated_sheet" /></td>
<td><p class="cost" id="a4_coated_sheet"></p></td>
</tr>
<tr>
<td><label for="a3_sheet">A3 Sheet</label></td>
<td><input name="a3_sheet" type="text" id="a3_sheet" /></td>
<td><p class="cost" id="a3_sheet"></p></td>
</tr>
<tr>
<td><label for="a3_coated_sheet">A3 Coated Sheet</label></td>
<td><input name="a3_coated_sheet" type="text" id="a3_coated_sheet" /></td>
<td><p class="cost" id="a3_coated_sheet"></p></td>
</tr>
<tr>
<td><label for="us_sheet">US Sheet</label></td>
<td><input name="us_sheet" type="text" id="us_sheet" /></td>
<td><p class="cost" id="us_sheet"></p></td>
</tr>
<tr>
<td><label for="cd_disc">CD Disc</label></td>
<td><input name="cd_disc" type="text" id="cd_disc" /></td>
<td><p class="cost" id="cd_disc"></p></td>
</tr>
<tr>
<td><strong>Total:</strong></td>
<td></td><td>
<p id="total">
</p></td>
</tr>
</form>[/HTML]
and finally, this is the PHP.
[PHP]<?php
require_once('index.php');
$con = mysql_connect('blah', 'blah', 'blah');
mysql_select_db('blah', $con) or die(mysql_error());
$sql = "INSERT INTO DBTable VALUES (null, '$line1', '$line2', '$line3', 'nothing')";
$res = mysql_query($sql) or die(mysql_error());
?>[/PHP]
TL;DR version:
Dynamic text updates on form, but only goes into DB as default value (0).
Any help on how to input the dynamic text as it stands on the page would be greatly appreciated.
Thank you!
That isn't how reading form data from PHP works, you don't assign those fields to a variable and then suddenly have access to them. Not sure where you found that, but you should read up on POSTing data with PHP.
Thanks for the quick reply.
I've assigned them variables so that I could access them in another file easier. I get (sort of) access to them as I can get their default value.
Would you kindly point me in the right direction please? I'm not a PHP guru at all (as you can tell).
The dynamic text [B]NEEDS[/B] to be in a <p> (as that's how the jQuery changes the content), so I don't want it to be inside a textarea or input field.
Thanks.
Right now, this is the amount of information that gets sent to the second PHP script:
[quote]
[/quote]
You need to find a way to use inputs to send them to the script, so you can get them with $_POST.
I don't even know what it's the jQuery used for; if you only want it to have a default value that the user can change, it can be done with HTML alone.
If you want it to calculate the total amount, there's no need to calculate it clientside AND send it to the second script. PHP can make sums as fine as javascript does.
And, while you're at it, please take a look at using PDO or mysqli. You won't regret it.
[QUOTE=BeatAlex;40170806]Would you kindly point me in the right direction please?.[/QUOTE]
[QUOTE=KmartSqrl;40170767]you should read up on POSTing data with PHP.[/QUOTE]
Alright, thank you for your replies! I shall look into this today.
Thanks for the help and fast replies! Much appreciated.
Sorry, you need to Log In to post a reply to this thread.