Avatar of Jhovyn Marcos
Jhovyn Marcos
 asked on

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

PHPMySQL ServerJavaScriptjQuery

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
Imran Ali

You want search to return exact 3 results to populate textareas?
Leonidas Dosas

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

Jhovyn Marcos

ASKER
@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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Leonidas Dosas

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

Jhovyn Marcos

ASKER
@Leonidas Dosas
Thank you for your time Sir... I've followed your code above but the data is not populating in my textarea.. :(
Ray Paseur

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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question