Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

php - having trouble using JavaScript or Ajax and text display the same page

I'm having mistakes do not know where error

Having trouble using JavaScript or Ajax and text display the same page

Please help me in the wrong reform

<!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>

and this upload.php file

<?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";
}
?>

and thank you all

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Add following line in your html page

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...