javascript - Error by passing variable from JS to PHP -
i know question answered 1 million times i'm becoming desperate code.
i have 2 files php files , want send variable part written in js other php file. here's 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 myalert() in radio button form
post.php
<?php $radio1 = $_post['radio1']; echo $radio1; ?>
and call post.php carga_datos.php this:
and error i'm retrieving:
notice: > undefined index: radio1 in c:\xampp\htdocs\gis\post.php on line 2
edit:
it seems have used datatype "json" expect "json", if none returned undefined or empty response.
if remove datatype: "json" should able retrieve data 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 want check "datatype: 'json'" still in code, need return php code following:
<?php echo json_encode($_post); ?>
this either return $_post data or empty [] array.
// old answer true/false values
if want see if checkbox checked or not retrieve boolean, please try following:
var radio1 = $("input[name='inputtipoactividad']").prop("checked");
this return true or false. if not wrong, if use
$("input[name='inputtipoactividad']:checked").val()
it'll return "on" or nothing (might wrong here though).
tested ".prop("checked")" , worked me.
Comments
Post a Comment