javascript - Send JS variable to PHP -
i take value using getelementbyid() , pass variable on php.
<script> function addmachine() { var ip = document.getelementbyid('iptextbox').value; document.getelementbyid('iptextbox').submit; } <\script>
the html looks that
<div class="container"> <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#addmachine"> <div class="modal fade" id="addmachine" role="dialog"> <div class="modal-dialog modal-sm"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">×</button> <h4 class="modal-title">add new machine</h4> </div> <div class="modal-body"> <label>insert machine ip<input id="iptextbox"></input></label> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal" onclick="addmachine()">add</button> </div> </div> </div> </div> <\div>
i don't have ideas on how pass php variable.
php backend language used render html , send client on page load. , javascript client side language. if want send variables js php, sending information client server without page reload, need use ajax:
ajax stands "asynchronous javascript , xml". although name includes xml, json more used due it's simpler formatting , lower redundancy. ajax allows user communicate external resources without reloading webpage. stackoverflow's documentation on javascript ajax
- with ajax
i have made post on documentation page, here is. hope helps understand how works.
this function runs ajax call using allowing send parameters (object) file (string) , launch callback (function) when request has been ended.
function ajax(file, params, callback) { var url = file + '?'; // loop through object , assemble url var notfirst = false; (var key in params) { if (params.hasownproperty(key)) { url += (notfirst ? '&' : '') + key + "=" + params[key]; } notfirst = true; } // create ajax call url parameter var xmlhttp = new xmlhttprequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readystate == 4 && xmlhttp.status == 200) { callback(xmlhttp.responsetext); } }; xmlhttp.open('get', url, true); xmlhttp.send(); }
here's how use it:
ajax('cars.php', {type:"volvo", model:"300", color:"purple"}, function(response) { // add here code executed when data comes page // example console.log(response) show ajax response in console });
and following shows how retreive url parameters in cars.php
:
if(isset($_request['type'], $_request['model'], $_request['color'])) { // set, can use them ! $response = 'the color of car ' . $_request['color'] . '. '; $response .= 'it ' . $_request['type'] . ' model ' . $_request['model'] . '!'; echo $response; }
if had console.log(response)
in callback function result in console have been:
the color of car purple. volvo model 300!
- with html form
in html page have include form
<form action="get_data.php" method="get"> name: <input type="text" name="name"><br> e-mail: <input type="text" name="email"><br> <input type="submit"> </form>
and in get_data.php
(the target file) you'll have write:
<?php echo $_get["name"]; echo $_get["email"];
this second method redirect user get_data.php
, personnaly don't , prefer ajax efficiency.
Comments
Post a Comment