Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

SOLVED IT

<?php $startPage = $_COOKIE["currentPage"];?>
<script type="text/javascript">
var startPageVal = <?php echo $startPage; ?>;
<?php echo base64_decode($pajinate_js); ?>
</script>

while $pajinate_js is the code below but instead startPage : startPageVal


I am having trouble figuring out how to add JavaScript from a form. Basically I have code such as this (ignore the script itself rather than one line (startPage):

$(function(){
 /* initiate the plugin */
 $("div.holder").jPages({
     containerID  : "content",
     perPage      : 2,
     startPage    : <?php echo $_COOKIE["currentPage"]; ?>,
     startRange   : 1,
     midRange     : 5,
     endRange     : 1,
     }      
});

if I place the code inside PHP page immediately I get startPage : 3 (or whatever value I get) but if I do it via Textarea dynamically in the browser source I get plain text:

startPage : <?php echo $_COOKIE["currentPage"]; ?> 

which will, as a result, cause the whole thing not to work. I am using base64_decode and base64_encode to pass it through the database, otherwise mysql insert statement doesn't work.

Hope you understood what i meant. Does anybody know how to get around it?

share|improve this question
 
Little confused - are you suggesting you have PHP code stored within your database and are trying to evaluate it after a query? Because that would be an extremely bad practice. The value of your cookie should be resolved in whichever file you have writing that JS to the DB. –  Emissary Mar 22 at 16:12
 
there is nothing too complicated as you think. All I am trying to do is add javascript to the database so that it can be custom on the page depending on the user on the page. I solved the issue differently anyway by setting javascript var and then using it in the code instead of startPage : <?php echo $_COOKIE["currentPage"]; ?> i set it var jstartPage = <?php echo $_COOKIE["currentPage"]; and in the code startPage : jstartPage and it worked. –  Marty Balandis Mar 22 at 16:32

4 Answers

Man , i have tested this example and it works,try to merge php and javascript like this :

<html>
<form  method="post" >
<input type="text" value="10" name="input">
<input type="submit" value='submit'>
</form>

<script type="text/javascript">
  var x = <?php echo $_POST['input']; ?> ;
  alert(x);
</script>
</html>
share|improve this answer
 
i have to do it via database. Sql Insert throws an error. So i encoded when inserting and decoded when echo on the page but it writes <?php echo $_COOKIE["currentPage"]; ?>, instead of the value i should get out of it. –  Marty Balandis Mar 22 at 15:52

It depends on the quotes you're using.

<?php

$js = '$(function(){
 /* initiate the plugin */
 $("div.holder").jPages({
     containerID  : "content",
     perPage      : 2,
     startPage    : '.$_COOKIE["currentPage"].',
     startRange   : 1,
     midRange     : 5,
     endRange     : 1,
     }      
});';
echo $js;

?>
share|improve this answer
 
i have to do it via database. Sql Insert throws an error. So i encoded when inserting and decoded when echo on the page but it writes <?php echo $_COOKIE["currentPage"]; ?>, instead of the value i should get out of it. –  Marty Balandis Mar 22 at 15:51
 
I guess I'm not understanding what you're trying to do. If I had to put this in a database I would take the var and mysqli_real_escape_string($js) or PDO::quote($js) depending on the method being used. –  AbsoluteƵERØ Mar 22 at 15:58

Try this:

<?php    
    $js = '<script>$(function(){
     /* initiate the plugin */
     $("div.holder").jPages({
         containerID  : "content",
         perPage      : 2,
         startPage    : '.$_COOKIE["currentPage"].',
         startRange   : 1,
         midRange     : 5,
         endRange     : 1,
         }      
    });<script>';
    echo $js;
?>
share|improve this answer

Okay, I think I get what the issue is - sorry it took a few minutes to sink in. You have your script/template or whatever stored in the database which requires a value from a PHP variable - only you can't resolve that variable before you write to the database because it's a cookie value which is specific to the user who is running the script. So you have written PHP code to the database expecting it to be evaluated after you have run your database query? Am I correct?

This isn't the way it works - and while it is "technically" possible to hack it together this way it would be extremely bad practice so...

What you really want to do is build the script on the fly. For example, your script could be stored as a string with a tag of some description which you swap out later on:

$(function(){
  /* initiate the plugin */
  $("div.holder").jPages({
  containerID  : "content",
  perPage      : 2,
  startPage    : %%COOKIEVAL%%,
  startRange   : 1,
  midRange     : 5,
  endRange     : 1,
  }      
});

Then after retrieving this from the database, you can run something like this:

$script = val_from_db(); // whatever
echo str_replace('%%COOKIEVAL%%', $_COOKIE["currentPage"], $script);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.