gpt4 book ai didi

javascript - 无法通过JQUERY传递PHP变量并将其插入到MYSQL中

转载 作者:行者123 更新时间:2023-11-29 22:46:15 27 4
gpt4 key购买 nike

我正在尝试通过 jquery 发送表单,它工作得很好。当点击提交按钮时我的变量:

$userid - got this variable from mysql and it's on top of my page (index.php)
$new_rand - generated this random number in file page_support. (support.php)

它们不会继续到另一个文件(page_jquery_new_ticket.php)。让我向您展示代码:

文件:support.php

<div id="support">
<!-- START BACKGROUND SUPPORT -->
<div class="background-support">
<div class="middle-container">
<div style="position: absolute; bottom:0px;">
<div class="font48 font-regular" >Pomoč i podrška</div>
<div class="font24 inactive background-male" style="float:left;"><a href="?p=help">Pomoč</a></div>
<div class="font24 active" style="float:right;"><a href="?p=support">Podrška</a></div>
</div>
</div>
</div>
<!-- END OF BACKGROUND SUPPORT -->
<!-- MIDDLE INFORMATION FOR HELP STARTS HERE -->

<script>
$(document).ready(function(){
$('#new-ticket').on('submit',function(e) {

$.ajax({
url:'pages/jquery-pages/page_jquery_new_ticket.php',
data:$(this).serialize(),
type:'POST',
success:function(data){
console.log(data);
$("#success-creating-ticket").hide().html(data).fadeIn('slow');
}

});
e.preventDefault(); //=== To Avoid Page Refresh and Fire the Event "Click"===
});
});

</script>

<div id="success-creating-ticket">

</div>

<div class="middle-container">
Prije nego što kontaktirate našu podršku molimo vas da pročitate sekciju „Pomoć“ gdje smo odgovorili na najčešća pitanja. U slučaju da vaše pitanje nije među njima pošaljite nam upit preko obrasca ispod i potrudićemo se da vam što prije pomognemo.

<br><br>
<div class="font-italic">Podrška odgovara na vaša pitanja od ponedjeljka do petka između 10.00 i 16:00 sati</div>
<br>

<?php
///////////////////////////////////////////////////////////////////////
// GET THE TICKET NUMBER IF ALREADY EXISTS CREATE NEW ONE
//////////////////////////////////////////////////////////////////////

$statement = $dbConn->prepare("SELECT ticketnumber FROM tickets");
$statement->execute();
$view_ticket_number = $statement->fetch(PDO::FETCH_BOTH);

$rndnumber = rand(1001, 2000);
$randnr = $rndnumber.$user_id;

if($view_ticket_number['ticketnumber'] == $randnr)
{
$rndnumber = rand(1001, 2000);
$new_rand = $rndnumber.$user_id;
echo "<h1>#".$new_rand."</h1>";
} else {
$rndnumber = rand(1001, 2000);
$new_rand = $rndnumber.$user_id;
echo '<div class="font-regular username_female font24">';
echo $settings_ticket." #".$new_rand;
echo '</div>';
}

?>

<br>

<form method="post" action="" name="new-ticket" id="new-ticket">

<input type="text" name="subject" class="width-50 " placeholder="Napisite naslov">
<br><br>
<textarea name="message" class="width-100" style="min-height: 100px;" id="message" placeholder="Napisite vase pitanje ovdje"></textarea>
<br><br>
<input type="submit" name="send" value="<?php echo $settings_btn_send;?>" class="button_pink_square"><br><br>
</form>





</div>
<!-- END OF MIDDLE INFORMATIO STARTS HERE -->

这是文件:pages/jquery-pages/page_jquery_new_ticket.php

<?php   

////////////////////////////////////////////////////////////
// THIS FILE CREATES NEW TICKET
////////////////////////////////////////////////////////////

include_once "../../inc/settings.php";
include_once '../../db/dbc.php';
include '../../languages/lang_bosnian_mainpage.php';


if(isset($_POST['send']) == $settings_btn_send)
{
$subject = mysql_real_escape_string($_POST['subject']); //subject of the message
$text = mysql_real_escape_string($_POST['message']); //text in the message
$tid = mysql_real_escape_string(date("Y-m-d")); //time when the message was sent

//check if subject is empty
if(empty($subject)) {
echo '<div class="error">';
echo 'Morate da napišete naslov';
echo '</div><br>';
} elseif(empty($text )) {
echo '<div class="error">';
echo 'Morate da opišete vaš problem';
echo '</div><br>';
} else {

//INSERT THINGS INTO THE DATABASE
$statement = $dbConn->prepare("INSERT INTO tickets (user_id, ticketnumber, subject, message, tickettime, status) VALUES (:user_id, :new_rand, :subject, :text, :tid, :settings_ticket_new)");
$statement->execute(array(
'user_id' => $user_id,
'new_rand' => $new_rand,
'subject' => $subject,
'text' => $text,
'tid' => $tid,
'settings_ticket_new' => 'Novi'
));
echo '<div class="success">';
echo 'Vaša poruka je poslana našoj podrsci. Uskoro ćemo odgovoriti na vaše pitanje';
echo '<div>';
//header("location: welcome.php?p=help");
}


}


?>

在文件(pages/jquery-pages/page_jquery_new_ticket.php)中提交时我的 index.php 中的变量 $userid 无法识别并且 $new_rand 也未被识别。我如何在提交页面时传输它们,以便将它们插入数据库。

希望你能理解我的问题。

干杯

最佳答案

您的 ajax 调用将序列化 $('#new-ticket') 形式的所有元素及其名称,并将它们传递到您的处理页面。 userid 未以该形式定义,因此它永远不会在 ajax 事务中传递到该页面。

如果没有别的事,将 user_id 变量定义为表单中的隐藏元素,然后它将被序列化并发送。

关于javascript - 无法通过JQUERY传递PHP变量并将其插入到MYSQL中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29109389/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com