0

I have to pass a string variable created in php to Javascript function to open form child using open.window. I have code like this

<html>
<head>
<script LANGUAGE="JavaScript" TYPE="text/javascript">
    function openWindow(v){
        WREF = window.open("test2.html?cad=e&cid="+v,"koreksi","width=550,height=350,top=50");
        if(!WREF.opener){ WREF.opener = this.window; }
    }
</script>
</head>

<body>
<?php $var='Red'; ?>
<a HREF="javascript:void(0);" onClick="openWindow(<?php $var='Red'; ?>);">Open the window</a>
</body>
</html>

By those code, i hope i can get an address like this http://localhost/adduser.php?cad=e&cid=red

Can you help me ? ( sorry i can't describe clearly for my q )

2 Answers 2

1

you can use <?php echo $var;?> to pass variable

this line should be:--

<a href="javascript:void(0);" onClick="openWindow('<?php echo $var;?>');">Open the window</a>
Sign up to request clarification or add additional context in comments.

1 Comment

This will result in a javascript error, as $var should be enclosed in quotes.
0

You need to enclose the php variable in single quotes and echo it:
<a HREF="javascript:void(0);" onClick="openWindow('<?php echo $var ?>');">Open the window</a>

Edit: the single quotes are needed as the PHP echo will render the actual string, which will result in a javascript function call like: openWindow(http://localhost/adduser.php?cad=e&cid=red), which is not valid syntax; on the other hand openWindow('http://localhost/adduser.php?cad=e&cid=red') is.

4 Comments

Thank you for your advice, and i tried to changed my code like this : <body> <?php $var='Red'; ?> <a HREF="javascript:void(0);" onClick="openWindow('<?php echo $var; ?>');">Open the window</a> </body> unfortunately, the results of the link is not as I expected. This is the result link : localhost/adduser.php?cad=e&cid=<?php echo $var; ?> What is wrong on my code above ?
Thank you for response. I think there is not any interpreter. The first php file is the above script ( my question above ). And the second file is like this : <?php $cadd = mysql_real_escape_string($_GET['cad']); $cuid = mysql_real_escape_string($_GET['cid']); echo $cadd; echo $cuid; ?>
i used xampp. i acces the page from localhost/test1.html. Test1.html is the name of my php file
Yes .... thank you, you are correct. I changed test1.html to test1.php. Thank your advice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.