You are here: Re: calling a javascript alert from php « PHP Programming Language « IT news, forums, messages
Re: calling a javascript alert from php

Posted by Rik on 07/14/06 01:14

IchBin wrote:
> I am following this thread because I am trying to do something
> simular.
> That is pass value back to php on a select ONCHANGE=. Problem is I do
> not know php\javascript all that well. I had posted my code to this
> newsgroup but received no answer. So I do not know if I can do what I
> am
> trying to do or I am just so far off that it does not warent an
> answer.

javascript/HTML is just plain text to PHP, you're better of asking an a
javascript group how you should accomplish what you're trying, get the
javascript code for that, and then build it in PHP.

What is it exactly you're trying to accomplish, because I can't make it out
from the following code?
(and BTW: <select> doesn't have a cols attribute...).

> This is what I have so far. I can not get to work and have comment out
> what I am trying:
>
> //
> // Handle the data coming back
> echo '<FORM NAME=author method=post action='.$_SERVER['PHP_SELF'].'>';
> //echo '<SELECT NAME=author_pk SIZE=20 COLS=20
> ONCHANGE='.$_POST[id].'>'; //echo '<SELECT NAME=author_pk SIZE=20
> COLS=20 ONCHANGE=getPK()>';
> echo '<SELECT NAME=author_pk SIZE=20 COLS=20>';
>
> while ($rows = mysql_fetch_object($result)) {
> echo '<OPTION VALUE='.$rows->id.'>'.
> $rows->TITLE.' '.
> $rows->lastname.', '.
> $rows->firstname.
> $rows->middlename.
> $rows->SUFFIX.
> '</OPTION>';
> }
> echo '</SELECT>';
> echo '</FORM>';
> //echo '<script language=Javascript>';
> //echo 'function getPK() {';
> //echo
>
"document.author.author_pk.options[document.author.author_pk.selectedIndex].
value
> ";
> //echo '}';
> //echo "</script>";

I really just know the basics of javascript, but:

--------------------------------------------------------
<script type="text/javascript">
function getPK(arg){
alert(arg);
}
</script>
<form name="testform">
<select name="author_pk" size="20"
onchange="getPK(this.options[this.selectedIndex].value)">
<option value="first">foo</option>
<option value="another">bar</option>
<option value="last">baz</option>
</select>
</form>
--------------------------------------------------------

or alternatively:
--------------------------------------------------------
<script type="text/javascript">
function getPK(){

alert(document.testform.author_pk.options[document.testform.author_pk.select
edIndex].value);
}
</script>
<form name="testform">
<select name="author_pk" size="20"
onchange="getPK(this.options[this.selectedIndex].value)">
<option value="first">foo</option>
<option value="another">bar</option>
<option value="last">baz</option>
</select>
</form>
--------------------------------------------------------

or, if you make & know an ID:
--------------------------------------------------------
<script type="text/javascript">
function getPK(){
selectbox = document.getElementById('author_pk');
alert(selectbox.options[selectbox.selectedIndex].value);
}
</script>
</head>
<body>
<form name="testform">
<select id="author_pk" name="author_pk" size="20" onchange="getPK()">
<option value="first">foo</option>
<option value="another">bar</option>
<option value="last">baz</option>
</select>
</form>
--------------------------------------------------------

That's how you can get the value.

Now, passing it back to PHP? That will require a trip to the server, and why
would you want to know it before the form is submitted? As it requires a
trip to the server, you could just as easily read the $_POST value.

If you want to do something on your page after selecting the something, you
either should have sent al the relevant info to the server in the HTML or in
js, or you'd have to make use of XMLHTTPRequests
(http://ajaxpatterns.org/XMLHttpRequest_Call).

That's going a bit to far in this group, I'd say: check the pointers, and if
things are unclear, ask comp.lang.javascript.

--
Rik Wasmus

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация