无法使用JavaScript或Ajax,文本显示相同的页面

时间:2016-11-25 10:01:45

标签: javascript php html ajax

我有错误不知道错误在哪里

无法使用JavaScript或Ajax并且文本显示相同的页面

请在错误的改革中帮助我

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>pop up example</title>

    <script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
    <script type="text/javascript">
        document.getElementById('myform').addEventListener("submit",upload);
function upload()
{
    var xhr = new XMLHttpRequest();
    xhr.open("POST","/upload.php",true);        
    {
        var formdata = new formData(myform);
        xhr.send(formdata);
    }
    xhr.onreadystatechange = function(){
    if(xhr.readyState == 4 && xhr.status == 200)
    {
             //some code to check if submission succeeded 
             url = xhr.responseText();
             if(url == 'failed')
                  Console.log('upload failed'); // do something for failure
             else
                  document.getElementById('urlBox').innerHTML = url;
    }
}; return false;
}
    </script>
</head>


<body>



<form id="myform" method="POST" action=<?php ($_SERVER["PHP_SELF"]); ?>>

    <p>chose url: 
    <select size="1" name="D1">
        <option value="google_drive">google drive</option>
        <option value="clody">clody</option>
    </select>    
    <input type="text" name="T1" size="40">    
    <input type="submit" value="go" name="B1">
    <input type="reset" value="reset" name="B2">
    </p>
</form>


 </body>

</html>

和这个upload.php文件

<?php
if(!empty($_POST['D1']) && !empty($_POST['T1'])){
    $providers = array(
        'google_drive' => 'Goole drive^https://drive.google.com/file/d/{replace}/view',
        'clody' => 'Cloudy^https://www.cloudy.ec/embed.php?id={replace}'        
    );

    if(isset($providers[$_POST['D1']])){

        $url =  str_replace('{replace}', $_POST['T1'], $providers[$_POST['D1']]);
        echo "$url";
    }
}

else{
   echo "failed";
}
?>

谢谢大家

3 个答案:

答案 0 :(得分:0)

在html页面中添加以下行

<div id='urlBox'></div>

答案 1 :(得分:0)

xhr.open("POST","/upload.php",true)后的分号是正常的吗?

答案 2 :(得分:0)

您可以尝试替换此行:

<form id="myform" method="POST" action=<?php ($_SERVER["PHP_SELF"]); ?>>

为此:

<form id="myform" method="POST" action="<?php ($_SERVER["PHP_SELF"]); ?>">

差异在于你的行动。你没有使用“”