如何将javascript参数传递给php

如何将javascript参数传递给php,javascript,php,html,Javascript,Php,Html,我打开模式窗口,从myFrom获取数据,并将电子邮件字段值保存到index.php中的我的javascript中。如何在不刷新index.php窗口的情况下,将javascript值解析为php并用echo显示 index.php <script type="text/javascript"> function opennewsletter() { emailwindow=dhtmlmodal.open('EmailBox', 'iframe', 'newsletter.php

我打开模式窗口,从
myFrom
获取数据,并将电子邮件字段值保存到
index.php
中的我的javascript中。如何在不刷新
index.php
窗口的情况下,将javascript值解析为php并用echo显示

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;
?>
<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>

函数opennewsletter()
{emailwindow=dhtmlmodal.open('EmailBox','iframe','newsletter.php','newsletter注册页','width=350px,height=200px,center=1,resize=0,scrolling=1')
emailwindow.onclose=function()
{//定义关闭窗口时要运行的自定义代码
var theform=this.contentDoc.forms[0]//访问iframe中的第一个表单仅供参考
var a=this.contentDoc.getElementById(“emailfield”).value;//在iframe中使用id=“emailfield”访问表单字段
返回true//允许关闭窗口
}
}//结束“opennewsletter”功能
newsletter.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;
?>
<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>
注册我们的时事通讯!
请输入您的电子邮件地址:


我想可能需要对服务器端客户端架构有一些了解

通常,您在浏览器中输入url,这会在服务器中触发某些PHP(和其他服务器端)代码在执行任何javascript代码之前很久就在服务器中远程执行,然后,它通过http连接发送一些资源,在本例中,是一个包含一些javascript代码的html页面。。。把它想象成通过连接发送的文本

然后——通常——PHP代码就完成了

之后,http连接器发送的html文件到达客户端浏览器,客户端浏览器打开它,绘制它并运行javascript代码

无法将变量从javascript发送回原始php执行实例,因为不存在

相反,您需要再次从浏览器调用服务器上的其他url,该url将在不同的执行实例中运行相同或其他php代码。如果将a的值作为参数附加到url请求,那么该值将在PHP代码中接收,然后您可以在服务器端使用它

有很多方法可以打开/调用服务器端url,例如,您可以使用AJAX:

您还需要从附加到url请求的客户端javascript代码发送参数,然后在php代码开始运行时在服务器端接收参数。例如,检查


还要注意,我提到“通常”。有一些方法可以使服务器端客户端的“常规”流产生一些黑暗魔法和交换、更改、变形。。。有框架和技巧,但首先可能更好地理解“通常”的流程。

而不刷新index.php窗口?Ajax可能是您的朋友关于您在评论中提到的php代码“我希望上面的javascript变量'a'值存储在这里”,似乎没有声明任何avariable@MD.JubairMizan对不起,我留下了javascript代码。现在编辑