-1

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

Vincent
  • 852
  • 6
  • 29
  • 67

2 Answers2

1
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/

jogesh_pi
  • 9,762
  • 4
  • 37
  • 65
  • I need it. because on another php. there's a process that i will do in which i will parse the html of the url "Google.com" and return my desired html tag. because i cant do it inside the javascript. they said that the client side and the server side cant do it. in which inside the javascript i can't put inside a php. – Vincent Dec 05 '13 at 04:52
1

Something like:

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

might do the trick

Charlie Affumigato
  • 1,017
  • 1
  • 7
  • 10