Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.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
从php调用javascript/ajax函数_Php_Javascript_Ajax_Function_Call - Fatal编程技术网

从php调用javascript/ajax函数

从php调用javascript/ajax函数,php,javascript,ajax,function,call,Php,Javascript,Ajax,Function,Call,我这里有个小问题 在我提交表格之后, 基于php响应,我想执行另一个javascript或ajax函数 这是我的表格: <form id="uploadForm" onsubmit="ytVideoApp.prepareSyndicatedUpload( this.videoTitle.value, this.videoDescription.value, this.videoCategory.value, this.videoTags.va

我这里有个小问题

在我提交表格之后, 基于php响应,我想执行另一个javascript或ajax函数

这是我的表格:

    <form id="uploadForm" onsubmit="ytVideoApp.prepareSyndicatedUpload( 
    this.videoTitle.value, 
    this.videoDescription.value, 
    this.videoCategory.value, 
    this.videoTags.value); 
    return false;"> 
    Enter video title:<br /> 
    <input size="50" name="videoTitle" type="text" /><br /> 
     Enter video description:<br /> 
    <textarea cols="50" name="videoDescription"></textarea><br /> 
    <select style="display:none" name="videoCategory"> 
    <option style="display:none" value="Music">Music</option> 
    </select> 
    Enter some tags to describe your video 
    <em>(separated by spaces)</em>:<br /> 
    <input name="videoTags" type="text" size="50" value="video" /><br /> 
    <input  id="butok" type="submit" value="go" > 
    </form>
其中,ytVideoApp.sendRequest是:

ytVideoApp.sendRequest = function(filePath, params, resultDivName) {
  if (window.XMLHttpRequest) {
    var xmlhr = new XMLHttpRequest();
  } else {
    var xmlhr = new ActiveXObject('MSXML2.XMLHTTP.3.0');
  }

  xmlhr.open('POST', filePath);
  xmlhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 

  xmlhr.onreadystatechange = function() {
    var resultDiv = document.getElementById(resultDivName);
    if (xmlhr.readyState == 1) {
      resultDiv.innerHTML = '<b>Loading...</b>'; 
    } else if (xmlhr.readyState == 4 && xmlhr.status == 200) {
      if (xmlhr.responseText) {
        resultDiv.innerHTML = xmlhr.responseText;
      }
    } else if (xmlhr.readyState == 4) {
      alert('Invalid response received - Status: ' + xmlhr.status);
    }
  }
  xmlhr.send(params);
}
ytVideoApp.sendRequest=函数(文件路径、参数、结果名称){
if(window.XMLHttpRequest){
var xmlhr=new XMLHttpRequest();
}否则{
var xmlhr=newActiveXObject('MSXML2.XMLHTTP.3.0');
}
xmlhr.open('POST',filePath);
setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xmlhr.onreadystatechange=函数(){
var resultDiv=document.getElementById(resultDivName);
if(xmlhr.readyState==1){
resultDiv.innerHTML='Loading…';
}else if(xmlhr.readyState==4&&xmlhr.status==200){
if(xmlhr.responseText){
resultDiv.innerHTML=xmlhr.responseText;
}
}else if(xmlhr.readyState==4){
警报(“收到无效响应-状态:”+xmlhr.Status);
}
}
xmlhr.send(参数);
}
我尝试了:
echo“function();”
不工作 打印“警报(‘某物’);”;不起作用

有人能帮我吗


谢谢

你可能会得到你想要改变的东西:

resultDiv.innerHTML = xmlhr.responseText;
致:

这可能是一个非常糟糕的主意,但您可以这样做,从服务器返回的任何内容都将作为javascript执行

更好的解决方案是修改ajax方法,使其采用回调函数而不是元素进行更新。它看起来更像这样:

ytVideoApp.sendRequest = function(filePath, params, callback) {
    ...
    } else if (xmlhr.readyState == 4 && xmlhr.status == 200) {
        if(callback) callback(xmlhr.responseText);
    }
    ...
ytVideoApp.sendRequest(filePath, params, function(responseText) {
    // Do something with the stuff sent back from the server...
});
你会这样称呼它:

ytVideoApp.sendRequest = function(filePath, params, callback) {
    ...
    } else if (xmlhr.readyState == 4 && xmlhr.status == 200) {
        if(callback) callback(xmlhr.responseText);
    }
    ...
ytVideoApp.sendRequest(filePath, params, function(responseText) {
    // Do something with the stuff sent back from the server...
});

更好的是。。。使用javascript框架,如jQuery、Prototype、MooTools或YUI。

这是什么?“回声”警报(“你好”);“回声”警报(“你好”);”;如果一个用户可以接收JSON,我就可以启动一个javascript函数??您可以接收JSON并将其传递给javascript函数。如果您对该项目有任何控制,那么您应该考虑使用jQuery,因为这使得这相当容易。如果要返回JSON,请调用jQuery.getJSON;如果要返回并执行javascript,请调用jQuery.getScript。