How to Write JavaScript Code Inside PHP

how to write javascript code inside php

Just echo the javascript out inside the if function

 <form name="testForm" id="testForm"  method="POST"  >
<input type="submit" name="btn" value="submit" autofocus onclick="return true;"/>
</form>
<?php
if(isset($_POST['btn'])){
echo "
<script type=\"text/javascript\">
var e = document.getElementById('testForm'); e.action='test.php'; e.submit();
</script>
";
}
?>

how to write javascript inside php tag

what I was doing wrong is , I was echoing the js in parts. What I needed to was something like this

    <?php
echo " <script>
$('#img').attr(\"src\",\"getImage.php?id=\"+2);
$('#img').show();
</script> ";
?>

whole js in just 1 peice is the key here.

how to write javascript code within php

You have to echo the whole JScript function because PHP doesn't know what you're doing:

echo "<script type = 'text/javascript'>function GetCellValues()
{
//echo('Save'); use document.write()?
var str = '';
var rows = document.getElementsByTagName('tr');
var table=document.getElementById("project");
for (var i=0;i<table.rows[0].cells.length;i++)
{
str = str + (table.rows[0].cells[i].innerHTML) + ', ' ;
}
for (var c = 1 ; c < rows.length ; c++)
{
str += '\n' + "0" + c + ', ';
var row = rows[c];
var inputs = row.getElementsByTagName('input');
for (var k = 0 ; k < inputs.length ; k++)
{
str += inputs[k].value + ', ';
}
}
}</script>";

Use javascript code inside php

Javascript and PHP are two separated things, while JS runs in the client, PHP will run in the server.
So:

<script>var showdiv1  = 0;</script>     
--- some html codes ---------
<script>showdiv1 = showdiv1 + 1;</script>

Will create a JS variable with value of 1. PHP have no idea of this variable so cant be used there.

The line <div class="col-md-12 box" id= "div<?php echo "<script type='text/javascript'>showdiv1</script>"?>"><br /> behave as expected, printing <div id="div<script type='text/javascript'>showdiv1</script>" class="col-md-12 box">

Maybe what you are trying to accomplish is:

<?php $showdiv1  = 0; ?>     
--- some html codes ---------
<?php $showdiv1 = $showdiv1 + 1; ?>
<div class="col-md-12 box" id= "div<?php echo $showdiv1 ?>"><br />

How to call a JavaScript function from PHP?

As far as PHP is concerned (or really, a web server in general), an HTML page is nothing more complicated than a big string.

All the fancy work you can do with language like PHP - reading from databases and web services and all that - the ultimate end goal is the exact same basic principle: generate a string of HTML*.

Your big HTML string doesn't become anything more special than that until it's loaded by a web browser. Once a browser loads the page, then all the other magic happens - layout, box model stuff, DOM generation, and many other things, including JavaScript execution.

So, you don't "call JavaScript from PHP", you "include a JavaScript function call in your output".

There are many ways to do this, but here are a couple.

Using just PHP:

echo '<script type="text/javascript">',
'jsfunction();',
'</script>'
;

Escaping from php mode to direct output mode:

<?php
// some php stuff
?>
<script type="text/javascript">
jsFunction();
</script>

You don't need to return a function name or anything like that. First of all, stop writing AJAX requests by hand. You're only making it hard on yourself. Get jQuery or one of the other excellent frameworks out there.

Secondly, understand that you already are going to be executing javascript code once the response is received from the AJAX call.

Here's an example of what I think you're doing with jQuery's AJAX

$.get(
'wait.php',
{},
function(returnedData) {
document.getElementById("txt").innerHTML = returnedData;

// Ok, here's where you can call another function
someOtherFunctionYouWantToCall();

// But unless you really need to, you don't have to
// We're already in the middle of a function execution
// right here, so you might as well put your code here
},
'text'
);

function someOtherFunctionYouWantToCall() {
// stuff
}

Now, if you're dead-set on sending a function name from PHP back to the AJAX call, you can do that too.

$.get(
'wait.php',
{},
function(returnedData) {
// Assumes returnedData has a javascript function name
window[returnedData]();
},
'text'
);

* Or JSON or XML etc.

How to include in .php a .html with links to a .js that has php codes

In your PHP file, create a global variable containing your JSON in a tag:

<script>var myNum = <?php echo json_encode($value); ?>;</script>

and then reference that variable in your script file with myNum.

how to add script inside a php code?

You can just echo all the HTML as normal:

<?php
echo '<input type="button" onclick="alert(\'Clicky!\')"/>';
?>


Related Topics



Leave a reply



Submit