我试图通过jquery发送一个表单,它的工作方式很好。 点击提交按钮我的varibles:
$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也没有被认可。如何在提交页面时传输它们,以便我可以将它们插入到数据库中。
我希望你理解我的问题。
Cheerz
答案 0 :(得分:0)
您的ajax调用将使用名称序列化$('#new-ticket')
形式的所有元素,并将它们传递到您的处理页面。 userid未在该表单中定义,因此永远不会在ajax事务中传递给该页面。
如果没有别的,将user_id变量定义为表单中的隐藏元素,然后将其序列化并发送。