0

My apologize in advance for my (maybe) stupid question but my knowledge of Javascript/JQuery is almost 0

I have in index.html the following script:

index.html

<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() { 
   $("#MyDIV")
   .load("buttons.php") 
  });
</script>
<div id="MyDIV"></div>

As you can see, the idea is load buttons.php in a div tag in index.html - That works fine -

buttons.php

<div id="buttom">
    <div id="botton5"><a href="#"><img src="5.png" id="5"/></a></div> 
</div>
<script> 
$('#botton5').click(function(event){ 
   $("#bottons").load('somepage.php?answer=5');  //here!

}); 
</script>

Thats also works fine and I receive the information from somepage.php in MyDIV in index.html

but does not work when I include a php in the URL in the line

$("#bottons").load('somepage.php?answer=5&title=<?php echo $title;?>&date=<?php echo $date;?>');   //here!

Including a PHP in load, the div does not load in index.html, can you please support me on how to add a php in the URL in load?

Thanks in advance for your support

  • Since you call `somepage.php` from JavaScript you can't use PHP there – modsfabio Jun 02 '17 at 12:50
  • Dom doesn't support directly with php codes – prasanth Jun 02 '17 at 12:51
  • 1
    Did you define `$title` and `$date` before trying to use them? – Qirel Jun 02 '17 at 12:51
  • Related? One https://stackoverflow.com/q/23740548/1809433 Two https://stackoverflow.com/a/13840431/1809433 – Nikita U. Jun 02 '17 at 12:53
  • My apologize @NikitaU. but as I said, my knowledge on this is almost 0 and I don't know how to adapt the related post to my situation – Luis Gerardo Runge Jun 02 '17 at 13:04
  • Is this code `$("#bottons").load('somepage.php?answer=5&title=&date=');` in your PHP file? If not, it would never work. This JQuery code should be in PHP file. – Milan Chheda Jun 02 '17 at 13:18
  • @MilanChheda I did not understand your question, but in order to provide more info, all works fine without PHP, even if I change and set to does not work – Luis Gerardo Runge Jun 02 '17 at 13:22
  • `somepage.php?answer=5&title= // does not work` `somepage.php?answer=5&title= // does not work` `somepage.php?answer=5&title=Hello world // Work` So, I assume that does not work when I include php, but I'm sure that must be a solution. – Luis Gerardo Runge Jun 02 '17 at 13:29

1 Answers1

0

@Luis Gerardo Runge In JavaScript you can't use PHP but logically you can take your value in hidden field and use in javascript. i have add some sample code my be it's help you

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head> 
<div id="buttom">
    <div id="botton5"><a href="#"><img src="5.png" id="5"/></a></div> 
</div>
<div id="bottons">

</div>
<?php 
$title = 'your title';
$date = 'yourdate';
?>
<input type="hidden" id="title" name="title" value="<?php echo urlencode($title); ?>">
<input type="hidden" id="date" name="date" value="<?php echo urlencode($date); ?>">

<script> 
$('#botton5').click(function(event){ 
alert('call');
   $("#bottons").load('json.php?answer=5&title='+$('#title').val()+'&date='+$('#date').val()+'');  //here!

}); 
</script>

FYI: You need to use urlencode and urldecode for pass parameter through the URL

Tester
  • 271
  • 1
  • 5
  • 16