在同一页面上使用ajax将javascript变量传递给php变量

I have a calendar created using a for loop. When the user clicks on a cell in the calendar, they will be navigated to a modal window;

$calendar.= '<td class="calendar-day"><a href="#myModal" data-toggle="modal" data-target="#myModal" data-id="'.$list_day .'" data-month="'.$month .'" data-year="'.$year .'" onclick=" document.getElementById('. "'myModal'" . ').style.display = ' . "'block'". ' ;"></button><div style="position:relative;height:100px;">'; 

I am passing the date by day, month and year to the modal and saving each as a seperate javascript variable, then merging them together to form a javascript variable (date). I would like to pass this variable to the modal window and use as a php variable. My javascript is:

$('#myModal').on('show.bs.modal', function(e) {
var id = $(e.relatedTarget).data('id');
var month = $(e.relatedTarget).data('month');
var year = $(e.relatedTarget).data('year');
var string = id +'/'+ month +'/'+year;
var date = year +'-'+month+'-'+id;

$(e.currentTarget).find('input[name="id1"]').val(string);
$(e.currentTarget).find('input[name="id"]').val(id);
$(e.currentTarget).find('input[name="month"]').val(month);
$(e.currentTarget).find('input[name="year"]').val(year);

 $(".test5").val(id+'/'+month+'/'+'/'+year);

$(document).ready(function() {
    $('#myModal').click(function() {
        $.ajax({
            url: 'my_planner.php',
            type: 'GET',
            data: { var_PHP_data: date },
            success: function(data) {
                $('#result').html(data);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                //case error                    }
            }
        });
    });
});

});

My modal html code is:

<div id="myModal" class="modal">

<!-- Modal content -->
<div class="modal-content">
<span class="close">&times;</span>
<div class="modal-body">
  <form action="my_planner.php" method="post">
  <!--<span class="test5"></span>-->
  <!--<span class="test"><input type="text" name="id_disabled" value="" disabled/>-->
  <input class="test" type="text" name="id1" value="" style="text-align:center; border:0px; font-weight:bold; font-size: 25px;"/>
  <hr class="my-4">
  <input type="hidden" name="id" value=""/>
  <input type="hidden" name="month" value=""/>
  <input type="hidden" name="year" value=""/>

<br>
<?$date = $_GET['var_PHP_data']; 
echo 'Date:'.$date;?>
<p id="result"></p>

The modal is displaying the string date but not the variable? Where am I going wrong? I'm not familiar with ajax

2个回答



您可以连接日期并将其添加到 data-date </ code>,例如:</ p>
\ n

  $ calendar。='&lt; td class =“calendar-day”&gt;&lt; a href =“#myModal”'
。' data-toggle =“modal”data-target =“#myModal”'
。' data-date =“'。$ list_day。'/'。$ month。'/'。$ year。'”'
。' onclick =“document.getElementById('。”'myModal'“。')。style.display ='
。”'block'“。';”&gt;&lt; / button&gt;&lt; div style =“position: 相对;高度:100像素;“&GT;';

</ code> </ pre>

在JS中:</ p>

  var date = $(e.relatedTarget).data('date'  ); 

$('#result')。html(date);
</ code> </ pre>
</ div>

展开原文

原文

You can concatenate date and add it to data-date for example:

$calendar.= '<td class="calendar-day"><a href="#myModal" '
  .' data-toggle="modal" data-target="#myModal" '
  .' data-date="' . $list_day .'/' . $month . '/' .$year .'"'
  .' onclick=" document.getElementById('. "'myModal'" . ').style.display = ' 
 . "'block'". ' ;"></button><div style="position:relative;height:100px;">'; 

and in JS:

var date = $(e.relatedTarget).data('date');

$('#result').html(date);

dousi1994
dousi1994 然后我如何在模态窗口中提取结果的值以保存为php变量?
2 年多之前 回复
douying1119
douying1119 更新答案$('#result')。html(date);
2 年多之前 回复
doufan6544
doufan6544 然后我如何将JS变量发送到模态div?
2 年多之前 回复



您不能在PHP中使用JS变量。 如果您构建变量客户端,则保持客户端,直到您在参数或表单值中传递它。 </ p>

你可以做的是在模态中显示JS变量。</ p>

而不是:</ p>

 &lt;?$ date = $ _GET ['var_PHP_data'];

echo'日期:'。$ date;?&gt;
</ code> </ pre>

您可以使用此HTML:</ p>

   &lt; p&gt;日期:&lt; span class =“date_value”&gt;&lt; / span&gt;&lt; / p&gt; 
</ code> </ pre>

并添加此JS:</ p>

  $(“。date_value”)。text(date); 
</ code> </ pre>

这样,跨度将被填充 输出你的JS日期变量的值。</ p>
</ div>

展开原文

原文

You can't use a JS variable in PHP. If you build the variable client-side, you remain client-side until you either pass it on in a parameter or a form value.

What you can do, is display the JS variable in the modal.

Instead of this:

<?$date = $_GET['var_PHP_data']; 
echo 'Date:'.$date;?>

You can use this HTML:

<p>Date: <span class="date_value"></span></p>

And add this JS:

$(".date_value").text(date);

This way, the span will be filled out with the value of your JS date variable.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐