-1

I know this question was already answered one million times but I'm becoming desperate with this code.

I have two files php files and I want to send a variable from a part written in JS to the other PHP file. So here's the code:

carga_datos.php

<script>
    function MyAlert() {
            var radio1 = $("input[name='inputTipoActividad']:checked").val();
            $.ajax({
                url : 'post.php',
                type : "POST",
                data: {'radio1' : radio1},
                dataType:'json',
                success : function(data) {

                    alert(data);
                },
            });
        }   
</script>

I'm calling to MyAlert() in a radio button form

post.php

<?php 
    $radio1 = $_POST['radio1'];
    echo $radio1;
?>

And I call post.php into carga_datos.php like this:

And this is the error I'm retrieving:


Notice: > Undefined index: radio1 in C:\xampp\htdocs\gis\post.php on line 2

Mohammad hayajneh
  • 623
  • 2
  • 11
  • 32
Sergio Barbero
  • 181
  • 1
  • 2
  • 8
  • 1
    have you checked the ajax request by looking in your browser's network tab whether any value is actually being sent for "radio1" in the request? Then we can see if the problem is really server- or client-side. – ADyson Sep 12 '17 at 12:09
  • Have you tested that `var radio1 = $("input[name='inputTipoActividad']:checked").val();` is correctly retrieving the radio button's value? Try console logging this before sending via AJAX to ensure it is ready to be sent. – Seb Cooper Sep 12 '17 at 12:10
  • Even if the variable was null, radio1 would still be set in `$_POST`, no? – iainn Sep 12 '17 at 12:11
  • Can you tell me how to do that or send me a guide? Thank you – Sergio Barbero Sep 12 '17 at 12:12
  • Can you try by removing `dataType:'json',` ? – Himanshu Upadhyay Sep 12 '17 at 12:13
  • @ iainn The AJAX post would fail if the radio1 value is not set. – Seb Cooper Sep 12 '17 at 12:13
  • yes, value of radio1 is correct. I checked that – Sergio Barbero Sep 12 '17 at 12:13
  • Are you getting a value from `echo $radio1;` ? that message you posted is a warning that you've not checked that `$_POST['radio1']` is set – IsThisJavascript Sep 12 '17 at 12:13
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – IsThisJavascript Sep 12 '17 at 12:14
  • Not working removing dataType – Sergio Barbero Sep 12 '17 at 12:17
  • 1
    I see `dataType` is the type expected in response from server not sent. Use `method` instead of `type` to set the HTTP request type. See http://api.jquery.com/jquery.ajax/ – Seb Cooper Sep 12 '17 at 12:19
  • @SergioBarbero can you show us full html page ? – Ahmed Ginani Sep 12 '17 at 12:20
  • @SergioBarbero are you geting an output from `echo $radio1`? – IsThisJavascript Sep 12 '17 at 12:21
  • @WillParky93 Just the error, no output – Sergio Barbero Sep 12 '17 at 12:25
  • Please try replacing `$radio1 = $_POST['radio1']; echo $radio1;` with `var_dump($_POST);` and update with the response you get from the AJAX call in your Chrome network tab. – Seb Cooper Sep 12 '17 at 12:26
  • @SergioBarbero the notice can be removed by changing `$radio1 = $_POST['radio1'];` to `$radio1 = (isset($_POST['radio1'])) ? $_POST['radio1']:die('No post sent!');` – IsThisJavascript Sep 12 '17 at 12:27
  • Yes if you are loading post.php without sending any AJAX data or setting the value of $_POST then the logic will fail. It may be useful to move all PHP that handles AJAX requests into a separate PHP file that you do not load directly in browser to avoid these issues. – Seb Cooper Sep 12 '17 at 12:29
  • 1
    @HimanshuUpadhyay read the jquery ajax docs. The `dataType` setting is related to the _response_ type. Nothing to do with what is being sent to the server, which is the problem here. `contentType` would affect what is _sent_. But it's not set here and will therefore be the default, which should be fine. – ADyson Sep 12 '17 at 13:53

1 Answers1

1

Edit:

It seems you have used dataType "json" so it will expect a "json", if none is returned you will get undefined or an empty response.

If you remove dataType: "json" you should be able to retrieve the data just fine.

    function MyAlert() {
    var radio1 = $("input[name='inputTipoActividad']:checked").val();
    $.ajax({
        url : 'post.php',
        method : "POST",
        data: {"radio1" : radio1},
        success : function(data) {
            console.log(data);
        }
    });
}

If you want to check with "dataType: 'json'" still in your code, you need to return your PHP code like the following:

<?php echo json_encode($_POST); ?>

This will then either return your $_POST data or just an empty [] array.

// Old Answer for just true/false values

If you just want to see if the checkbox was checked or not to retrieve a boolean, please try the following:

var radio1 = $("input[name='inputTipoActividad']").prop("checked");

This will return TRUE or FALSE. If not wrong, if you use

$("input[name='inputTipoActividad']:checked").val()

it'll return "on" or nothing (might be wrong here though).

Tested it with the ".prop("checked")" and it worked for me.

DenDen
  • 34
  • 2