Jump to content

Inserting PHP variable into JavaScript function


Recommended Posts

I have a blog, and under each comment there is this link:

 

<a href="article/<?php echo $_GET['id']; ?>/#comments" onClick="quote(3, '<?php echo strip_tags($content); ?>');">Quote</a>

 

This calls the following JavaScript function:

 

function quote(thisField, text) {
  if (text == null) {
    var text = prompt('Enter quote:', '');
  }
  if (text != null) {
    document.thisForm.elements[thisField].value += "<quote>" + text + "</quote>";
  }
}

 

It works beautifully, as long as the PHP variable $content doesn't contain any quotes in it. I tried using htmlentities() and addslashes() on it with no luck. It doesn't insert anything at all into the form.

Really close to getting this to work:

 

<script type="text/javascript">
  var content = <?php echo json_encode($content); ?>;
</script>
<a href="article/<?php echo $_GET['id']; ?>/#comments" onClick="quote(3, content);">Quote</a>

 

However, now the last comment on the page is the one that always gets inserted. Probably because the js var content is getting set each time a comment is displayed. How do I work around this?

I thought this would work, but it doesn't:

<script type="text/javascript">
  content[i] = <?php echo json_encode($content); ?>;
</script>
<a href="article/<?php echo $_GET['id']; ?>/#comments" onClick="quote(3, content[i]);">Quote</a>
<script type="text/javascript">
  i ++;
</script>

 

Before the PHP while loop, I initialize the js array content and the js var i:

 

<script type="text/javascript">
  var i = 0;
  var content = new Array();
</script>

 

But, this results in the text not being passed at all. So now I'm being prompted for the text. Please help!

<script type="language/javascript">
  function copyContent() {
    document.thisForm.content.value += document.thatForm.content.value;
  }
</script>
<form name="thatForm">
  <textarea name="content"><?php echo stripSpaces($content); ?></textarea>
</form>
<a href="article/<?php echo $_GET['id']; ?>/#comments" onClick="copyContent()">Quote</a>

 

Doesn't work. Seriously, doesn't anyone know how to do this?

Well, hopefully everyone will be able to benefit from my labors. I finally got it to work. Here is the js function:

 

function copyContent(field1, field2) {
  document.getElementById(field1).value = document.getElementById(field2).value;
}

 

Here is the html form:

 

<form action="article/<?php echo $id; ?>#comments" method="post">
  New comment:<br />
  <textarea id="content0" name="content" rows="5" cols="69"></textarea>
  <p>
    <input type="submit" name="insert" value="Save" /> <input type="submit" name="cancel" value="Cancel" />
  </p>
</form>

 

Then, I get all the comments from a mysql database. Before the while loop, I set the php var $i = 1. Then, this html form is inside the loop, followed by a link that executes the function:

 

<form>
  <input id="content<?php echo $i; ?>" type="hidden" value="<quote><?php echo htmlentities(strip_tags($content)); ?></quote>" />
</form>
<a href="javascript:void()" onClick="copyContent('content0', 'content<?php echo $i; ?>');">Quote</a>

 

Finally, I increment the php var $i ++.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.