We help IT Professionals succeed at work.

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

168 Views
Last Modified: 2017-03-20
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

Imran AliWeb Developer

Commented:
You want search to return exact 3 results to populate textareas?
CERTIFIED EXPERT
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.
CERTIFIED EXPERT
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
Author of the Year 2014

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
Author of the Year 2014
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION