1

i have a problem with my simple program in php that include an alert javascript. This is the code:

<?php

function iva(){
$country='IT';
$vatnum=$_POST['n'];

$a="Work";
$b="NotWork";

$url='http://isvat.appspot.com/'.$country.'/'.$vatnum.'/';
 $response = file_get_contents($url);
//global $a, $b;
if( $response == 'true' ){
echo $a;
}
if ($response != 'true'){
echo $b;
}
}
?>
<script>
function ivaz(){

alert("<?php iva() ?>");

}
</script> 

<form method="post">
<input name="n"  type="textarea" >
<input  onclick="ivaz();" value="Validate" type="submit"> </input> </form>

My program take a value from input text box and pass the value to php script that return true or false in a javascript alert. The program work, but return previous value passed in input box. Can someone help me to solve it?

Thanks guys.

1

3 Answers 3

3

No, it doesn't work that way. If you want to call a PHP function from Javascript without the page refreshing, you need an XMLHttpRequest.

Example:

<?php
// your php process when called by XMLHttpRequest
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $vatnum = $_POST['n'];
    $country='IT';
    $a = "Work";
    $b = "NotWork";

    $url = 'http://isvat.appspot.com/'.$country.'/'.$vatnum.'/';
    $response = file_get_contents($url);
    //global $a, $b;
    if( $response == 'true' ){
        echo $a;
    } else {
        echo $b; 
    }
    exit;
}

?>

<form method="post" id="form1">
    <input name="n" type="text" id="n" />
    <input value="Validate" type="submit">
</form>

<script type="text/javascript">
// when the form is submitted
document.getElementById('form1').addEventListener('submit', function(e){
    e.preventDefault(); 
    var n = document.getElementById('n').value; // get the textbox value
    var xmlhttp = new XMLHttpRequest();
    var params = 'n=' + n;
    var php_url = document.URL;
    xmlhttp.open('POST', php_url, true);

    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var response = xmlhttp.responseText;
            alert(response); // alert the server response
        }
    }

    xmlhttp.send(params);
});
</script>
Sign up to request clarification or add additional context in comments.

10 Comments

Note: Using jQuery is highly recommended for maximum browser compatibility.
@Icefyre if jquery is tagged, i could have answer it with it. but since its not there. i did not
@Icefyre even fuzzy people here downvote answers with jquery code with a non jquery tagged question even if its correct.
Didn't know that, but will keep it in mind for the future. Thank you.
the code work!! thanks. If i want load this code on another javascript? it's possible?
|
0

Remove onclick="ivaz();" from input tag

Comments

0

You cannot run the php script without reloading the page as php is generated serverside and javascript runs clientside.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.