я скопировал код поменял только УРЛ - не работает.! ВЫ можете у себя на локалке сделать с моим пхп примером? --- Добавлено --- Вот весь мой код HTML: <html> <head> <style> #title{ background:#f8f8f8; margin-top:50px; text-align:center; width:500px; margin:auto; } #wf{ } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> </head> <body> <script> <div> <center> <form id="sform" method="POST" name="form1" action=""> <div> <label for="name">Enter website</label> <input id="name" type="text" name="sitename"> <input type="button" value="Get metadata" name="sbutton" id="sbutton" onclick="document.getElementById('wf').style.display = 'inline-block';"> <input type="submit" value="Write to file" name="wfbutton" id="wf" /> </div> </form> </center> </div> </body> </html> PHP: <?php//functionsfunction getTitle($url) { $data = file_get_contents($url); $title = preg_match('/<title[^>]*>(.*?)<\/title>/ims', $data, $matches) ? $matches[1] : null; echo "<div id='title'>Title:</div>"; print_r("<div id='title'>$title</div>");}if (isset($_POST['sitename'])){ if (empty($_POST['sitename'])) { echo "<div id='title'>Sorry, but field cannot be empty, please try again</div>"; } if($_POST['sitename']) { $tag = get_meta_tags("http://".$_POST['sitename']); } $site = "http://".$_POST['sitename']; //kw if (isset($tag['keywords'])) { if(count($tag['keywords']) == 0){ echo "<div id='title'>Keywords not found</div>"; } else{ echo "<div id='title'>List of keywords:</div>"; print_r("<div id='title'>".$tag['keywords']."</div>"); } } else{ echo "<div id='title'>Keywords not found</div>"; } //desc if (isset($tag['description'])) { if(count($tag['description']) == 0){ echo "<div id='title'>Description not found</div>"; } else{ echo "<div id='title'>Description:</div>"; print_r("<div id='title'>".$tag['description']."</div>"); } } else{ echo "<div id='title'>Description not found</div>"; } //title if($_POST['sitename']) { getTitle($site); } //get time response // Создаем дескриптор cURL $ch = curl_init($site); //hide output curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Запускаем curl_exec($ch); // Проверяем наличие ошибок if (!curl_errno($ch)) { $info = curl_getinfo($ch); echo "<div id='title'>".'Прошло ', $info['total_time'], ' секунд во время запроса к ', $info['url'], "\n"."</div>"; } // Закрываем дескриптор curl_close($ch); if (isset($_POST['wfbutton'])){ $filename = 'metadata.txt'; $content = $tag['keywords']; $content .= $tag['description']; header('Content-disposition: attachment; filename='.$filename); header('Content-type: text/html'); echo $content; exit(); } }?> PHP: <script> $("#sbutton").click(function () { event.preventDefault(); $.ajax({ url: "index.php", type: "GET", data: { sitename: $("#name").val() }, success: function(data){ alert("Success"); }, error: function(){ alert('error'); } }); });</script>
Вот весь мой код HTML: <html> <head> <style> #title{ background:#f8f8f8; margin-top:50px; text-align:center; width:500px; margin:auto; } #wf{ } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> </head> <body> <script> <div> <center> <form id="sform" method="POST" name="form1" action=""> <div> <label for="name">Enter website</label> <input id="name" type="text" name="sitename"> <input type="button" value="Get metadata" name="sbutton" id="sbutton" onclick="document.getElementById('wf').style.display = 'inline-block';"> <input type="submit" value="Write to file" name="wfbutton" id="wf" /> </div> </form> </center> </div> </body> </html> поменял на пост, не работает
Поставил, не помогло. --- Добавлено --- короч пзц какой-то, не работает и все, помогите мне пожалуйста
Вы можете запустить мой код у себя и убедиться? Я очень хочу разобраться с аяксом Код: <?php //functions function getTitle($url) { $data = file_get_contents($url); $title = preg_match('/<title[^>]*>(.*?)<\/title>/ims', $data, $matches) ? $matches[1] : null; echo "<div id='title'>Title:</div>"; print_r("<div id='title'>$title</div>"); } if (isset($_POST['sitename'])){ if (empty($_POST['sitename'])) { echo "<div id='title'>Sorry, but field cannot be empty, please try again</div>"; } if($_POST['sitename']) { $tag = get_meta_tags("http://".$_POST['sitename']); } $site = "http://".$_POST['sitename']; //kw if (isset($tag['keywords'])) { if(count($tag['keywords']) == 0){ echo "<div id='title'>Keywords not found</div>"; } else{ echo "<div id='title'>List of keywords:</div>"; print_r("<div id='title'>".$tag['keywords']."</div>"); } } else{ echo "<div id='title'>Keywords not found</div>"; } //desc if (isset($tag['description'])) { if(count($tag['description']) == 0){ echo "<div id='title'>Description not found</div>"; } else{ echo "<div id='title'>Description:</div>"; print_r("<div id='title'>".$tag['description']."</div>"); } } else{ echo "<div id='title'>Description not found</div>"; } //title if($_POST['sitename']) { getTitle($site); } //get time response // Создаем дескриптор cURL $ch = curl_init($site); //hide output curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Запускаем curl_exec($ch); // Проверяем наличие ошибок if (!curl_errno($ch)) { $info = curl_getinfo($ch); echo "<div id='title'>".'Прошло ', $info['total_time'], ' секунд во время запроса к ', $info['url'], "\n"."</div>"; } // Закрываем дескриптор curl_close($ch); if (isset($_POST['wfbutton'])){ $filename = 'metadata.txt'; $content = $tag['keywords']; $content .= $tag['description']; header('Content-disposition: attachment; filename='.$filename); header('Content-type: text/html'); echo $content; exit(); } } ?> <html> <head> <style> #title{ background:#f8f8f8; margin-top:50px; text-align:center; width:500px; margin:auto; } #wf{ } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> </head> <body> <script> $("#sbutton").click(function () { event.preventDefault(); $.ajax({ url: "index.php", type: "POST", data: { sitename: $("#name").val() }, success: function(data){ alert("Success"); }, error: function(){ alert('error'); } }); }); </script> <div> <center> <form id="sform" method="POST" name="form1" action=""> <div> <label for="name">Enter website</label> <input id="name" type="text" name="sitename"> <input type="button" value="Get metadata" name="sbutton" id="sbutton" onclick="document.getElementById('wf').style.display = 'inline-block';"> <input type="submit" value="Write to file" name="wfbutton" id="wf" /> </div> </form> </center> </div> </body> </html>
Что именно не работает? Окошко выскакивает? Вот еще одна возможная ошибка url:"index.php", url:"/index.php",
Все там должно работать. Ну попробуй так PHP: $( document ).ready(function() { $("#sbutton").click(function(){ ......}
https://jsfiddle.net/e4jma5fx/3/ введи в форму что то и нажми отправить. Это и так твой код. Тот редактор ничем не отличается. Путь от корня правильно указал? url:"/index.php" ?
Ну php ведь работал до этого. Зачем его проверять? type: "POST" + url: "/правильный путь к index.php" это все что нужно изменить