How do I get value from span and assign it to a php variable?

2.8k Views Asked by At

How can I take the value of:

<span name="MinPrice" class="irs-from">0</span>
<span name="MaxPrice" class="irs-to">0</span>

And assign them to PHP variables:

$MinPrice
$MaxPrice

This is my JQuery code:

   var base_html =
    '<span class="irs">' +
    '<span class="irs-line" tabindex="-1"><span class="irs-line-left"></span><span class="irs-line-mid"></span><span class="irs-line-right"></span></span>' +
    '<span class="irs-min">0</span><span class="irs-max">1</span>' +
    '<span name="MinPrice" class="irs-from">0</span><span name="MaxPrice" class="irs-to">0</span><span class="irs-single">0</span>' +
    '</span>' +
    '<span class="irs-grid"></span>' +
    '<span class="irs-bar"></span>';

I use form:

<form action="<?php echo languageURL(0, ''.$g['url'].'products.html'); ?>" method="get">

<div class="wrapper" style=" padding:20px;">
  <div class="range-slider">
      <input type="text" class="js-range-slider" value="" />
  </div>
</div>

    <input type="submit" class="btn-primary goo" value="Търсене" />
</form>
2

There are 2 best solutions below

4
On BEST ANSWER

You could use AJAX to assign your values to PHP variables. You can send the form data like this in jQuery:

function sendData()
{
    $.ajax({
        type: "POST",
        url: 'ajax.php',
        data: {$("#formID").serialize(), MinPrice: $('#minSpan span').html(), MaxPrice: $('#maxSpan span').html()},
        dataType: "html",
        success: function(html)
        {
            // do something ...
        }
    });
}

In this case the data is sent via POST and your form needs an ID (e.g. id="formID").

Update: Now also the values of the spans are sent, here with the IDs id="minSpan" and id="maxSpan". In ajax.php you can read the POST values, try it with to see all available values:

var_dump($_POST);
0
On

As already mentioned in comments

  1. Span doesn't have values they have innerHTML.
  2. You will need to do it through ajax(if you want to do it using jQuery).

Now To get the value out of base_html variable, you will first need to parse HTML string and then take out values from it i.e.

var min = $.parseHTML( base_html).find(".irs-from").text();
var max = $.parseHTML( base_html).find(".irs-to").text();
$.ajax({url: "value_set.php?min="+min+"max="+max, success: function(result){
   //do whatever you like to do here
  }});