php tutorial - PHP strpos with $.ajax and $.post jQuery to search strings without refreshing - php programming - learn php - php code - php script
Search string in PHP
In the last chapter, I explained how you can use the strpos function of PHP to search strings. I used hard-coded values along with a demo where a user entered value is taken to search string by using an HTML form. As a user presses the submit / search button, it will return the result after refreshing the web page and tells whether source string contains the search term or not.
In certain situations, you may need to return the search results without reloading the web page. One of the ways is to accomplish that task is to use PHP strpos function with jQuery ajax / post or get methods.
The $.ajax is the main method while $.post and $.get are shorthand methods of ajax method. I will show you using $.ajax and post jQuery methods to work with strpos PHP function to search strings without reloading the web page.
A demo to use strpos and $.ajax method
In the demo page, a textbox is given without any submit button. Enter some text or search term in the textbox and press Tab key or click somewhere outside the textbox that will result in running the PHP code that checks the search term in source string by using PHP strpos function:
Following is the source string that I used in the demo:
Try the demo online with search terms in source string:
Learn php - php tutorial - php-strpos-ajax - php examples - php programs
In real time applications, the source strings can be database driven or arrays etc. I have used this hard-coded string just to explain how strpos function works.
Let me explain the PHP and jQuery code used in the example:
First of all, a textbox with id=”searchterm” is created in the markup section:
At the change event of the textbox, that occurs as you leave the textbox by using a Tab key or clicking mouse somewhere else, after changing the text inside it. The following jQuery code executes which is placed in the <script> section of <head> tag:
The value of textbox is assigned to a variable. After that, the jQuery ajax method was used that called the strpos-php.php file. The strpos-php.php file is placed at the same location where $.ajax method is used.
As $.ajax method is called, it also sends data which is the search term entered in the textbox. Before proceeding to “success” part, the code inside the strpos-php.php file will execute.
Following is the code inside strpos-php.php file:
You can see, first the sent parameter’s value is taken by using the $_REQUEST array. After that, strpos method is used:
It checks the search string in the source string which is $chkstring, in that case. If search term that you entered is found, the echo statement will display a message.
The result will be returned to the “success” part of $.ajax method which is received by “searchresult” parameter. See this line in jQuery code again:
The searchresult will be displayed in another div with class=”result” in the markup section.
A demo to use strpos with $.post jQuery method
As I mentioned earlier, the $.post is a high level method of $.ajax which is easier to use. It loads data by using HTTP request.
In the following example, I will use almost the same code in PHP file while jQuery code uses the $.post method rather $.ajax.
See the demo and code online:
Learn php - php tutorial - php-strpos-post - php examples - php programs
The main difference between this and above example is the jQuery code:
You can see how $.post method is calling the strpos-post.php file along with sending a parameter.
The code for the PHP file is almost the same except I used the $_POST array instead of $_REQUEST to get the value of sent parameter.
Case insensitive search by using PHP stripos and jQuery
As such, the strpos function is case sensitive. If you use “Demo” and “demo” in search, it has different meanings.
To enable case-insensitive search, you can use stripos PHP function.
See the following demo, where I simply replaced strpos method with stripos in the above example with $.post jQuery.
Try searching terms with capital and small letters:
Only this line of code is changed in the PHP file:
You can see, if you search SOURCE in the above example with strpos function, it will display the result as not found.
Whereas, search with stripos function will result as found!