How to use a PHP code in JavaScript [closed]
I want to use PHP code in JavaScript, but it doesn't work:
<script type="text/javascript">
function edit(pollNo)
{
<?php
$result = mysql_query( 'CALL ret_poll('.pollNo.')' );
开发者_Python百科 $row = mysql_fetch_assoc($result);
echo 'document.poll.pollTitle.value='.$row['title'];
?>
}
</script>
You could use jQuery to perform an AJAX request to a PHP page that executes the PHP code and pass along the arguments from the JavaScript function. Check out the jQuery website for more information on this subject: http://api.jquery.com/category/ajax/
The big problem in your code is that JavaScript runs on the Client Side and PHP runs on the Server Side. That's why it's impossible to mix these two like this.
I wrote this for you, but I haven't tested it. I'm pretty sure it gives you an idea of how to implement this:
getpollinfo.php
<?php
if ( IsSet( $_GET['poll'] ) ) {
/* Connect to MySQL here */
$result = mysql_query( 'CALL ret_poll(' . intval( $_GET['poll'] ) . ')' );
$row = mysql_fetch_assoc($result);
echo json_encode($row);
}
?>
JavaScript code (assuming you've loaded the jQuery framework)
function edit(pollNo)
{
$.get('getpollinfo.php', { 'poll' : pollNo }, function(data) {
$('#pollTitle').html(data.title);
});
}
Javascript = client side.
Php = server side.
You can't combine them the way you use, you will need to use an ajax to do what you want.
The sequence will be:
- javascript: your edit function
- javascript: ajax request to php script
- php: perform query and return result
- javascript: handle the response
First you have to understand that PHP runs on the server side and JavaScript on the client. Hence you cannot directly invoke PHP code in a JavaScript function by just putting it there.
When the client makes a request, the PHP interpreter executes the code and produces some output. This output is sent to the browser and there the JavaScript code is executed. PHP's job is already done at this point.
You achieve what you want with Ajax (though I think you have to learn more about JavaScript, PHP and HTTP first).
But to give you an example (using the jQuery library):
function edit(pollNo){
$.get('get_poll.php',{id: pollNo}, function(data) {
$('input[name="pollTitle"]').val(data);
});
}
Where your PHP file looks like:
// db connection...
$result = mysql_query( 'CALL ret_poll('.mysql_real_escape_string($_GET['id'].')' );
$row = mysql_fetch_assoc($result);
echo $row['title'];
Javed, its not that you can't use php in javascript. However the uses are very limited.
See an example
alert('<?php echo "hi"; ?>');
Once the server renders this, the script will be
`alert('hi');` //SO it will alert `hi` now
Mostly, you can use PHP and javascript mixture to create a script you want to run on the run time
For example
<script>
<?php
$location = $_SESSION['logged'] ? 'admin.php' : 'index.php';
?>
window.location('<?php echo $location; ?>');
</script>
Or Something like this
<script>
<?PHP if($test==true) { ?>
function test() {
//do something;
}
<?php } else { ?>
function test() {
//do something else but not the previous one
}
<?php } ?>
</script>
However, What you are trying is TRYING TO USE PHP CODES AS JAVASCRIPT Which is not possible !!!!!!!!
Instead, you can use AJAX, as other answers suggest, for a workaround.
A little demo of your probable solution is
FILE: myMYSQLPROCESSING.php
// Your connections and other above
$result = mysql_query( 'CALL ret_poll('.$_POST['pollno'].')' );
$row = mysql_fetch_assoc($result);
echo $row['title'];
JS
function edit(pollNo)
{
$.post("myMYSQLPROCESSING.php",
{ "pollno" : pollNo },
function(data) {
// Now data, will contain whatever you echo in myMYSQLPROCESSING.php
// May be a success message or error message
// and do whatever you want with the response
document.poll.pollTitle.value=data; //in your case something like this
}
);
}
This is a jQuery code, if you are not familiar with it.
You cannot. And you'll never be able to. It would break a very big hole in the security wall. By the way you can use the AJAX technique and call a PHP file via Javascript.
References:
- AJAX
If you need to do what your saying, you will end up just rendering the page with that specific Javascript code version.
If you really want to do that you have to look at the output to see that you need to quote strings in Javascript.
<script type="text/javascript">
function edit(pollNo)
{
<?php
$result = mysql_query( 'CALL ret_poll('.pollNo.')' );
$row = mysql_fetch_assoc($result);
echo 'document.poll.pollTitle.value="'.$row['title'].'"';
?>
}
</script>
Your browser will end up with:
<script type="text/javascript">
function edit(pollNo)
{
document.poll.pollTitle.value="somehardcodedtext";
}
</script>
You will have to consider escaping quote symbols in the output of $row['title']
and whether you would rather just call the server using AJAX instead.
精彩评论