0

I have an ajax call to an external php file "commentS.php" that refreshes every 3 seconds. However. I expected to just use the variable in the hidden input field (name="idd") and expected it to just reflect on the "commentS.php", but, it does not. What I want to achieve is to have the variable "idd" echoed on the "commentS.php" page as a variable to be used in a mysql query. I did the document write on the commentS.php but it just shows a white page with the variable number that is "idd" when the ajax call is made.

<input type="hidden" name="idd" value="<?php echo $row['story_id']; ?>">
<script type="text/javascript" src="jquery-3.3.1.min.js"></script>
<script type="text/javascript">
var named = "<?php echo $idd; ?>";
$(document).ready(function() {
setInterval(function () {
$('#content').load('commentS.php')
}, 3000);
});
</script>

Any help would be great, a read, anything. I am very new to js and ajax altogether. thanks

jwikom
  • 31
  • 7
  • Seems like you need to POST a form to comments.php rather than just loading it via GET. How is comments.php set up to retrieve the idd value? – James Nov 05 '18 at 14:07
  • Possible duplicate of [Best way to pass parameters to jQuery's .load()](https://stackoverflow.com/questions/263962/best-way-to-pass-parameters-to-jquerys-load) – Gufran Hasan Nov 05 '18 at 14:13
  • 1
    If mixing php and javascript that way is your solution, then you are most likely asking the wrong question. – faerin Nov 05 '18 at 14:17

1 Answers1

0

Try this :

$('#content').load('commentS.php?idd=<?php echo $idd; ?>')

In commentS.php you have to do this in the beginning :

$idd = $_GET['idd']
Sami
  • 717
  • 6
  • 28
  • 1
    You likely need a cache buster too – mplungjan Nov 05 '18 at 14:10
  • **Danger**: This code is [vulnerable to XSS](https://www.owasp.org/index.php/XSS) User input needs escaping before being inserted into an HTML document!. – Quentin Nov 05 '18 at 14:18
  • Sami your way does work, HOWEVER $('#content').load('commentS.php?idd=') somehow the echo within the javascript "" doesn't echo the value. If I just put a numerical value in then I get the result I desire, it just however doesn't print. Is there a way to get the php result as a javascript variable that can be added after .load('commentS.php?idd=(RIGHT HERE). thanks so much for your help been at this all day yesterday without avail. – jwikom Nov 05 '18 at 14:32
  • replace with – Sami Nov 05 '18 at 14:42
  • thank you so much Sami, everything now works like I want it too. wish I had three hands to give you three thumbs up. – jwikom Nov 05 '18 at 14:44
  • u're welcome :) As @Quentin said, this method is very dangerous ! In your PHP you have to double check the value ! If it's an ID you have to cast it : $idd = (int) $_GET['idd'] – Sami Nov 05 '18 at 14:59
  • ok will do, now I have another issue, I have another script I am working on but instead of using the GET method which works perfect from the way you helped I am now using POST and it just doesn't echo the form value. I am trying to pass this through { formdata.append("imagetitle", ' '); } but it doesn't show the form value at all. is there a work around? – jwikom Nov 07 '18 at 17:34
  • it i put the variable in a string say image_title = "this is the image title"; then it parses over like i want to. however $_POST["image_title"]; after inputted text carries nothing. – jwikom Nov 07 '18 at 17:49