简体   繁体   中英

Assigning textbox value to php variable without using form

I want to retrieve textbox value and assign it to php variable but without using form. I am doing search functionality in which i want to hide some contents on button click and and on the same click want searching function but if i use form then either it will redirect to another page or again reload the page which i don't want.

So i want some another method for this.

Ajax, in addition of being a hero of the trojan war according to greek mythology... is the technology you need, and also your friend.

I recommend to use jQuery [As Barry has posted faster than me, because I wanted to make sure I got the mythology reference right...]

And as MaxArt has pointed out, there are actually two greek heroes named "Ajax", both did participate in the trojan war (and both in Homer's Iliad).


You can use jQuery like so:

<!-- Link jQuery (replace src with the location where you have jQuery) -->

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js">
</script>

<!-- Try something like this [having jQuery linked]: -->

<script type="text/javascript">
     function doButtonStuff()
     {
         //Use jQuery to "hide some contents"
         $('#stufftohideId').hide(); //stufftohideId identifies what to hide

         //set the url of the web page that will take this info on your server
         var ulr = "http://localhost";

         //The following part will fail if you cannot connect to the server
         $.post(
             url,
             {text: $('#textboxId').val()}, //textboxId identifies the textbox
             function (data)
             {
                 //data is what the server responded
                 //do something with data, for instance:
                 alert(data);
             }
         );
     }
</script>


<input type="text" id="textboxId"></input>
<input type="button" onclick="doButtonStuff();" value="Click Me"></input>

<div id="stufftohideId">
    <p>
        This is an example of something to hide.
    </p>
</div>

You will get on the server side, of the web page with the url you passed like so:

<?php $text = $_POST['text']; ?>

Make sure to verify the request method, for example:

<?php
    if ($_SERVER['REQUEST_METHOD'] == 'POST')
    {
        $text = $_POST['text'];
        //do something with $text, for instance:
        echo $text;
    }
?>

And also to validate and sanitize the input. If a session is required verify that too. Whatever the server returns, will be recieved in the client side.

如果您不希望重新加载页面,请使用例如jQuery的AJAX: http : //api.jquery.com/jQuery.ajax/

You can prevent page reload or redirect by using ajax implementation for PHP. You could check CakePHP, tppAjax...

$(document).ready(function() {

    $("#textboxID").change(function(){
        var text = $(this).val();
    });

});

Using jQuery, you can use the .change method within the document ready function to get the text field value. Once you get the value via jquery you can perform some ajax and send the javascript variable to a php page where you can use the $_GET method to get it and at this point you have your variable in the php and you can do whatever you want with it.

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