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

15

I'm trying to send a form through jquery and it works fine sort of . When hitting the submit button my 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) 

They are not proceded to the another file (page_jquery_new_ticket.php). Let me show you the code:

File: support.php

<div id="support">
<!-- START BACKGROUND SUPPORT -->
<div >
  <div >
    <div >
      <div >Pomoč i podrška</div>
      <div ><a href="?p=help">Pomoč</a></div>
      <div ><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 >
    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 >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 >';
          echo $settings_ticket." #".$new_rand;
          echo '</div>';
        }

    ?>

    <br>

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

    <input type="text" name="subject" placeholder="Napisite naslov">
    <br><br>
    <textarea name="message"  id="message" placeholder="Napisite vase pitanje ovdje"></textarea>
    <br><br>
    <input type="submit" name="send" value="<?php echo $settings_btn_send;?>" ><br><br>
  </form>





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

Here is the file: 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 >';       
        echo 'Morate da napišete naslov';
        echo '</div><br>';
      } elseif(empty($text )) {
        echo '<div >';   
        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 >';
        echo 'Vaša poruka je poslana našoj podrsci. Uskoro ćemo odgovoriti na vaše pitanje';
        echo '<div>';
        //header("location: welcome.php?p=help");
      }


}


?> 

When submitting in file (pages/jquery-pages/page_jquery_new_ticket.php) The variables $userid from my index.php are not recognized and also $new_rand is not eighter recognized. How can i transfer them when submitting the page so i can insert them into the database.

I hope you understand my question.

Cheerz

242

Answer

Solution:

Your ajax call will serialize all the elements of the form$('#new-ticket') with names and pass them to your processing page. userid isn't defined in that form, therefore it will never be passed across to that page in the ajax transaction.

If nothing else, define the user_id variable as a hidden element in the form, it'll then be serialized and sent across.

People are also looking for solutions to the problem: javascript - Yii2: How not to have any date on edit in a datepicker field?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.