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

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

Jhovyn MarcosAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Imran AliWeb DeveloperCommented:
You want search to return exact 3 results to populate textareas?
0
Leonidas DosasCommented:
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

0
Jhovyn MarcosAuthor 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.
0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

Leonidas DosasCommented:
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

0
Jhovyn MarcosAuthor Commented:
@Leonidas Dosas
Thank you for your time Sir... I've followed your code above but the data is not populating in my textarea.. :(
0
Ray PaseurCommented:
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
0
Ray PaseurCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.