Search code examples
phpjquerytrygetvalue

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


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>

Solution

  • 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);