我在将变量传递到第二个php文件时遇到问题。
这是我的checkboxtestbackend.php,我需要从index.php传递两个变量。 当我尝试在index.php中使用全局$ variable时,此操作无济于事,如果我尝试制作包含与checkboxtestbacked.php中的变量相同信息的新变量,则结果是相同的:
$checkboxstatus = $_POST['checkboxstatus'];
$host = "localhost";
$user = "root";
$password = "";
$dbname = "odcinki";
$cxn = mysqli_connect($host,$user,$password,$dbname);
if (mysqli_connect_errno()) {echo "Brak polaczenia" . mysqli_connect_error();}
$query = " UPDATE `1`
SET lastname = '$checkboxstatus'
WHERE id = '$postID' ";
$result = mysqli_query($cxn, $query) or die ("zesrało sie i chuj");
index.php:
<?php
$host = "localhost";
$user = "root";
$password = "";
$dbname = "odcinki";
$conn = mysqli_connect($host,$user,$password,$dbname);
if (mysqli_connect_errno()) {echo "Brak polaczenia" . mysqli_connect_error();}
$postID = get_queried_object_id();
$current_user = wp_get_current_user();
$current= $current_user->ID ;
$sql = "SELECT lastname FROM `$current` WHERE id = '$postID'";
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$keepchecked= $row['lastname'];
}
?>
<?php if ( is_user_logged_in() ) { ?> Status: <input type="checkbox" name = "seks" value ="1" id="checkboxtest" <?php echo ($keepchecked == "YES") ? 'checked="checked"' : ''; ?> >
<?php
}
?>
<script type="text/javascript">
$(document).ready(function(e) {
$('#checkboxtest').change(function(){
if( $('#checkboxtest').prop('checked') )
{checkboxstatus = "YES";}
else
{checkboxstatus = "NO";}
$.ajax({
type: "POST",
url: "checkboxtestbackend.php",
data: {checkboxstatus: checkboxstatus},
success: function(result) {
console.log('the data was successfully sent to the server');
}
})
});
});
?>
答案 0 :(得分:0)
我认为您的代码应该是这样的
JsonSerializationReplacer
Y0u需要制作JavaScript变量并将其发布到checkboxtestbackend.php页面
$checkboxstatus = $_POST['checkboxstatus'];
$postID = $_POST['postID'];
$host = "localhost";
$user = "root";
$password = "";
$dbname = "odcinki";
$cxn = mysqli_connect($host,$user,$password,$dbname);
if (mysqli_connect_errno()) {echo "Brak polaczenia" . mysqli_connect_error();}
$query = " UPDATE `1`
SET lastname = '$checkboxstatus'
WHERE id = '$postID' ";
$result = mysqli_query($cxn, $query) or die ("zesrało sie i chuj");