6 Replies - 3760 Views - Last Post: 17 August 2010 - 04:24 PM Rate Topic: -----

#1 CodeVillain  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 143
  • Joined: 10-July 10

Transfer PHP variable to JavaScript?

Posted 16 August 2010 - 03:31 PM

Well, what I'd like to do is transfer the value of my PHP variable to Javascript so I can create a function.

To be more specific, I have my PHP variable pull a value from my MySQL database, and I need to create an onclick function for an input button to display an alert if the value is less than a certain number.

I apologize if this is better suited in the Javascript section, but my thinking based on typical web development tutorial orders is that someone skilled in PHP is more likely to know Javascript than someone skilled in Javascript is to know PHP.

Anyway, can anybody offer any help? All I need to do is know is how to turn the value of a PHP variable into a Javascript one so I can more easily create onclick functions.

Is This A Good Question/Topic? 0
  • +

Replies To: Transfer PHP variable to JavaScript?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,548
  • Joined: 23-August 08

Re: Transfer PHP variable to JavaScript?

Posted 16 August 2010 - 03:41 PM

Javascript is in the page sent to the browser by PHP, so something like this will:

function showMessage(val)
{
    alert("Hello " + val);
}

<a onclick="showMessage(\"<?php echo $my_php_var; ?>\");" />


if $my_php_var is "CodeVillain", will end up in the browser as

<a onclick="showMessage(\"CodeVillain\");" />


and clicking on the link will display the alert message with "Hello CodeVillain".
Was This Post Helpful? 1
  • +
  • -

#3 CodeVillain  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 143
  • Joined: 10-July 10

Re: Transfer PHP variable to JavaScript?

Posted 16 August 2010 - 03:59 PM

Hmm, either I'm not understanding this or I didn't make myself clear.

What I need to do is have the function only display an alert if the value of the PHP statement is less than a certain number.

I know this code can't possibly work, but it may give you a better idea of what it is I'm' trying to do.

<script>
 var1 = <?php $var1 ?>
function()
{
if var1 < 1500
{
alert("You need more credits!");
}
}
</script>
<input type="button" value="button" onclick="function()">


This post has been edited by CodeVillain: 16 August 2010 - 03:59 PM

Was This Post Helpful? 0
  • +
  • -

#4 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: Transfer PHP variable to JavaScript?

Posted 16 August 2010 - 10:41 PM

This
var1 = <?php $var1 ?>;
Should be this
var1 = <?php echo $var1; ?>
You should probably read up some tutorials on PHP if you're having difficulty figuring out something as simple as an echo statement.
Was This Post Helpful? -1
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3554
  • View blog
  • Posts: 10,335
  • Joined: 08-June 10

Re: Transfer PHP variable to JavaScript?

Posted 16 August 2010 - 10:56 PM

if the condition is on the PHP side, let PHP choose to print or not to print the call.

PS. function is a reserved keyword and cannot be used as variable name.
Was This Post Helpful? 1
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,548
  • Joined: 23-August 08

Re: Transfer PHP variable to JavaScript?

Posted 17 August 2010 - 03:39 AM

One way to do it:
<?php
if (!isset($_POST['submit']))
    $val = 2000;
else
    $val = $_POST['val'];
?>
<html>
<head>
<?php if ($val < 1500) { ?>
<script type='text/javascript'>
function alertIfLessThan1500()
{
    alert('Less than 1500');
}
<?php } ?>
</script>
</head>
<body onload="<?php if($val < 1500) echo 'alertIfLessThan1500()'; ?>">
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
<input type="text" name="val" />
<input type="submit" name="submit" />
</form>
</body>
</html>


Was This Post Helpful? 1
  • +
  • -

#7 CodeVillain  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 143
  • Joined: 10-July 10

Re: Transfer PHP variable to JavaScript?

Posted 17 August 2010 - 04:24 PM

View Postgrimpirate, on 16 August 2010 - 08:41 PM, said:

This
var1 = <?php $var1 ?>;
Should be this
var1 = <?php echo $var1; ?>
You should probably read up some tutorials on PHP if you're having difficulty figuring out something as simple as an echo statement.


Umm... no, it was just a typo... var = <?php $var; ?> doesn't even work anyway, if you bothered to read what I said I was giving it as an example of what I wanted to do for the sake of better explaining myself, it was not being presented as functional code. Please properly read people's threads before replying. =\

Thanks for the help though guys, I'd never used the print function before and didn't even think to use it.

Thanks for the help guys, I'd completely forgotten I could just use the print function to implement code like that, I haven't used it since originally taking a PHP tutorial. The help was very much appreciate.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1