Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Javascript调用php函数并在php代码中使用Javascript变量_Javascript_Php_Primes_Prime Factoring - Fatal编程技术网

从Javascript调用php函数并在php代码中使用Javascript变量

从Javascript调用php函数并在php代码中使用Javascript变量,javascript,php,primes,prime-factoring,Javascript,Php,Primes,Prime Factoring,JavaScript function calcPrimesLoop() { var primes = document.getElementById('primes'); primes.appendChild(document.createTextNode('\n'+this.prime.nextPrime())); $.ajax({ url: "/test.php", type: "post", data: {prim

JavaScript

function calcPrimesLoop() {
    var primes = document.getElementById('primes');
    primes.appendChild(document.createTextNode('\n'+this.prime.nextPrime()));

    $.ajax({
        url: "/test.php",
        type: "post",
        data: {prime: this.prime.nextPrime()},
        success: function(data) {
        } 
    });

    calcPrimesDelay = setTimeout('calcPrimesLoop()', this.delay);
}
Php


我想这就是所有相关的脚本。我有一个脚本,可以得到素数,它的工作非常完美。但是现在我想把这些数字记录在一个文本文件中。这就是我试图做到的,但我没有成功。非常感谢。问题是这些数字没有被记录下来


我添加了一个Ajax正在工作的警报。但是当我向php脚本添加表单并提交时,它就可以正常工作了。因此,ajax和php脚本无法协同工作。

您应该了解ajax,了解如何使用Javascript将信息传递到服务器端页面并检索返回值


使用ajax和jQuery,它实际上很简单

function calcPrimesLoop() {
    var primes = document.getElementById('primes');
    primes.appendChild(document.createTextNode('\n'+this.prime.nextPrime()));

    $.ajax({
        url: "myScript.php",   // URL of your php script
        type: "post",
        data: {prime: this.prime.nextPrime()},
        success: function(data) {
            alert("success");
        } 
    });

    calcPrimesDelay = setTimeout('calcPrimesLoop()', this.delay);
}

myScript.php:

<?php
    $content = $_POST['prime'];
    ...

您应该明确地查找

您可以选择使用带有Javascript函数的AJAX,或者使用Javascript函数简化您的生活

以下是一个示例:

//STEP ONE: INCLUDE THE LAST VERSION OF JQUERY
<script src="http://code.jquery.com/jquery-latest.min.js"
    type="text/javascript"></script>
//STEP TWO, GET YOUR FUNCTION TO WORK:

function sendVariableTo(variable,url) {
    $.ajax({
        url:url, //Or whatever.php
        type: "GET", //OR POST
        data: { myVar: variable}, //On php page, get it like $_REQUEST['myVar'];
        success:function(result){
            //If the request was ok, then...
            alert(result) //Result variable is the php page 
            //output (If you echo "hello" this alert would give you hello..)
        },
    });
}
//第一步:包括JQUERY的最新版本
//第二步,让你的功能发挥作用:
函数sendVariableTo(变量,url){
$.ajax({
url:url、//或其他任何内容。php
键入:“GET”、//或POST
数据:{myVar:variable},//在php页面上,像$_请求['myVar']一样获取它;
成功:功能(结果){
//如果请求没有问题,那么。。。
alert(result)//结果变量是php页面
//输出(如果您回显“hello”,此警报将显示hello.)
},
});
}

希望这有帮助,再见

我不确定ajax是如何工作的?它弄乱了我的脑袋。为了这件事,我很早就准备好了。你能解释一下我是怎么做的吗。谢谢:)就像ajax根本不是为internet explorer设计的一样,w3schools.com也不是为以正确的方式学习东西而设计的。没错,但是很多Webdev从小就在w3schools.com上学习和实验东西(特别是在21世纪初)。从这个问题中,OP需要理解基本的客户端/服务器端差异,这在这里得到了很好的说明-谢谢,但这仍然不起作用。我已经更新了我的代码。谢谢,错误是什么,启动ajax后是否填充了$content?不会出现错误。它只是没有公布号码
//STEP ONE: INCLUDE THE LAST VERSION OF JQUERY
<script src="http://code.jquery.com/jquery-latest.min.js"
    type="text/javascript"></script>
//STEP TWO, GET YOUR FUNCTION TO WORK:

function sendVariableTo(variable,url) {
    $.ajax({
        url:url, //Or whatever.php
        type: "GET", //OR POST
        data: { myVar: variable}, //On php page, get it like $_REQUEST['myVar'];
        success:function(result){
            //If the request was ok, then...
            alert(result) //Result variable is the php page 
            //output (If you echo "hello" this alert would give you hello..)
        },
    });
}