1

I apologize for the ease of this question for you. But I looked at your beautiful site for an answer to my problem, but I was not lucky. I'm trying to build my own,form-wizard for student registration, in easy steps as a graduate project for university. I use PHP, JQuery and AJAX to send data .

My problem:

I have a single form and to button , The first three input are searched in the database via the submit button and it is worked good , then automatically form-wizard moves to the next fieldset and then displays the inpust field to student to enter his information .

finally thir is button to save data to database by AJAX and this button is my problem . the php say undefined index .

this is code for html wizard-form

$('form').on('submit', function(e) {
  var $formInput = $(this).find('.fi');

  $formInput.each(function(i) {
    if (!$(this).val()) {
      e.preventDefault();
      $(this).addClass('input-error');
      return false;
    } else {
      if ($formInput.length === i + 1) {
        var id_high_school = $('[name="id_high_school"]').val();
        var SEC_SCHOOL_YEAR = $('[name="SEC_SCHOOL_YEAR"]').val().toString();
        var sum_high_school = $('[name="sum_high_school"]').val();

        alert(SEC_SCHOOL_YEAR);
        $.ajax({
          url: "select_for_modal_serch.php",
          method: "post",
          data: {
            id_high_school: id_high_school,
            SEC_SCHOOL_YEAR: SEC_SCHOOL_YEAR,
            sum_high_school: sum_high_school

          }
        }).done(function(datas) {
          $('#studint_detail').html(datas);
          $('#dataModal').modal("show");
        }).fail(function() {
          alert('fail..');
        });

      }


    }

  });


  return false;

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<form name="mainForm" action=" <?php echo $_SERVER['PHP_SELF'] . '#form1' ?> " id="form1" method="POST" enctype="multipart/form-data">

  <!-- condetion for regster -->
  <fieldset>
    <iframe src="license.html"></iframe>
  <input class="form-check-input" id="check_qury" type="checkbox" value="yes">  
    <div class="wizard-buttons">

      <button type="button" class="btn btn-next">التالي</button>

    </div>
  </fieldset>
  <fieldset>
    <!-- 3 <input type = text > to search for data from mysql -->
    <!--her is the submit button and is get data by ajax  -->
    <input type="submit" class="form-control btn btn-primary view-data" id="submit_high_school" value="بحث" name="submit_high_school" />
    <!-- by ajax is work and then is go to the next filedset -->
  </fieldset>
  <fieldset>
    <!-- mor data <input type = text > to search for data from mysql -->
    <button type="button" id="sava_data_to_tables" name="sava_data_to_tables" class="btn btn-next">
 <!-- this is the button of my problem cant send this form data to mysql -->
  </fieldset>
I doing this code to solve the problem but nothing work :

$('#sava_data_to_tables').on('click', function (event) {

        var form_data = $(this).parents('form').serialize();

        var colage = $('[name="colage"]').val();
        var spichelest = $('[name="spichelest"]').val();
        
        // and rest of input type 

        $.ajax({
            url: "insert_into_info_contact.php",
            method: "POST",
            data: form_data,
            success: function (data) {
                alert(data);
            }, cache: false,
            contentType: false,
            processData: false
        });

    });

and my PHP :

 <?php 

// isset($_POST['sava_data_to_tables'])
//$_SERVER['REQUEST_METHOD']==='POST'
// !empty($_POST)

if ($_SERVER['REQUEST_METHOD']==='post')
{  echo "you submit data" 
;}
else {

echo "its not work" ; }
?> 
Anas
  • 37
  • 1
  • 9
  • Can you do a `var_dump($_POST);` in your php and post the results here? – jrswgtr Jul 28 '18 at 21:49
  • @JorisWagter this is the result array(0){ } – Anas Jul 28 '18 at 22:01
  • Your post values are empty. That means the data is not sent by your ajax function. Check the value of form_data with a console.log and post the results here. – jrswgtr Jul 28 '18 at 22:44
  • Try `var form_data = $(this).closest('form').serialize();` And make sure there is some values entered in the form. Like if you want the checkbox value, you have to check it. And add a `name` attribute to the fields you want to send. See this [CodePen](https://codepen.io/Bes7weB/pen/BPmwJK?editors=1011) – Louys Patrice Bessette Jul 28 '18 at 23:53
  • @jrswgtr I apologize for delaying because of electricity and internet . – Anas Jul 29 '18 at 11:05
  • @jrswgtr this is the result of console.log(form_data) : SEC_SCHOOL_YEAR=2016&id_high_school=515272&sum_high_school=649&colage=7&spichelest=17&stu_addres=sdf&stu_street=df&stu_phone=65&father_stu_name=sdf&father_stu_jop=adf&father_stu_jop_addres=sdf&father_stu_jop_kind=on&father_name_rel=%D9%88%D8%A7%D9%84%D8%AF%D8%A9&father_stu_addres=d&father_stu_phone=56&father_stu_email=ss%40ss.ss&father_stu_kind=on&frind_stu_name=sdf&frind_stu_rel+=%D9%88%D8%A7%D9%84%D8%AF%D8%A9&frind_stu_jop=ad&frind_stu_jop_addres=5&frind_stu_phone=-1&frind2_stu_name=ad&frind2_stu_r ... I think this is my data is her – Anas Jul 29 '18 at 11:08
  • I see something in your php code. `($_SERVER['REQUEST_METHOD']==='post')` is `false` because `'POST'` should be in capitals. try `($_SERVER['REQUEST_METHOD'] === 'POST')`. There also isn't any field in you data above with the key `sava_data_to_tables`. – jrswgtr Jul 29 '18 at 11:12
  • there is data in form_data SEC_SCHOOL_YEAR=2016&id_high_school=515272&sum_high_school=649&colage=6&spichelest=15&stu_addres=home&stu_street=stret+&stu_phone=77777&father_stu_name=name&father_stu_jop=job&father_stu_jop_addres=work&father_stu_jop_kind=on&father_name_rel=%D9%88%D8%A7%D9%84%D8%AF%D8%A9&father_stu_addres=home&father_stu_phone=7777&father_stu_email=ss%40ss.ss&father_stu_kind=on&frind_stu_name=frend1&frind_stu_rel+=%D9%88%D8%A7%D9%84%D8%AF%D8%A9&frind_stu_jop=job1&frind_stu_jop_addres=addres1&frind_stu_phone=123&frind2_stu_name=frend2&frind2_stu_rel=%D9%88%D8%A7%D9%84%D8%AF%D8%A9& .. – Anas Jul 29 '18 at 11:13
  • @jrswgtr is get me in if condtion , its right it have to be capitals .. but stile the same error because I set in if (){var_dump($_POST);}else{var_dump($_POST);} and I have the same error – Anas Jul 29 '18 at 11:19
  • @LouysPatriceBessette get the same error>>var form_data = $(this).closest('form').serialize(); Undefined index: .. – Anas Jul 29 '18 at 11:25
  • Ok... So there are data on the client-side, before the Ajax request is done. Now on PHP side, the `undefined index` error is because you try to access an array value that does not exist. `var_dump($_POST)` is a debugging tool to see what is in the array. You have a line number with each PHP error, so you should know exactly on which line it is. [More reading here](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Louys Patrice Bessette Jul 29 '18 at 15:07

1 Answers1

0

I found some help from a friend .. He just change this :

var form_data = $(this).closest('form').serialize();

to this :

 var form_data = new FormData($(this).closest('#form1').get(0));

He solved my problem. I honestly did not understand what the problem was, but he told me I had sent a different kind of data >> Thanks every One . :)

Anas
  • 37
  • 1
  • 9