NicoleSigaud Posted October 11, 2014 Share Posted October 11, 2014 Hello, I would like to create a window through JavaScript with PHP code that would insert data into a database, but I don't know if it's possible, because I tried everything I could so far and had no results at all. The code I am using is <script language="JavaScript"> <!-- top.consoleRef = new Object(); top.consoleRef.closed = true; function writeConsole(content) { top.consoleRef=window.open('em_branco.php','myconsole','width=350,height=250') top.consoleRef.document.writeln( '<html><head><title>página nova</title></head><body><?php $db = pg_connect("host=localhost dbname=dpf_db user=postgres password=asdasd"); $query = "INSERT INTO tbl_dummy(nome) VALUES(' + '´treco´' + ')"; $result = pg_query($query); if (!$result) { $errormessage = pg_last_error(); echo "Error with query: " . $errormessage; exit(); } printf ("These values were inserted into the database"); pg_close(); ?></body></html>' ) //top.consoleRef.document.close() } //--> </script> Note that line 13 has a sort of aberration, because I'd need three kinds of quotes, and not only two (the +'´treco´' stuff). And I don't know how to circumvent this, because escaping didn't work either. Any help is most welcome, and thank you in advance for your attention. Nicole Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 11, 2014 Share Posted October 11, 2014 The easy way is to crate the page in a php script and then use js to open the window and pass control to that script. You can easily find many examples of this on google. Quote Link to comment Share on other sites More sharing options...
NicoleSigaud Posted October 11, 2014 Author Share Posted October 11, 2014 The easy way is to crate the page in a php script and then use js to open the window and pass control to that script. You can easily find many examples of this on google. The problem is: I have a form in a page which has a script that creates as many form fields as the user needs. After creating these (numbered) extra fields, everyone of them needs to send data to a php page that inserts into the database what is in these fields. To accomplish that I assumed that a script in JS would then create/open windows with php code written on-the-fly to insert the data. If I create a static php page this would not work, because the extra fields are numbered and therefore would need to pass the contents to an equally differentiated page. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 12, 2014 Share Posted October 12, 2014 Your English is not clear enough to me. Quote: 'script in JS would then create/open windows with php code' makes no sense. One can't have php code inside a js script. What are you trying to describe here? Again - you use js to open the window and pass control to the script named as one of the parms of the JS open command. You simply have to port all of your curren tphp code from the calling script to the JS-called script. Quote Link to comment Share on other sites More sharing options...
NicoleSigaud Posted October 12, 2014 Author Share Posted October 12, 2014 Your English is not clear enough to me. Quote: 'script in JS would then create/open windows with php code' makes no sense. One can't have php code inside a js script. What are you trying to describe here? Again - you use js to open the window and pass control to the script named as one of the parms of the JS open command. You simply have to port all of your curren tphp code from the calling script to the JS-called script. I apologyse for not writing English as well as I should, but now you answered one part of what I was asking - "One can't have php code inside a js script." - I did not know that. The method you described later ("have to port all of your curren tphp code from the calling script to the JS-called script.") was something I could not understand, sorry. Let's see... I call a new windows through JS. This new window already exists inside the directory or it's created on the fly? Inside this new window I have a php code, right? Sorry for not undestanding very well... Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 12, 2014 Share Posted October 12, 2014 Try googling how to use js to open a window and you will see how you can generate a window of the size you want as well as trigger a php script to take over that window. I think that is what you want. 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.