简体   繁体   中英

Ajax not able to post javascript variable to php

I want to post values of a JavaScript variable to a PHP page, and then use those values there.

I get back a new HTML page, which I did not want to happen. Also, it is showing the error message produced by:

echo"some error";

What am I missing? I don't understand why this happens?

Here is my html file try.html

<html>
    <head>
        <title>try</title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    </head>
    <body>
         <a id="button" href="try.php" target="_blank">send data</a>
         <script>
            var sum2= 2010;
            $('#button').click(function() {                 
                var val1=sum2;        
                $.ajax({
                    type: 'POST',
                    url: 'try.php',
                    data: { text: val1 },
                    dataType: 'script' // I am not sure about what I write here script,json,html?
                });
            });
        </script>
    </body>
</html>

And this is my PHP file try.php

<?php       
if(isset($_POST['text']))
{
    $text = $_POST['text'];
    echo $text;
}
else {
    echo"some error";
}
?>

Firstly, remove script from data-type. This is how you can pass the data to php file:

$('#button').click(function() {

                var val1=sum2;

                $.ajax({
                type: 'POST',
                url: 'try.php',
                data: { text: val1 },
                });
            });

dataType - The type of data that you're expecting back from the server. Here you don't want anything in return then it's fine. If you are expecting anything like json or something, you can specify it.

If You want anything in response, you can handle in this manner $('#button').click(function() {

                var val1=sum2;

                $.ajax({
                type: 'POST',
                url: 'try.php',
                data: { text: val1 },
                });
               success: function (data) {
                 //Your success handler code
                },
            });

success - A callback function to be executed when Ajax request succeeds.

Firstly remove href from link. By this it is directly redirecting to try.php

<a id="button" href="javascript:void(0);" target="_blank">send data</a>

In try.php if you echo something which in turn will comeback to try.html.

If you just want to post the data to try.php and echo there the just use form and submit. You dont need ajax in that. Ajax is to send the data/receive back without reloading the existing page. The response will be seen in the current page itself.

<html>
    <head>
        <title>try</title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    </head>
    <body>
         <a id="button" href="try.php">send data</a>
         <script>
            var sum2= 2010;
            $('#button').click(function() {

                var val1=sum2;
                var json_params = { 
                    text: val1 
                    };
                var json_data = JSON.stringify(json_params);
                $.ajax({
                type: 'POST',
                url: 'try.php',
                data: json_data,
                dataType: "html",
                success: function(response)
                    {
                        alert(response);
                    }
                });
            });
        </script>
    </body>

</html>

In the try.php page you can place this code

<?php  
if (isset($_POST)){
$data = json_decode(file_get_contents('php://input'));
print_r($data);
}    
else{
    echo "not set";
}
?>

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