A solution to Get span id value in PHP

In this article, you going to see how to get span tag of html value assign to the PHP variable. We going to see in a step by step.

Let’s start with the solution for getting span value in PHP in a detail.

You going to see this with the help of example.

To store the span text into PHP variable, we going to achieve this using below example.

Below is the example from which you can understand completely.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Span ID value in PHP</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
Printed PHP variable total amount value : 
<?php
  if(isset($_POST['total'])){
	 echo '<strong>'.$output = $_POST['total'].'</strong>';  
  } 
?>
<div class="form-sep">
	<div class="row-fluid">
		<div class="span6">
			<label class="field-title">------Student Payble Fees------</label><br/>
			<label class="field-title">Maths Fee:</label>
			<input type="text" name="valsum" class="valsum" value="10" > +<br/>
			<label class="field-title">English Fee:</label>
			<input type="text" name="valsum" class="valsum" value="20" ><br/>=
			<td align="center"><strong>(Store in Span id value) Rs. <span id="sum"></span></strong></td>
			<form id="formtotal" name="formtotal" action="" method="post"><br/>
			<input type="text" name="total" id="total" value="">
				</div><br/>
			<input type="submit" value="SUBMIT" >	
			</form>
	</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
        //iterate through each textboxes and add keyup
        //handler to trigger sum event
        $(".valsum").each(function() {
            $(this).keyup(function(){
                calSum();
            });
        });
    });
    function calSum() {
        var sum = 0;
        //iterate through each textboxes and add the values
        $(".valsum").each(function() {
            //add only if the value is number
            if(!isNaN(this.value) && this.value.length!=0) {
                sum += parseFloat(this.value);
            }
        });
        //.toFixed() method will roundoff the final sum to 2 decimal places
        $("#sum").html(sum.toFixed(2));
        $("#total").val(sum.toFixed(2));
    }
</script>
</body>
</html>

OUTPUT:

OUTPUT ON FORM SUBMIT
OUTPUT ON FORM SUBMIT

With help of Student fees calculation you can understand completely.

In the above example you can see two input box one for Math fees and another one is for English fees.

I am calculating both subject fees using Jquery.

I taking both input value using Jquery and then passing into function where calculation is done.

After the completion of the calculation it is assign to html input tag element inside form.

When the total value of the both fees get inserted inside input tag name total of form.

An important step to assign to PHP :

You can see the output above.

Now step is to how the value is get populated using Jquery.

On click of submit button the form value get posted.

This posted value is get assign to PHP variable.

In this form is get posted on click of submit button and value is get assigned to PHP value.

Finally we done with the assign of span data to php variable.

This posted total variable you can also use to insert this data to into database.

“Save textbox value to database PHP”

To insert the posted value into database you can use the SQL statement query.

Below is syntax for SQL statement query.

INSERT INTO table_name
VALUES (value1, value2, value3, value4,...);

Below is descriptions regarding parameter and its value.

ParameterDescription
value1It is the first value to be inserted.
value2It is the second value to be inserted.
value3It is the third value to be inserted.
value4It is the fourth value to be inserted.
Insert statement query

I hope you liked my this article. If you have any queries or any question regarding this, Feel free to comment on Me.

1 thought on “A solution to Get span id value in PHP”

Leave a Comment