Jump to content

Create new window in JS with PHP code


NicoleSigaud

Recommended Posts

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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... 

Link to comment
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.