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

さらに良いことに... jQuery、Prototype、MooTools、YUI などの JavaScript フレームワークを使用します。

于 2010-02-22T05:40:30.003 に答える