Jump to content

php into javascript


HAN!

Recommended Posts

You could always have

 

<input type="hidden" name="name" value="<?php echo $variable; ?>"/>

 

And then use

 

var variable = document.form.name.value;

 

That seems about the easiest way to do it, but I'm not sure if that could cause any security problems. I'd wait for somebody else's opinion on this. This is just an idea.

Link to comment
Share on other sites

make sure your output is in the correct syntax and you should be find.

 

<script language="javascript">
      // if the variable might contain " mark
      var something = "<?php echo str_replace("\"", "\\\"", $var_goes_here);?>";

      // if the variable is clean
      var something = "<?php echo $var_goes_here;?>";

     // if the variable is a number
     var something = <?php echo $var_goes_here;?>;
</script>

 

When you view the page, view the source code to make sure it's correct

Link to comment
Share on other sites

well thanks for these notes but i tried them and nothing worked can u take a look at my code to see if im missing somthing plz, thanks

<?PHP 
$user='hani';
$pass='annd';
?>
<script language="JavaScript"> 
<!-- 
function validate(form) {

var u = "<?php echo str_replace("\"", "\\\"", $user);?>";
var p = "<?php echo str_replace("\"", "\\\"", $pass);?>";

if (form.username.value!=u)
)
{
alert("Please enter the correct username");
form.username.focus();
return false;
}
if(form.password.value!=p)
{
alert("Please enter the correct password");
form.password.focus();
return false;
}
}
//--> 
</script>
</head>

Link to comment
Share on other sites

fist off do u look at the javascript errors ?, secondly u have a ) @ f (form.username.value!=u)

)

{ that dosent make sense. you also have  <!-- comment starting may disrupt ur code

 

if ur using ie js errors are at the bottom left small tiny icon double click it, in firefox top right, dont worry about string replace just yet use the bare variables until u can get a result

Link to comment
Share on other sites

I'm pretty sure you cannot break into <?php ?> within your <script></script> tags.

 

You need to do something like this:

<html>
<head>
<script language="JavaScript"> 
<!-- 
function validate() {

var u = document.my_form.hide_user.value;
var p = document.my_form.hide_pass.value;

if (document.my_form.user_name.value != u) {
alert("Please enter the correct username");
document.my_form.user_name.focus();
return;
  }
if (document.my_form.password.value != p) {
alert("Please enter the correct password");
document.my_form.password.focus();
return;
  }
}
//--> 
</script>
</head>
<body>
<?PHP 
$user='hani';
$pass='annd';
?>
<form name="my_form" action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="hide_user" value="<?php echo $user; ?>">
<input type="hidden" name="hide_pass" value="<?php echo $pass; ?>">
Username: <input type="text" name="user_name" value="" size="10">
Password: <input type="text" name="password" value="" size="10">
<a href="javascript:validate()" title="Submit" tabindex="65">Submit</a>
</form>
</body>
</html>

Link to comment
Share on other sites

Agreed, As PHP is server-side, by the time javascript is parsed at the local browser, the PHP has done everything it needs to do.

 

The way I do it is save the script in an external .php file, and use:

<script language="Javascript" src="path_to_php_file"></script>

 

This works for me.

Link to comment
Share on other sites

when i use php in javasquipt i just

 

<script>

    var jsvariable = <?php $somthing; ?>

 

</script>

 

its just that u wont see any highlighted code so u have to know what ur doing but u wont realy need it for much

 

or <a href="javascript:boob('<?php $myFave; ?>')">

 

emagine php as writing your html out for u before it gets to your browser it will fill in all the blanks

Link to comment
Share on other sites

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.