473,406 Members | 2,281 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,406 software developers and data experts.

PHP echoed into Javascript

Hi

I'm trying to echo some php into a piece of javascript that checks that the selectedIndex in a given select name is not 0, and gives an alert if this is so:
////
<script language="JavaScript"><!--
function validate(form) {
if (form2.pmethod.selectedIndex == 0) {
alert('You have not selected a postage method. Please select one.');
return false;
}
////
This all works fine, but when I try to echo some php into this from a foreach post loop, it doesn't work. From playing around with it, it is the squae brackets that cause the script not to run properly:

<?
foreach ($_POST['problem'] as $postName=>$postValue)
{
?>
if (form2.[<?echo''.$postValue.'';?>].selectedIndex == 0) {
alert('You have not selected a replacement type for <?echo''.$postValue.'';?>. Please select one.');
return false;
}
<?
}
?>

Does anyone know a way around the square brackets, or an alternative way to check the form, given that the name of the select option is echoed out through a foreach loop in the following format:

name="replacement_type['.$postValue.']"

Thanks.
Jan 12 '07 #1
4 1339
b1randon
171 Expert 100+
Hi

I'm trying to echo some php into a piece of javascript that checks that the selectedIndex in a given select name is not 0, and gives an alert if this is so:
////
<script language="JavaScript"><!--
function validate(form) {
if (form2.pmethod.selectedIndex == 0) {
alert('You have not selected a postage method. Please select one.');
return false;
}
////
This all works fine, but when I try to echo some php into this from a foreach post loop, it doesn't work. From playing around with it, it is the squae brackets that cause the script not to run properly:

<?
foreach ($_POST['problem'] as $postName=>$postValue)
{
?>
if (form2.[<?echo''.$postValue.'';?>].selectedIndex == 0) {
alert('You have not selected a replacement type for <?echo''.$postValue.'';?>. Please select one.');
return false;
}
<?
}
?>

Does anyone know a way around the square brackets, or an alternative way to check the form, given that the name of the select option is echoed out through a foreach loop in the following format:

name="replacement_type['.$postValue.']"

Thanks.
That's the way you do it in javascript. The real problem is your php. You should most this post to the php section where they can help you figure out why your php is not working properly.
Jan 12 '07 #2
Yeah, but if I change the name of the select name to

replacement_type'.$postValue.'

instead of:

replacement_type['.$postValue.']

and echo the javascript out as below:
*********
<?
foreach ($_POST['problem'] as $postName=>$postValue)
{
?>
if (form2.<?echo''.$postValue.'';?>.selectedIndex == 0) {
alert('You have not selected a replacement type for <?echo''.$postValue.'';?>. Please select one.');
return false;
}
<?
}
?>

**********
i.e. - removing the square brackets from the javascript, it works perfect. It seems that the square brackets in the name cause the javascript not to work properly.

ANy ideas? Is there a different way I can write the javascript, or php?
Jan 13 '07 #3
I've fixed it:

I used getElementById instead, so the php can work and the javascript can work:

<?
foreach ($_POST['problem'] as $postName=>$postValue)
{
?>
var replacement_type = document.getElementById ('replacement_type<?echo''.$postValue.'';?>');
if ((replacement_type).selectedIndex == 0)
{
alert('You have not selected a replacement type for <?echo''.$postValue.'';?>. Please select one.');
return false;
}
<?
}
?>
Jan 13 '07 #4
AricC
1,892 Expert 1GB
Danny is your problem fixed or should I move this to the PHP forum?
Jan 13 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: TJ | last post by:
Hello All, I posted a question a few days ago entitled "Select element with no selected options". Over the course of the discussion, RobG wrote: > This is just an example, it is not really...
7
by: Tasha's Dad | last post by:
A description of the problem: 1) Go to a page with various settings and a timeout (forces re-login if over 10 minutes) 2) Before the timeout, make some changes to settings. 3) Press a "reset to...
3
by: dshan | last post by:
Hi all, I can't find an answer to my quesiton anywhere. Given a drop-down list with USA states and other text fields I pass this information for further processing via PHP. If any of the...
2
by: pantagruel | last post by:
Hi, I'm trying to write to a file. The file is getting created. The file is even getting written to because the file ends up being one kb. so I guess there is something coming in. The code...
4
by: mantrid | last post by:
Can anyone see why the checkbox in the code below doesnt get ticked when the textbox is changed ***This javascript function is called in the textbox's onchange event to check it when it is...
3
by: Mani | last post by:
Hi I am trying to post the values to the form. The form action is not done in html i.e not in form tag, it is done by javascript code using document.FORMNAME.action = "results.asp"; I think...
6
by: orfiyus | last post by:
Wutup Im trying to write a php script that has a link. When the user clicks the link the link turns into a text field which the user can modify. When the user clicks elsewhere the text that the...
1
by: neilfarah | last post by:
I have tried with one for loop or two for loops. I've even separated each firstChild.nodeValu e by a semicolon and I still get the same result. It will print with one of the nodeValues, but not...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.