可以';t通过JQUERY传递PHP变量并将它们插入MYSQL


Can't pass PHP variables through JQUERY and insert them into MYSQL

我正试图通过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未被识别为eighter。如何在提交页面时传输它们,以便将它们插入数据库。

我希望你能理解我的问题。

Cheerz

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

如果没有其他内容,请将user_id变量定义为表单中的隐藏元素,然后对其进行序列化并发送。