Helpful Information
 
 
Category: Ajax and Design
php js ajax: 2 variable with ajax

hey all

i got a small problem.. i made a page with php, js and ajax. that works great but now i want to add a 2de variable to the page so that i can do more think

i've got now a pulldownlist where i can select a week. and i want to add 3 radio button to it so i can chose a week and then change between the radiobuttons

is the code i use for the js (with 1 var)


function showUser(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="includes/realisatie.incl.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

and the code of the radiobuttons

<form action="index.php?pagina=includes/opties.realisatiemaken" method="get">
<input type="hidden" name="pagina" value="includes/opties.realisatiemaken">
<INPUT TYPE="button" onClick="showUser(this.name)" name="<?php echo $weekid; ?>" value="Update">
<input type="submit" value="Realisatie vernieuwen">

<input type="radio" name="type" value="1" > Realisatie
<input type="radio" name="type" value="2" > Verschil
<input type="radio" name="type" value="3" CHECKED> Alles (werkt nog niet)

i tryed to fix it by mine own but i cant get 2 variable into the ajax

does anybody of u got an solution??

when u want more info just ask.. i hope u get the problem

Traxion

You just add another query string parameter.

url=url+"&foo="+ bar

Eric

You just add another query string parameter.

url=url+"&foo="+ bar

Eric

jea.. but how do i get that in html?

because i would like to do it with out any more buttons on the form

when i change a radiobutton now the var from the pulldownlist is gone and then the query isnt good anymore so my page is useless

I'm not sure I understand the question but if you add an id to the form

<form id="aform" action="index.php?pagina=includes/opties.realisatiemaken" method="get">

You can get the value of the radio button by



.
.
.
url=url+"?q="+str
for (var i = 0; i < document.aform.type.length; i++) {
if (document.aform.type[i].checked) {
url=url+"&type=" + document.aform.type[i].value;
}
}
url=url+"&sid="+Math.random()
.
.
.


Or something similar. I really haven't done that much work with radio buttons.

david_kw

Your using AJAX and forms, why not "post" the form to the AJAX page instead of "get"? Not sure on the max of "get" but when I use forms and AJAX, I always post.

Your using AJAX and forms, why not "post" the form to the AJAX page instead of "get"? Not sure on the max of "get" but when I use forms and AJAX, I always post.

what is the differnce.. if securaty doesn't matter:).. GET is easyer for debugging:)

thnx i will try the code

to bad it doesnt work

i edit the code again and now there is one button lese (that one didnt use the ajax script)



<form id="aform" action="index.php" method="get">
<input type="hidden" name="pagina" value="realisatie">
<input type="hidden" name="weekid" value="<?PHP echo $weekid; ?>">
<input type="radio" name="type" value="1" > Realisatie
<input type="radio" name="type" value="2" > Verschil
<input type="radio" name="type" value="3" > Alles (werkt nog niet)
<INPUT TYPE="submit" onClick="showUser(this.name)" name="<?php echo $weekid; ?>" value="Update">
</form>

i tryed stuff like in the radiobutton on onclick but then i only get the valua of the radiobutton

i still dont get how i get both variables into ajax

Exactly what are "both variables"? I see 3 named elements in the form. What does your showUser function look like now?

david_kw

what is the differnce.. if securaty doesn't matter:).. GET is easyer for debugging:)

thnx i will try the code

The difference? Besides if you have a long form and your GET url gets chopped... and post is more secure? Easier to debug? How? print_r($_REQUEST) does both.

Exactly what are "both variables"? I see 3 named elements in the form. What does your showUser function look like now?

david_kw

variable 1 weekid
variable 2 type

the var pagina i dont need for the ajax


function showUser(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="includes/realisatie.incl.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

it works right now with only weekid

now i want that i can pick a weekid.. and the page change to that week

and then i need to change the type so that the page change on somethings but the weekid must stay the same.

if type is emty it should be standaard on 3 (i can build that bym ine own)

i hope that u get the problem

already thnx
Traxion

variable 1 weekid
variable 2 type


So did you try the code in my earlier post? Here it is again.


function showUser(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="includes/realisatie.incl.php"
url=url+"?q="+str
for (var i = 0; i < document.aform.type.length; i++) {
if (document.aform.type[i].checked) {
url=url+"&type=" + document.aform.type[i].value;
}
}
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

david_kw

i tryed that but he doesnt seem to find the aform

document.aform has no properties
showUser("13")realisatie.ajax.j... (line 14)
onchange(change )index.php (line 1)
[Break on this error] for (var i = 0; i < document.aform.type.length; i++) {...


i tryed to get that out by a if statment..

if (document.aform.type.length !== ""){
for (var i = 0; i < document.aform.type; i++) {
if (document.aform.type[i].checked) {
url=url+"&type=" + document.aform.type[i].value;
}
}
}

i dont now i do that so correctly?
but he seems to ask for that "document.aform has no properties"


<form id="aform" method="get">
<br>
<input name="type" value="3" type="hidden">
<select name="weekid" onchange="showUser(this.value)">

<option value="">Maak uw keuze</option>
<option value="18">weeknr 13 2007 (2007-03-27)</option>
<option value="17">weeknr 12 2007 (2007-03-19)</option>
</select>
</form>

to make it all more implicated (and im sorry for posting this now, i should be doing this much ealier). this is the code from the 1ste form when i open and call the first time the ajax

the code i post in a post above is the form what i call when i calt this one already..

im srry that i didnt tell that earlyer

Hmm, try adding a name field as well. That should fix being able to find the form at least.

<form id="aform" name="aform" method="get">

david_kw

Umm, on this one the input named 'type' is not a radio button but rather a hidden field. If that is the case you don't have to do the loop at all but instead just

url=url+"&type=" + document.aform.type.value;

pretty much just like A1ien51 said in the first response.

david_kw

oke i will try that

:( my company just update the sql server from 3.xx to 5..

some querys dont work anymore.. and the page load is way to long.

i must try to fix that first.. then i will try the ajax again.. thnx for all the support from here

i got it working:)

with document.aform.type.value but unafraid i cant call the ajax from any ather way then that way.. we i must live with that

thnx for the help:)

Grats and welcome. :) I'm not total sure what you mean by "unafraid i cant call the ajax from any ather way then that way".

But just in case it is useful, you could also get the form with

alert(document.forms['aform'].type.value);
alert(document.getElementById("aform").type.value);

which would let you do send the name in as a parameter to a function.



function showUser(str, frm)
{
.
.
.
url=url+"&type=" + document.forms[frm].type.value;
.
.
.
}


david_kw

Grats and welcome. :) I'm not total sure what you mean by "unafraid i cant call the ajax from any ather way then that way".

But just in case it is useful, you could also get the form with

alert(document.forms['aform'].type.value);
alert(document.getElementById("aform").type.value);

which would let you do send the name in as a parameter to a function.

david_kw

i would also like to (im dreaming:P) call it also true $_GET or $_POST

but then i cant use a form or define it like a form

on anther page i use AJAX just on variable ( $var) and there i can also get the variable from $_GET so i can use both..

i dont now if u got fast a solution for that..?

Actually I'm not sure what you mean. $_GET and $_POST are in PHP which is serverside. If you want something dynamic in Ajax then I'd think the serverside variables would not have the values you need.

david_kw

$proj = $_GET['project'];
if( isset( $proj ) )
{
echo"<script type=\"text/javascript\">showUser('" . $proj . "');</script>";
}

did i make for another page and now can i call the ajax true a form and true index.php?pagina=projecten&project=OCV25-1 project=OCV25-1

so there i use $_GET.. and then call AJAX...

but now the javascript needs document.aform.type.value and i cant fake that true an form

im not very good with javascript so maybe its very easy and i dont see how i can do it..

if its hard to make just forget it my boss is already happy the way it is right now:)

So what value is actually in $_GET['project']?

Or what is the exact URL including the query?

I still don't see why the page can't be built without ajax if you are going to do it that way, but it should be able to be done. Is that value the name of the form?

david_kw

the value of $_GET['project'] is on project of mine company like OCV25-1 or ESH01 or thinks like that.

the url is than

index.php?pagina=projecten&project=OCV25-1

$pagina is the variable for the page i call and $project = the project is want to see

then i use the code above to call the ajax and see the page with ajax.

the problem is that i got (where u helpt me above here) the ajax ( or ecxtualy js) ask for a document.aform.type.value and document.aform.weekid.value witch i cant fake or create/call with out having to press or change somethings

i will try to explain it next week better.. but now i got some other thinks to do that have more priority

thnx again for the support:)










privacy (GDPR)