0

I have a form in my website. Inside the form, i have a select menu whose take data from a sql table (charging with a loop). I display the different parts of the form with some js scripts (which is working perfectly). In the last step, i have an overview of submitted datas. But i dont know how to get the "select" data and show it in the same page.

<form role="form" id="myform" method="post" action="logs_post.php">
                        <div class="tab-content">
                            <div class="tab-pane active" role="tabpanel" id="step1">
                                <div class="step1">
                                    <h3> Choose the Client</h3>
                                    <div class="row">
                                        <div class="col-md-12">
                                            <script type="text/javascript">
                                                $(document).ready(function() {
                                                    $(".js-example-basic-single").select2(
                                                    );
                                                });
                                            </script>
                                            <label for="exampleInputEmail1">TA Name</label>
                                            <?php 
                                            echo '<select class="js-example-basic-single  form-control"  name="TAName"  id="TAName" style="color:black;" >';

                                            while($cat = $NAMEClients->fetch()){
                                                echo '
                                                <option style="color:black;" value="'.$cat[ID].'">'.'<p id="nameta" >'.$cat[nom_agence].'</p>'.'  -  '.'<p id="office" >'.$cat[OID].'</p>'.'</option>';
                                                }
                                                echo '</select>';
                                                ?>
                                                <?php json_encode($NAMEClients) ?>
                                        </div>
                                    </div>
                                </div>
                                <ul class="list-inline pull-right">
                                    <li><button type="button" id="thebutton" class="btn btn-primary next-step">Save and continue</button></li>
                                </ul>

                                <script>
                                $('#thebutton').click(function(e) {
                                   e.preventDefault();
                                    console.log($('.js-example-basic-single').val());
                                });
                                </script>


                                <?php 
                                    /*$requete =  "SELECT nom_agence, OID FROM agencies WHERE ID="($('.js-example-basic-single').val()) " ";
                                    $reponse->query($requete);*/
                                ?>
                            </div>...........

I have put in comment the request i want to execute. My problem is with WHERE ID="($('.js-example-basic-single').val()) "

Can you give me some ideas on how to perform that.

NB: i've seen some AJAX suggestions but i dont know how to use it.

1
  • 1
    PHP and JavaScript can't directly interact like that. You'd need to either post a form or make an AJAX request to send a client-side value to the server for use by PHP. If you choose to use AJAX, I guess you'd want to start with some tutorials and examples. "I don't know how to use it" isn't really an answerable question. There are many walk-throughs available. A Google search for something like "PHP AJAX tutorial" would help. Commented Apr 16, 2017 at 11:16

2 Answers 2

1

Use jquery ajax() here, its syntax is very simple:

var elemId = $('.js-example-basic-single').val();

$.ajax({
  url: 'process.php',
  method: 'post',
  data: {
    // key: value pair like
    id: elemId,
    success: function(response){
       // Do something here
    }
  }
});

process.php:

$id = $_POST['id'];

here $id contains the value of $('.js-example-basic-single').val();

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, so will the page reload, because of the post process ?
There is no page reload when you are using ajax()
1

You can update the page undergorund without refresh. It is a good practice to have separate files for HTML, CSS, JavaScript, PHP. Do not lump all on a single file. This is the way forward. I notice you are using the class selector and have a button. So try this method:

<script>
$(document).ready(function() {
$('#thebutton').click(function() {
    var data = {                
        basicSingle : $('.js-example-basic-single').val()   // or use .text() if val() doesn't work
    };
    $.post('phpFilename', data, function(data) {
        console.log(data);  //php returns data      
    });             
  };
};
</script>


<?php
basicSingle = test_input($_POST["basicSingle"]);
$sql = /* your code here */
echo data;
mysqli_close($conn);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.