简体   繁体   中英

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

I have a code in php where onclick i will call a javascript. but i dont know how to use the ajax and pass the value to php and return it.

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

    }
</script>

body: main.php (current page) when the user clicked on the link. it will alert correct.

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

a php that ajax will call.. 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?  ?>

i need to return the result.

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. thanks :)

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

I think you don't to use ajax for this kind of alert:

HTML:

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

JavaScript Code:

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

Demo:

Check out the working example here: http://jsfiddle.net/jogesh_pi/6UGFT/

Something like:

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

might do the trick

Maybe this will help you How do I return the response from an asynchronous call? . You got the jQuery and simple javascript version explained.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM