-1

I do pop modal window, and get data from myFrom and save the email field value to my javascript in index.php. How can I parsing the javascript value in to php and display it with echo, without refreshing the index.php window ?

index.php

<script type="text/javascript">

function opennewsletter()
{   emailwindow=dhtmlmodal.open('EmailBox', 'iframe', 'newsletter.php', 'Newsletter Signup page', 'width=350px,height=200px,center=1,resize=0,scrolling=1')

    emailwindow.onclose=function()
    { //Define custom code to run when window is closed
        var theform =this.contentDoc.forms[0] //Access first form inside iframe just for your reference
        var  a = this.contentDoc.getElementById("emailfield").value; //Access form field with id="emailfield" inside iframe

        return true //allow closing of window   
    }

} //End "opennewsletter" function

</script>
<a href="#" onClick="opennewsletter(); return false">Signup for our newletter</a> 
<?php
    $value = a;  //I want above javascript variable 'a' value to be store here
    echo $value;
?>

newsletter.php

<h4>Sign up for our newsletter!</h4>
<form id="myform" name="myform" method="post" >
  <p>Enter your email address please:<br>
  <input type="text" id="emailfield" name="emailfield" size="30" />
  <input type="submit" id="submit" name="submit"  value="Ok" onClick="parent.emailwindow.hide()" /></p>
</form>
user1039663
  • 1,230
  • 1
  • 9
  • 15
darmono
  • 11
  • 2

1 Answers1

0

I guess that some understanding about the serverside-clientside arquitecture maybe is needed.

Usually you enter an url in the browser and this can trigger in the server that some PHP (and other serverside) code is executed remotely in the server long before any javascript code is executed and then, as result, it sends thru the http connection some resource, in this case a html page with some javascript code... think on it like some text sent thru the connection.

Then -usually- the PHP code is finished.

After that the html file sent by the http connector arrives at the clientside browser which opens it, draws it and runs javascript code.

You cannot send back variables from javascript to the original php execution instance because is not there.

Instead you need to call again from the browser to other url on the server which luckily will run the same or other php code in a different execution instance. If you attach to the url request the value of a as a parameter, then the value will be received in the PHP code and then you can work in the serverside with it.

There are plenty ways to open/call a serverside url to do it, you can use AJAX for example: https://en.wikipedia.org/wiki/XMLHttpRequest

Also you will need to send the parameter from the clientside javascript code attached to the url request and after that receive it in the serverside when php code starts to run. Check for example jQuery Ajax POST example with PHP

Also note that I mention "usually". There are ways to make some dark magic and swap, change, deform the "usual" flow of the serverside-clientside... there are frameworks and tricks, but first probably is better to understand the "usual" flow.

user1039663
  • 1,230
  • 1
  • 9
  • 15