0

我这里有个小问题!!

在我提交表单后,基于 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>

在提交时运行这个 javascript 函数

 ytVideoApp.prepareSyndicatedUpload = function(videoTitle, videoDescription, videoCategory, videoTags) {
    var filePath = 'operations.php';
    var params = 'operation=makesomething' +
                 '&videoTitle=' + videoTitle +
                 '&videoDescription=' + videoDescription +
                 '&videoCategory=' + videoCategory +
                 '&videoTags=' + videoTags;
    ytVideoApp.sendRequest(filePath, params, ytVideoApp.SYNDICATED_UPLOAD_DIV);
}

其中 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);
}

我试过:echo "<script>function();</script>"不工作 print "alert('something');"; 不工作

任何 1 可以帮助我吗?

谢谢!

4

1 回答 1

1

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

resultDiv.innerHTML = xmlhr.responseText;

至:

eval(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...
});

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

于 2010-02-22T05:40:30.003 回答