Hello, I contacted one problem. From JS, I have a number in the DIV block and I need to insert into the variable the value that I put in the DIV

<div id='iidd'>12</div> 

PHP:

 $id = #iidd; //сюда нужно вывести число из div блока 

I tried to do this:

 $ids = "<div id='iidd'></div>"; $idssd = htmlspecialchars($ids); $id = preg_replace('/[^0-9]/', '', $idssd); 

But the problem occurs when $ idssd, it does in html, but the number from JS, which outputs to the div does not bring with it. Displaying just $ ids displays the number, but in additions with this, and in the variable $ id you only need the number

  • How does a div block with a number get from the browser to the server where php is running? - Igor
  • JS: $("#div").text(id); (id - in a variable), html: <div id="div"></div> - Sasha Osipov
  • It seems to me that you are not sending the value in php . When inserting a value into a div using js how do you pass it to php ? - x_ror
  • @ SashaOsipov is not the answer to my question - Igor
  • The output simply $ ids displays the number, but in additions with this div, and in the variable $ id you only need the number - Sasha Osipov

2 answers 2

Not sure I understood the question correctly, but still. There is an option to send a variable to the server. Those. after $("#div").text(id); write $.post('script.php',{a:id}); , and in script.php you can write a variable to a file, and in your script, read it from a file. Or, instead of script.php, substitute the path to your script, and there already process

  • Made. JS: $.post('u/two.php',{"isdsd":id}); PHP: $id = $_POST[isdsd]; The answer: emptiness - Sasha Osipov
  • The answer is not looking there. - vp_arth
  • one
    $.post('u/two.php',{isdsd:id}); - remove the quotes here, and here put: $id = $_POST['isdsd']; - Andrei
  • @ Andrei nothing has changed - Sasha Osipov
  • one
    Then I do not know. Should work - Andrei

Here is an example of how you should send a block div value to the server.

 document.getElementById("some_id").addEventListener("DOMSubtreeModified", function() { var value = this.innerHTML; $.ajax({ url: './ajax.php', type: "GET", data: { value_of_div: value }, success: function(response, textStatus, jqXHR){ console.log('sended!'); }, error: function(response, textStatus, jqXHR){ console.log('error!'); } }); console.log(value); }); document.addEventListener("click", function() { document.getElementById("some_id").innerHTML = 13; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="some_id">12</div> 

Clicking on the div changes the value of the div and is sent to the server. This is the same as you change the div value in js .

And then in the file

ajax.php

 if(isset($_POST['value_of_div'])){ $value = $_POST['value_of_div']; echo $value; } 

Something like this.