program php and alert javascript on form html

BlekZ

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.

Kevin

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>

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Create javascript alert in PHP

분류에서Dev

php code not executing after calling a javascript alert

분류에서Dev

html form validation with javascript

분류에서Dev

PHP + HTML: create a form dynamically

분류에서Dev

What is the alternative of html form in php?

분류에서Dev

How to add php content or variable inside javascript alert box?! javascript, php, alertbox

분류에서Dev

PHP: Interacting with 'selected values' in a html form

분류에서Dev

Not able to login from an html form through php

분류에서Dev

HTML form is not posting the data through PHP

분류에서Dev

JavaScript - HTML form to JSON (empty values) and format

분류에서Dev

HTML Form javascript sum total taxes

분류에서Dev

Proper way to submit a form with php / javascript

분류에서Dev

Filter Alert Javascript, how to

분류에서Dev

alert while loop javascript

분류에서Dev

what is this usage of alert in javascript?

분류에서Dev

Text won't write to page but will show in alert() message. JavaScript and HTML

분류에서Dev

HTML form PHP post to self to validate or submit to new page

분류에서Dev

I can't get the html form to run a php script

분류에서Dev

FORM HTML <입력>의 PHP 스크립트

분류에서Dev

HTML - collect data with php from a textfield out of the post <form>

분류에서Dev

Error in code to upload file via php and html form

분류에서Dev

How to delete data from html form php mysql

분류에서Dev

Undefined value when calling an input from a HTML form from PHP

분류에서Dev

HTML form with multiple image-select via Javascript

분류에서Dev

HTML form with javascript gets "NaN" in IE but works in Chrome

분류에서Dev

How to use a html form to send data to javascript to be saved as a variable

분류에서Dev

Javascript + PHP Form 다른 파일 실행

분류에서Dev

PHP : HTML Form을 사용하여 PHP 액션 호출

분류에서Dev

PHP : HTML Form을 사용하여 PHP 액션 호출

Related 관련 기사

  1. 1

    Create javascript alert in PHP

  2. 2

    php code not executing after calling a javascript alert

  3. 3

    html form validation with javascript

  4. 4

    PHP + HTML: create a form dynamically

  5. 5

    What is the alternative of html form in php?

  6. 6

    How to add php content or variable inside javascript alert box?! javascript, php, alertbox

  7. 7

    PHP: Interacting with 'selected values' in a html form

  8. 8

    Not able to login from an html form through php

  9. 9

    HTML form is not posting the data through PHP

  10. 10

    JavaScript - HTML form to JSON (empty values) and format

  11. 11

    HTML Form javascript sum total taxes

  12. 12

    Proper way to submit a form with php / javascript

  13. 13

    Filter Alert Javascript, how to

  14. 14

    alert while loop javascript

  15. 15

    what is this usage of alert in javascript?

  16. 16

    Text won't write to page but will show in alert() message. JavaScript and HTML

  17. 17

    HTML form PHP post to self to validate or submit to new page

  18. 18

    I can't get the html form to run a php script

  19. 19

    FORM HTML <입력>의 PHP 스크립트

  20. 20

    HTML - collect data with php from a textfield out of the post <form>

  21. 21

    Error in code to upload file via php and html form

  22. 22

    How to delete data from html form php mysql

  23. 23

    Undefined value when calling an input from a HTML form from PHP

  24. 24

    HTML form with multiple image-select via Javascript

  25. 25

    HTML form with javascript gets "NaN" in IE but works in Chrome

  26. 26

    How to use a html form to send data to javascript to be saved as a variable

  27. 27

    Javascript + PHP Form 다른 파일 실행

  28. 28

    PHP : HTML Form을 사용하여 PHP 액션 호출

  29. 29

    PHP : HTML Form을 사용하여 PHP 액션 호출

뜨겁다태그

보관