473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Generating multiple output on one page

Hello,
I'm building a website in PHP and Javascript. The registration
portion is divided into 2 sections:
1. In one, I get info about the visitor. This is sent via POST to a php
script which is section 2.
2. In the second section, I get some specific information about their
pets and make suggestions. I'm having problems with section 2, and I
hope y'all can help.

The core of the PHP script in Section 2 consists of the following:

<?php
for ($n=1; $n <= $num; $n++) {

print "Please enter age of pet #$n:" ;
print "<INPUT type='text' name='petage'&nbsp &nbsp &nbsp" ;
$div = $n . "ajaxDiv" . ";";
$_ENV['div'] = $div;
print "DIV IS: $div<br><br>";
print "<INPUT TYPE='button' onClick='ajaxFunction()' NAME='$age'
VALUE='Done!'>" ;
print "<INPUT TYPE='reset' NAME='Clear' VALUE='Reset form'>
<br><br>" ;
print "<div id='$div'></div><br><br>" ;

}

ajaxfunction() looks like:
if(ajaxRequest.readyState == 4){
var div = "<?php echo $_ENV['div']; ?>";
if (div = "") {
div = "1ajaxDiv";
}
var ajaxDisplay = document.getElementById('div');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
// Get pet stores based on zipcode
var zipcode = "<?php echo $zip; ?>"
var querystring = "?zipcode=" + zipcode;

ajaxRequest.open("GET", "getstores.php" + querystring, true);
ajaxRequest.send(null);
The idea is that the user fills in the age of their pet, I look in my
DB and spew out the names and addresses of vets and pet stores in the
area. Thanks to ajaxfunction(), I can do this through:
print "<INPUT TYPE='button' onClick='ajaxFunction()' NAME='$age'
VALUE='Done!'>" ;

The function calls another PHP script and outputs the data in the same
HTML file.

So here is my issue. This code works fine when there is only one pet.
If there are more than one, then clicking on the second button results
in no output at all.

Any suggestions?

I was thinking of enclosing it in a form like so:
<form method="post" action="/cgi-bin/samescript.php">
loop to output buttons
</form>

Meaning call the same script after clicking the first button, second
button and so on.

But I would then need to change:
print "<INPUT TYPE='button' onClick='ajaxFunction()' NAME='$age'
VALUE='Done!'>" ;

so when the user clicked on it, it would call ajaxFunction() AND the
PHP script.

Is this possible?

Thanks for listening and have a GREAT holiday season!!

Nov 27 '06 #1
3 2007
Hi Sandman --

Where does the variable $num come from? Is is the result of a count
function of some kind? It would help to know how this variable gets
set before we troubleshoot any further.

I might also suggest sending the div tag id when you call your
javascript function rather than setting an environment variable to hold
it. In this application, you can cut down on unnecessary (and possibly
confusing) code by doing so. Your function could be written like this:

function ajaxFunction(output)
{
...(other code)...
if (ajaxRequest.readyState === 4)
{
document.getElementById(output).innerHTML =
ajaxRequest.responseText;
}
}

Where "output" holds the div id of the tag that will contain the output
from your Ajax request. This is by no means the only way to do it, but
it just seems simpler and easier to me.
Geoffrey
Sandman wrote:
Hello,
I'm building a website in PHP and Javascript. The registration
portion is divided into 2 sections:
1. In one, I get info about the visitor. This is sent via POST to a php
script which is section 2.
2. In the second section, I get some specific information about their
pets and make suggestions. I'm having problems with section 2, and I
hope y'all can help.

The core of the PHP script in Section 2 consists of the following:

<?php
for ($n=1; $n <= $num; $n++) {

print "Please enter age of pet #$n:" ;
print "<INPUT type='text' name='petage'&nbsp &nbsp &nbsp" ;
$div = $n . "ajaxDiv" . ";";
$_ENV['div'] = $div;
print "DIV IS: $div<br><br>";
print "<INPUT TYPE='button' onClick='ajaxFunction()' NAME='$age'
VALUE='Done!'>" ;
print "<INPUT TYPE='reset' NAME='Clear' VALUE='Reset form'>
<br><br>" ;
print "<div id='$div'></div><br><br>" ;

}

ajaxfunction() looks like:
if(ajaxRequest.readyState == 4){
var div = "<?php echo $_ENV['div']; ?>";
if (div = "") {
div = "1ajaxDiv";
}
var ajaxDisplay = document.getElementById('div');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
// Get pet stores based on zipcode
var zipcode = "<?php echo $zip; ?>"
var querystring = "?zipcode=" + zipcode;

ajaxRequest.open("GET", "getstores.php" + querystring, true);
ajaxRequest.send(null);
The idea is that the user fills in the age of their pet, I look in my
DB and spew out the names and addresses of vets and pet stores in the
area. Thanks to ajaxfunction(), I can do this through:
print "<INPUT TYPE='button' onClick='ajaxFunction()' NAME='$age'
VALUE='Done!'>" ;

The function calls another PHP script and outputs the data in the same
HTML file.

So here is my issue. This code works fine when there is only one pet.
If there are more than one, then clicking on the second button results
in no output at all.

Any suggestions?

I was thinking of enclosing it in a form like so:
<form method="post" action="/cgi-bin/samescript.php">
loop to output buttons
</form>

Meaning call the same script after clicking the first button, second
button and so on.

But I would then need to change:
print "<INPUT TYPE='button' onClick='ajaxFunction()' NAME='$age'
VALUE='Done!'>" ;

so when the user clicked on it, it would call ajaxFunction() AND the
PHP script.

Is this possible?

Thanks for listening and have a GREAT holiday season!!
Nov 28 '06 #2
Hi Sandman --

Where does the variable $num come from? Is is the result of a count
function of some kind? It would help to know how this variable gets
set before we troubleshoot any further.

I might also suggest sending the div tag id when you call your
javascript function rather than setting an environment variable to hold
it. In this application, you can cut down on unnecessary (and possibly
confusing) code by doing so. Your function could be written like this:

function ajaxFunction(output)
{
...(other code)...
if (ajaxRequest.readyState === 4)
{
document.getElementById(output).innerHTML =
ajaxRequest.responseText;
}
}

Where "output" holds the div id of the tag that will contain the output
from your Ajax request. This is by no means the only way to do it, but
it just seems simpler and easier to me.
Geoffrey
Sandman wrote:
Hello,
I'm building a website in PHP and Javascript. The registration
portion is divided into 2 sections:
1. In one, I get info about the visitor. This is sent via POST to a php
script which is section 2.
2. In the second section, I get some specific information about their
pets and make suggestions. I'm having problems with section 2, and I
hope y'all can help.

The core of the PHP script in Section 2 consists of the following:

<?php
for ($n=1; $n <= $num; $n++) {

print "Please enter age of pet #$n:" ;
print "<INPUT type='text' name='petage'&nbsp &nbsp &nbsp" ;
$div = $n . "ajaxDiv" . ";";
$_ENV['div'] = $div;
print "DIV IS: $div<br><br>";
print "<INPUT TYPE='button' onClick='ajaxFunction()' NAME='$age'
VALUE='Done!'>" ;
print "<INPUT TYPE='reset' NAME='Clear' VALUE='Reset form'>
<br><br>" ;
print "<div id='$div'></div><br><br>" ;

}

ajaxfunction() looks like:
if(ajaxRequest.readyState == 4){
var div = "<?php echo $_ENV['div']; ?>";
if (div = "") {
div = "1ajaxDiv";
}
var ajaxDisplay = document.getElementById('div');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
// Get pet stores based on zipcode
var zipcode = "<?php echo $zip; ?>"
var querystring = "?zipcode=" + zipcode;

ajaxRequest.open("GET", "getstores.php" + querystring, true);
ajaxRequest.send(null);
The idea is that the user fills in the age of their pet, I look in my
DB and spew out the names and addresses of vets and pet stores in the
area. Thanks to ajaxfunction(), I can do this through:
print "<INPUT TYPE='button' onClick='ajaxFunction()' NAME='$age'
VALUE='Done!'>" ;

The function calls another PHP script and outputs the data in the same
HTML file.

So here is my issue. This code works fine when there is only one pet.
If there are more than one, then clicking on the second button results
in no output at all.

Any suggestions?

I was thinking of enclosing it in a form like so:
<form method="post" action="/cgi-bin/samescript.php">
loop to output buttons
</form>

Meaning call the same script after clicking the first button, second
button and so on.

But I would then need to change:
print "<INPUT TYPE='button' onClick='ajaxFunction()' NAME='$age'
VALUE='Done!'>" ;

so when the user clicked on it, it would call ajaxFunction() AND the
PHP script.

Is this possible?

Thanks for listening and have a GREAT holiday season!!
Nov 28 '06 #3
Hi Geoffrey,
$num is the number of times I would need to display the text boxes.
As it turned out I made 2 newbie errors:
1. A JavaScript variable must begin with a letter or an underscore. In
my initial code, I had:
$div = $n . "ajaxDiv" . ";";
which should have been
$div = "ajaxDiv" . $n;

2. You were right about sending it off as a parameter instead of
screwing around with ENV variables. However, the function's argument
should be quoted before sending it off as a parameter. So the above
variable should really be:
$div4js = "\"" . $div . "\"";

Thanks for your suggestion though, which got me thinking along those
lines.

Sandman

Geoffrey wrote:
Hi Sandman --

Where does the variable $num come from? Is is the result of a count
function of some kind? It would help to know how this variable gets
set before we troubleshoot any further.

I might also suggest sending the div tag id when you call your
javascript function rather than setting an environment variable to hold
it. In this application, you can cut down on unnecessary (and possibly
confusing) code by doing so. Your function could be written like this:

function ajaxFunction(output)
{
...(other code)...
if (ajaxRequest.readyState === 4)
{
document.getElementById(output).innerHTML =
ajaxRequest.responseText;
}
}

Where "output" holds the div id of the tag that will contain the output
from your Ajax request. This is by no means the only way to do it, but
it just seems simpler and easier to me.
Geoffrey
Sandman wrote:
Hello,
I'm building a website in PHP and Javascript. The registration
portion is divided into 2 sections:
1. In one, I get info about the visitor. This is sent via POST to a php
script which is section 2.
2. In the second section, I get some specific information about their
pets and make suggestions. I'm having problems with section 2, and I
hope y'all can help.

The core of the PHP script in Section 2 consists of the following:

<?php
for ($n=1; $n <= $num; $n++) {

print "Please enter age of pet #$n:" ;
print "<INPUT type='text' name='petage'&nbsp &nbsp &nbsp" ;
$div = $n . "ajaxDiv" . ";";
$_ENV['div'] = $div;
print "DIV IS: $div<br><br>";
print "<INPUT TYPE='button' onClick='ajaxFunction()' NAME='$age'
VALUE='Done!'>" ;
print "<INPUT TYPE='reset' NAME='Clear' VALUE='Reset form'>
<br><br>" ;
print "<div id='$div'></div><br><br>" ;

}

ajaxfunction() looks like:
if(ajaxRequest.readyState == 4){
var div = "<?php echo $_ENV['div']; ?>";
if (div = "") {
div = "1ajaxDiv";
}
var ajaxDisplay = document.getElementById('div');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
// Get pet stores based on zipcode
var zipcode = "<?php echo $zip; ?>"
var querystring = "?zipcode=" + zipcode;

ajaxRequest.open("GET", "getstores.php" + querystring, true);
ajaxRequest.send(null);
The idea is that the user fills in the age of their pet, I look in my
DB and spew out the names and addresses of vets and pet stores in the
area. Thanks to ajaxfunction(), I can do this through:
print "<INPUT TYPE='button' onClick='ajaxFunction()' NAME='$age'
VALUE='Done!'>" ;

The function calls another PHP script and outputs the data in the same
HTML file.

So here is my issue. This code works fine when there is only one pet.
If there are more than one, then clicking on the second button results
in no output at all.

Any suggestions?

I was thinking of enclosing it in a form like so:
<form method="post" action="/cgi-bin/samescript.php">
loop to output buttons
</form>

Meaning call the same script after clicking the first button, second
button and so on.

But I would then need to change:
print "<INPUT TYPE='button' onClick='ajaxFunction()' NAME='$age'
VALUE='Done!'>" ;

so when the user clicked on it, it would call ajaxFunction() AND the
PHP script.

Is this possible?

Thanks for listening and have a GREAT holiday season!!
Nov 28 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tristan Miller | last post by:
Greetings. I would like to produce a static multilingual website in XHTML. Is it possible to specify each web page in its own XML file, but have all of the translations encapsulated in that one...
4
by: Lee Chapman | last post by:
Hi, I am having difficulty getting the ASP.NET framework to generate valid XHTML. My immediate problem surrounds user input in, for example, textbox controls. I consider characters such as...
18
by: baltimoredude1 | last post by:
Hi I was writing a simple code to generate the first 100 prime numbers. Everything looks fine to me except the output of the program. What's wrong with it? I am attaching the program as well as...
3
by: Sandman | last post by:
Hello, I'm building a website in PHP and Javascript. The registration portion is divided into 2 sections: 1. In one, I get info about the visitor. This is sent via POST to a php script which is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.