G
G
Gangg2018-04-13 20:07:11
AJAX
Gangg, 2018-04-13 20:07:11

[php] How to insert url from php code into ajax request?

I have this ajax request code

<script type="text/javascript"> 
$(document).ready(function(){
    $('#buttonget').click(function(){    
         $.ajax({
         url: 'demo.php',
         success: function(data) {
         $('.results').html(data);
         }
         });
    })
});
</script>
<button id="buttonget">Получить результат</button>
<div class="results">Ждем ответа</div>

There now in the url parameter is the value demo.php But I need to insert a link from the php code there, which is formed on the fly with the parameters. For example: And now I need to insert the $demoUrl php variable into the url parameter in the ajax request like this
$demoUrl = "demo.php?parm=1&parm=2&parm3"
<script type="text/javascript"> 
$(document).ready(function(){
    $('#buttonget').click(function(){    
         $.ajax({
         url: <?php echo $demoUrl ?>,
         success: function(data) {
         $('.results').html(data);
         }
         });
    })
});
</script>
<button id="buttonget">Получить результат</button>
<div class="results">Ждем ответа</div>

But it is clear that this does not work. But how can I substitute a url request in ajax that is generated earlier by a php script?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivanq, 2018-04-13
@Ivanq

<script type="text/javascript"> 
$(document).ready(function(){
    $('#buttonget').click(function(){    
         $.ajax({
         url: <?php echo str_replace("</script>", "<\\/script>", json_encode($demoUrl)) ?>,
         success: function(data) {
         $('.results').html(data);
         }
         });
    })
});
</script>
<button id="buttonget">Получить результат</button>
<div class="results">Ждем ответа</div>

That is, we encode the string into JSON and then escape </script>it to be safe.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question