Php - how to do multiple search w/ multiple output alternately

Jhovyn Marcos
Jhovyn Marcos used Ask the Experts™
on
I have a question regarding on multiple output alternately with only one search engine....I have this scenario below..

Scenario:

    First search the data. The output will go in textarea1(id='content')
    Second search the data. The output will go in textarea2(id='content1')
    Third search the data. The output will go in textarea3('content2')

NOTE: I only used one search engine..

Please help me :( Thank you and have a nice day! Below is my code

Table: users
 CREATE TABLE users
 (id INT PRIMARY KEY AUTO_INCREMENT,
 name VARCHAR(50),
 age INT)

Open in new window



      

I have a question regarding on multiple output alternately with only one search engine....I have this scenario below..

Scenario:

    First search the data. The output will go in textarea1(id='content')
    Second search the data. The output will go in textarea2(id='content1')
    Third search the data. The output will go in textarea3('content2')

NOTE:I only used one search engine..

Please help me :( Thank you and have a nice day! Below is my code

Table: users

 CREATE TABLE users
 (id INT PRIMARY KEY AUTO_INCREMENT,
 name VARCHAR(50),
 age INT)

index.php
<html>
<head>

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">
function get() {
$.post('data.php', { name: form.search.value },
function(output) {
$('#content').html(output).show();
$('#content1').html(output).show();
$('#content2').html(output).show();
}); 
}

</script>
</head>
<body>
<p>
<form name="form">
    <input type="text" name="search">
    <input type="button" value="Get" onClick="get();">  
</form>
<div>
<textarea id='content' cols='15' rows='5'></textarea>
<textarea id='content1' cols='15' rows='5'></textarea>
<textarea id='content2' cols='15' rows='5'></textarea>
</div>
</p>

</body>
</html>

Open in new window


data.php
require 'connection.php';

$name = mysql_real_escape_string($_POST['name']);

if($name==NULL)
echo "Please enter a name";

else{
$age = mysql_query("SELECT age FROM users WHERE memberName='$name'");
$age_num_rows = @mysql_num_rows($age);

if($age_num_rows==0)
echo "Name does not exists";

else
{
$age = mysql_result($age, 0);   
echo "$name's age is $age";
}
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Imran AliWeb Developer

Commented:
You want search to return exact 3 results to populate textareas?
Distinguished Expert 2017

Commented:
You must parse at json type the echo php  variable like this:

if($age_num_rows==0)
echo json_encode("Name does not exists");

else
{
$age = mysql_result($age, 0);   
echo json_encode("$name's age is $age");

Open in new window


And then in get function you must declare the data type as json like this:

function get() {
$.post('data.php', { name: form.search.value },
function(output) {
$('#content').html(output).show();
$('#content1').html(output).show();
$('#content2').html(output).show();
},'json'); 
}

Open in new window

Author

Commented:
@Imran Ali

I want to populate my data in textarea not simultaneously... If I search a data, the record will be populate to my first textarea and when I do search again, the record will be populate to my second textarea.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Distinguished Expert 2017

Commented:
I make some changes in your code. First I check via for loop when a textarea content is null and the via get method i set the output.
function get() {
 var valueText=$('textarea');
 var element; 
  function(){
  for(var i=0;i<=valueText.length;i++){
    if(valueText[i].val()!==null){
      continue;
    }else{
      element=valueText[i];
       return element;
    }
    
  }
  

$.post('data.php', { name: form.search.value },
function(output) {
element.html(output).show();
},'json'); 
}
}

Open in new window

Author

Commented:
@Leonidas Dosas
Thank you for your time Sir... I've followed your code above but the data is not populating in my textarea.. :(
Most Valuable Expert 2011
Top Expert 2016

Commented:
First search the data. The output will go in textarea1(id='content')
Second search the data. The output will go in textarea2(id='content1')
Third search the data. The output will go in textarea3('content2')

NOTE:I only used one search engine..
This design pattern doesn't make any sense to me.  Why would you create three outputs from a single search?  Maybe if you can describe the real problem we can help with the design pattern.

For an example of how jQuery and AJAX work together, please see this article.  It has tested and working code examples you can copy.
https://www.experts-exchange.com/articles/10712/The-Hello-World-Exercise-with-jQuery-and-PHP.html
Most Valuable Expert 2011
Top Expert 2016
Commented:
Please see:
https://iconoun.com/demo/temp_marcos_client.php

There are two parts to AJAX requests - the client side and the server side.  Here is the server side first, it simulates the database lookup, which I cannot do for you because I do not have your database.
<?php // demo/temp_marcos_server.php
/**
 * https://www.experts-exchange.com/questions/29009129/Php-how-to-do-multiple-search-w-multiple-output-alternately.html
 *
 * https://www.experts-exchange.com/articles/10712/The-Hello-World-Exercise-with-jQuery-and-PHP.html
 */
error_reporting(E_ALL);


// MAKE SURE THAT PHP WORKS WITH UTF-8
mb_internal_encoding('UTF-8');
mb_regex_encoding('UTF-8');


// SANITIZE THE REQUEST VARIABLE AS NEEDED
$q = !empty($_GET['q']) ? trim(strtolower($_GET['q'])) : '[null]';


/************* MAKE THE SEARCH HERE ************/


// RETURN THE RESPONSE
echo "$q SEARCH - simulated results";

Open in new window

Next the client side.  It makes a "search" and puts the output (server response) into three textarea controls.
<?php // demo/temp_marcos_client.php
/**
 * https://www.experts-exchange.com/questions/29009129/Php-how-to-do-multiple-search-w-multiple-output-alternately.html
 *
 * https://www.experts-exchange.com/articles/10712/The-Hello-World-Exercise-with-jQuery-and-PHP.html
 */
error_reporting(E_ALL);


// MAKE SURE THAT PHP WORKS WITH UTF-8
mb_internal_encoding('UTF-8');
mb_regex_encoding('UTF-8');


// CREATE OUR WEB PAGE IN HTML5 FORMAT, USING HEREDOC SYNTAX
$htm = <<<HTML5
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta charset="utf-8" />
<meta name="robots" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<style type="text/css">
/* STYLE SHEET HERE */
</style>

<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<script>

$(document).ready(function(){
    $("#signal").click(function(){
        indata = $("#xinput").val();
        $.get("temp_marcos_server.php", {q:indata}, function(response){
            $("#content").html(response);
            $("#content1").html(response);
            $("#content2").html(response);
        });
    });
});
</script>

<title>HTML5 Page With jQuery in UTF-8 Encoding</title>
</head>
<body>

<noscript>Your browsing experience will be much better with JavaScript enabled!</noscript>

<p>
<form name="form">
    <input type="text" name="search" id="xinput">
    <input type="button" value="Get" id="signal">
</form>
<div>
<textarea id='content' cols='15' rows='5'></textarea>
<textarea id='content1' cols='15' rows='5'></textarea>
<textarea id='content2' cols='15' rows='5'></textarea>
</div>
</p>

</body>
</html>
HTML5;


// RENDER THE WEB PAGE
echo $htm;

Open in new window

Hopefully these code examples and the article will help you move forward!  

With that said, you still face another hurdle, albeit a small one.  You can't use MySQL any more.  It has been removed from PHP, so you will need to choose another extension.  This article can help you with that part.  It has parallel examples showing how to make the migration from the old MySQL to MySQLi or PDO.
https://www.experts-exchange.com/articles/11177/PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

On the chance that you're new to PHP and want to learn the language, this article can help you find good learning resources (and more importantly, avoid the many bad and outdated examples that litter the internet).
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html

Best of luck with your project, and please post back if you still have questions.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial