简体   繁体   中英

getting undefined index error in php

I just want receive the array and print it which is sent by jquery.post()

my HTML-JS

<input type="submit"  id="myButton" />

<script type="text/javascript">

    var arr = new Array();
    arr[0] = "Ding";
    arr[1] = "Dong";
    arr[2] = "Bong";

    var json_string= JSON.stringify(arr); // convert it into a json string. 

    //and sending the data to server. temp.php.
    $("#myButton").click(function(){
    $.post('temp.php' , {json : json_string },function(data){

                       location.href = "temp.php";
         }); 

    });

</script>

I have checked by alert() that data sending is successful. After button click page is also redirecting to temp.php

My php page temp.php

<?php
    $json_string = $_POST['json']; //geting the post request. //this is line 3
    $array_items = json_decode($json_string); //converting json string to a php array.
    echo $array_items[0]; //just to print 1st element of array
 ?>

It should print Ding . But I am getting error

Undefined index: json in C:\wamp\www\phpJSarray\temp.php on line 3

What is my mistake in temp.php page ? Please help me out.

call this javascript on your onclick or on form submit however u like.

var arr = new Array();
arr[0] = "Ding";
arr[1] = "Dong";
arr[2] = "Bong";

var json_string = JSON.stringify(arr); // convert it into a json string.
$("#data").val(json_string);    
$("#form1").submit(); //and sending the data to server. temp.php.

HTML:

<form action="temp.php" id="form1" method="POST">
    <input type="hidden" id="data" name="data">
</form>

temp.php

$dataArray = json_decode($_POST['data']);
<script>

$(document).ready(function() {    
   $("#myButton").on( 'click', function () {
    $.ajax({
        type: 'post',
        url: 'temp.php',
        data: {
            arr1: "Ding",
            arr2: "Dong",
            arr3: "Bong",
        },
        success: function( data ) {
            console.log( data );
        }
    });
});
});

</script>

You'd then call it by doing $_POST['arr1']; . I hope this solves your problem.

The $.post is doing an AJAX call on the current page... which has no apparent effect because once it finishes, the location.href = "temp.php"; is redirecting you to temp.php.

If you'd like to make a POST to temp.php, you could just do it through a normal form. Use jQuery to set the value of a hidden input element to json_string if desired (or better, let jQuery change the form data to JSON).

After you send your data via AJAX to temp.php and it returns a successful response, redirect to temp.php with an empty POST happens. Thats why you get no data after redirect.

You should also check whether array element exists in temp.php :

<?php

    if (isset($_POST['json'])) {
        $json_string = $_POST['json']; 
        $array_items = json_decode($json_string); 
        echo $array_items[0]; 
    }

?>

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