Jump to content

Recommended Posts

I am trying to call a javascript function from a php script.  This is the approach I'm using.  I have an alert in the function, just to confirm the function executes.

echo "<script type='text/javascript' src='http://localhost/Project/JScript/File.js'>";
echo "<script type='text/javascript'>functionName();</script>";

Is this the correct approach?

Link to comment
https://forums.phpfreaks.com/topic/235358-calling-javascript/
Share on other sites

I should have clarified that, yes, a html table.  When a form on another page is submitted, depending on the contents of $_POST the php script checks the data submitted, calls the javascript function which adds a new row with $_POST data to a table on the action page before displaying the action page.  At the moment, I only have an alert in the function, until I'm sure the javascript function is executing.  But as yet, the javascript is not responding.   

Link to comment
https://forums.phpfreaks.com/topic/235358-calling-javascript/#findComment-1209513
Share on other sites

PHP cannot call javascript functions and vise versa. By the time the javascript code is executed PHP has finished. PHP is executed on the server and the results of the PHP script is sent back to the http server The http server sends the text/html/javascript/css code to the web browser. The web browser then renders the webpages.

 

You could use AJAX which will allow to pass data from javascript to PHP on the fly without having to reload your page.

 

However if all you want to do is add a new row to a table you can just use solely javascript for this. You don't need PHP for it.

Link to comment
https://forums.phpfreaks.com/topic/235358-calling-javascript/#findComment-1209517
Share on other sites

Could you explain what your form is, what your script does with the data it gets from your form and why the need to call a javascript function from PHP - which I have already explained you cannot do. You can use javascript (via AJAX) to call PHP but you cannot use PHP to call javascript

Link to comment
https://forums.phpfreaks.com/topic/235358-calling-javascript/#findComment-1209523
Share on other sites

On page 1, the user selects information by selecting various checkboxes.  Those selected if any, are displayed on page 2 in another form which in turn when submitted, may require one or more rows to be added to various tables on page 3.  Pages and 2 are both working fine, I just need to call the javascript function based on whats in $_POST from page 2.  If I can get that working that would be great. 

Link to comment
https://forums.phpfreaks.com/topic/235358-calling-javascript/#findComment-1209529
Share on other sites

You can use PHP to include or not include javascript functions in a compiled page, as I have done that and the OP code does just that, but as wildteen states, actually calling them from one language to another cannot be done

 

if you are basing part 3 on $_POST data from part 2, to create the form, you shuoldn't need JS at all, as all the info you need to create the form 'should' be available in php and be executeable that way.

posting code of the forms so far would help out a lot

Link to comment
https://forums.phpfreaks.com/topic/235358-calling-javascript/#findComment-1209548
Share on other sites

The form on page 2 is created when the form on page 1 is submitted, theres no actual html code as such on page 2, and page 2 cannot be accessed unless the user is logged in and submits data from page 1.  The form on page 2 is of this nature.

<div>
<form method='post' action='file.php'>
<table>
<tr><td>$Ref</td><td>$Text</td></tr>
<tr><td></td><td><textarea name='text[]'></textarea></td></tr>
<tr><td colspan='2'><input type='submit' value='Submit'></td></tr>
</table>
</form>
</div>

The form code on page 2 is written by php when page 1 is submitted.  So when the form on page 2 is processed, I need to call a javascript function to add one or more new rows to html tables on page 3.  How that is done is my question?

Link to comment
https://forums.phpfreaks.com/topic/235358-calling-javascript/#findComment-1209568
Share on other sites

i think i am getting more confused

 

does the user input something on page 2 in the text area that determines the extra rows in page 3?

 

 

relaying what I said earlier, and as from your 1st post you can trigger a js function by echoing it into the page via php, and even pass variables to it like this

 

$passinginfo = $_POST['textarea'];
echo "<script type='text/javascript'>functionName(" . $passinginfo . ");</script>";

but the jscript will only , as wildteen states, affect the compiled html page, and not affect the php

but if you know in javascript how many extra rows to echo, why not just do that in php?

 

 

Link to comment
https://forums.phpfreaks.com/topic/235358-calling-javascript/#findComment-1209572
Share on other sites

To complicate things ever so slightly, when(if) I do get the javascript to run, as I said previously, the number of rows depends on the size of a variable array in $_POST so I need to convert the php array, say $var into a javascript array, iterate through it to insert the POST values into the new rows being created.  What I'm thinking is process the submission externally from the webpage, re-direct to the webpage, call the javascipt using onload, convert the processed $_POST elements to javascript variables, then create rows using a for loop iterator.  Is this viable?  If not, what would be a practical approach?

Link to comment
https://forums.phpfreaks.com/topic/235358-calling-javascript/#findComment-1209661
Share on other sites

All of your problems could be solved much quicker, if you posted the actual code that you are working with.  Your explanations are confusing, and you will not get desired info from the responses. 

 

FACTS:

PHP is a server side programming language.

Javascript is a client side programming language.

HTML is a markup language that is interpreted by your browser.

 

PHP can write HTML.

Javascript can write HTML.

 

Programming languages cannot interact with each other, they speak different languages, therefore they don't understand each other.

Therefore:

PHP cannot call Javascript, it can only write it to the browser.

Javascript cannot call PHP, it can only send a request to the server.

 

In the middle of that mix, is where you use AJAX (Asynchronous Javascript And XML), to send a request to the server (via javascript), to a php script, and print the returned value (from the php script) back to the HTML page (via javascript).

Link to comment
https://forums.phpfreaks.com/topic/235358-calling-javascript/#findComment-1209677
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.