简体   繁体   中英

Pass value from a href inside a bootstrap modal using php

I have a link like this.

<a value-id='1' href='#myModal' class='marker' title='Edit'>LINK</a>

Inside the modal i would like to receive the value in php.

<div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">

            <?php
                //CODE TO PUT THE VALUE INSIDE A VARIABLE
            ?>    

        </div>
    </div>
</div>

My question is how to put the value inside the variable...

Call a ajax function on onclick like this :-

<a value-id='1' href='#myModal' class='marker' title='Edit'  onclick="functionname(passifvalueisdynamichere)">LINK</a>

Now if required get value from server side then you can call like :-

  function functionname(id) {
        xmlhttp = getobject();
        var query = "id="+id+"&action=setdraftMessage";
        var base_url = document.getElementById("baseurlval").value;
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4) {
                var newArray = new Array();
                      ////get value from server side///////////////
                var result = xmlhttp.responseText;
                ///setvalue like///
document.getElementById("mailid").value =newArray[0];

            }
        };
        xmlhttp.open("GET", base_url + "index.php/pass/index?type=setdraftMessage&"+ query, true);
        xmlhttp.send(null);
    }

JavaScript is a client-side language and PHP is a server-side language. You cannot run PHP code inside your JavaScript.

If you want to populate data in the modal and send it to the back-end, submit a form to a PHP script on your server.

Don't use an <input type="submit" /> inside of your modal. It will not play nicely with Bootstrap. Use a link to submit the form via Ajax. You can style the link as a Bootstrap button.

For Example, if the link has a class of submit and the form has an ID of 'my-form' you can do the following in jQuery to submit the form:

$(function() {
     $('.modal-footer').on('click', '.submit', function() {
         var form = $('#my-form');
         var data = form.serialize(); 
         var url = form.attr('action');
         $.post(url, data)
             .done(function() {
                 alert('thanks for submitting your form!')
             })
             .fail(function() {
                 alert('uh oh, something went wrong! PLease try again');
             })
     });

 });

Obviously you'll have to write a PHP script to handle the form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM