I am passing data using J-Query and AJAX to res.php page:-
attempt.php (my main page from where the res.php is called)
$.ajax(
{
url: "res.php",
type: "POST",
data: data,
success: function (data)
{
var build_id='build_';
build_id += i;
alert(build_id);
$('#npc').append(data);
$('#build_id').attr('disabled','true');
var total = $("#build_id");
alert(total);
}
});
In the res.php page I am using the passed data to generate the query and retrieve data from the database and calculated the total value and display the total in a text box.
res.php
$i = mysql_real_escape_string($_POST['i']);
$query = "select * from ";
$query = $query . $building;
$query = $query . " where lvl=" . $level;
$query = $query . ";";
$result = mysql_query($query) or die('Error in Child Table!');
echo $i;
while( $data = mysql_fetch_assoc($result))
{
$total_res = $data['lumber'] + $data['clay'] + $data['iron'] + $data['crop'];
echo '<tr><td>'. $building. '</td><td>'. $level. '</td><td><input type="text" style="width:70px" id="build_' . $i . '" value="' . $total_res . '"></td></tr>';
}
I am using id="build_' . $i . '" to auto increment the id of the text box. Till here everything goes fine.
Back to the attampt.php page,
when I try to access the data from the text box i am unable to access it.
The code that I’m using to access the data is:-
var build_id='build_';
build_id += i;
$('#build_id').attr('disabled','true');
var total = $("#build_id");
alert(total);
the alert function at the end of the code gives the following output:-
[object Object]
I am trying to append a list of text boxes with some values to a table and then calculate the sum of all the values in the text boxes.
Please help.
$('#build_id')should be$('#'+build_id)and to calculate the total of all the values, you should putvar total=0;outside your callback function, and the function should containAnother problem is that each row of the table has the same
id="build_$i", but IDs have to be unique.And do you really intend to display
$iat the beginning of the table, without it being in a table row?