如何使用ajax调用php并返回结果

how to use ajax to call a php and return a result

本文关键字:返回 结果 php 调用 何使用 ajax      更新时间:2024-03-10

我在php中有一段代码,onclick I将在其中调用javascript。但我不知道如何使用ajax并将值传递给php并返回

<script>
    function getURL(e)
    {
       //ajax code that will check.php
    }
</script>

body:main.php(当前页面)。它将提醒正确。

<?php $url = "www.google.com"; ?>
<a href="#" onclick="getURL('<?php echo $url; ?>');">click me</a>

ajax将调用的php。。check.php

<?php 
 $html = file_get_html($url);
    foreach($html->find('iframe') as $e) 
    echo $e->src; 
return "correct"  //idk if it's correct to return. should it be echo?  ?>

我需要返回结果。

可以要求一些示例的基本代码来调用ajax到php吗?它会提醒"正确"的单词。或者php将返回的任何内容。谢谢:)

May a ask for some sample basic code to call of ajax to php using this?   
and it will alert the "correct" word. or anything that the php will return

我认为您不应该将ajax用于这种警报:

HTML:

<a href="#" onclick="getUrl('www.google.com');">click me</a>
<div id="result"></div>

JavaScript代码:

function getUrl( url ){
    var result = document.getElementById('result');
    // Just a Simple validation
    var regex = /www'.google'.com/;
    if( regex.test(url) ){
        result.innerHTML = "Correct";
    }
    else{
        result.innerHTML = "Error";
    }
}

演示:

查看此处的工作示例:http://jsfiddle.net/jogesh_pi/6UGFT/

类似于:

<script>
  function getURL(e){
   //ajax code that will check.php
   $.get('check.php',function(data){
                      alert(data);
                     });
  }
</script>