rpjd Posted May 2, 2011 Share Posted May 2, 2011 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? Quote Link to comment Share on other sites More sharing options...
spiderwell Posted May 2, 2011 Share Posted May 2, 2011 javascript is almost always executed client side, and php is executed serverside. so what are you trying to achieve in the javascript? can it not be done in php? Quote Link to comment Share on other sites More sharing options...
rpjd Posted May 2, 2011 Author Share Posted May 2, 2011 I want to add a new row to a table. Quote Link to comment Share on other sites More sharing options...
spiderwell Posted May 2, 2011 Share Posted May 2, 2011 an html table? does the js execute after all the php stuff is done because your example should work then when page is pushed to client as i did something similar once attaching a ckedit js object to a textarea Quote Link to comment Share on other sites More sharing options...
rpjd Posted May 2, 2011 Author Share Posted May 2, 2011 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. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted May 2, 2011 Share Posted May 2, 2011 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. Quote Link to comment Share on other sites More sharing options...
rpjd Posted May 2, 2011 Author Share Posted May 2, 2011 The form is on a php webpage. After processing whats in $_POST in a seperate script, how/where do I then call the javascript function? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted May 2, 2011 Share Posted May 2, 2011 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 Quote Link to comment Share on other sites More sharing options...
rpjd Posted May 2, 2011 Author Share Posted May 2, 2011 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. Quote Link to comment Share on other sites More sharing options...
spiderwell Posted May 2, 2011 Share Posted May 2, 2011 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 Quote Link to comment Share on other sites More sharing options...
rpjd Posted May 2, 2011 Author Share Posted May 2, 2011 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? Quote Link to comment Share on other sites More sharing options...
spiderwell Posted May 2, 2011 Share Posted May 2, 2011 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? Quote Link to comment Share on other sites More sharing options...
rpjd Posted May 2, 2011 Author Share Posted May 2, 2011 Does the javascript function to add rows to html tables on a page have to be called from the actual page (even if its using php) or can it be called from an external php file that proceses the form? Quote Link to comment Share on other sites More sharing options...
rpjd Posted May 2, 2011 Author Share Posted May 2, 2011 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? Quote Link to comment Share on other sites More sharing options...
jcbones Posted May 2, 2011 Share Posted May 2, 2011 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). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.