473,466 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

beginner AJAX

14 New Member
hi, I am a beginner with AJAX, i have an assignment where I have to pass a number to my php script and have it sent back converted into CELCIUS OR FARENHEIHT(spelled wrong). The problem is I dont know how to pass multiple arguments to my php script...

Here is my stuff below if you can look at it and someone could help me i would appreciate it...

HTML

[HTML]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Ajax C-F conversion</title>

<style>
.NoNum {color:red; font-weight:bold;}

</style>


<script>[/html]
Expand|Select|Wrap|Line Numbers
  1. var http = createRequestObject();
  2.  
  3. function createRequestObject() {
  4.     var ro;
  5.     var browser = navigator.appName;
  6.     if(browser == "Microsoft Internet Explorer"){
  7.         ro = new ActiveXObject("Microsoft.XMLHTTP");
  8.     }else{
  9.         ro = new XMLHttpRequest();
  10.     }
  11.     return ro;
  12. }
  13.  
  14. function convertTemp(argTemp) {
  15.  
  16.     http.open('get', 'convert.php?temp='+argTemp);
  17.     http.onreadystatechange = handleResponse;
  18.     http.send(null);
  19.  
  20. }
  21.  
  22. function handleResponse() {
  23.  
  24.     if(http.readyState == 4){
  25.  
  26.         document.getElementById("results").innerHTML = http.responseText;
  27.     }
  28. }
[html]</script>
</head>

<body>

<div>
<form name="myForm" action="#">


<h1>Enter Degrees Here:</h1>

<input type="text" name="temp">

<select name="type" id="type">
<option value="CF">Centigrade to Farenheit</option>
<option value="FC">Farenheit to Centigrade</option>
</select>

<input type="button" value="Submit" name="btnSubmit" onclick="convertTemp(this.value);"/>

</form>
</div>

<h1>Degrees Fahrenheit</h1>
<div id="results">

</div>
</body>
</html>[/html]


PHP SCRIPT

[PHP]<?php

$type=$_GET["type"];

$temp=$_GET["temp"];

if ($type=="CF"){
$convert=($temp*9/5)+32;

print ($convert);
}

if ($type=="FC"){
$convert=($temp-32)*(5/9);
print ($convert);
}


?>[/PHP]
Apr 2 '08 #1
12 1618
hkma08
11 New Member
Basically it is not that hard. You just need to add one more arg to your function, then continue your php link with &type= something, like this:

Expand|Select|Wrap|Line Numbers
  1. function convertTemp(argTemp,argType) {
  2.  
  3. http.open('get', 'convert.php?ctype='+argType+'&temp='+argTemp);
  4. http.onreadystatechange = handleResponse;
  5. http.send(null);
  6.  
  7. }
  8.  
The more serious problem is logical. You should get the values of the input and select dropdown, not the button itself, so 'this.value' never works. Instead you should get those values and pass to your onclick function:

Expand|Select|Wrap|Line Numbers
  1. <input type="button" value="Submit" name="btnSubmit" onclick="javascript:var temp=document.forms['myForm']['temp'].value;javascript:var ctype=document.forms['myForm']['type'].value;convertTemp(temp,ctype);"/>
  2.  
After these, you should be able to convert the numbers. And also, php doesn't use print ( except for arrays), instead it uses 'echo $convert;'.

Got it?
Apr 2 '08 #2
Justn226
14 New Member
Basically it is not that hard. You just need to add one more arg to your function, then continue your php link with &type= something, like this:

Expand|Select|Wrap|Line Numbers
  1. function convertTemp(argTemp,argType) {
  2.  
  3. http.open('get', 'convert.php?ctype='+argType+'&temp='+argTemp);
  4. http.onreadystatechange = handleResponse;
  5. http.send(null);
  6.  
  7. }
  8.  
The more serious problem is logical. You should get the values of the input and select dropdown, not the button itself, so 'this.value' never works. Instead you should get those values and pass to your onclick function:

Expand|Select|Wrap|Line Numbers
  1. <input type="button" value="Submit" name="btnSubmit" onclick="javascript:var temp=document.forms['myForm']['temp'].value;javascript:var ctype=document.forms['myForm']['type'].value;convertTemp(temp,ctype);"/>
  2.  
After these, you should be able to convert the numbers. And also, php doesn't use print ( except for arrays), instead it uses 'echo $convert;'.

Got it?
Thats not working tho...
Apr 2 '08 #3
acoder
16,027 Recognized Expert Moderator MVP
ctype should be type unless you change the PHP code.

What's not working? Do you get errors?

Note: there's no need for the "javascript:" protocol in event handlers.
Apr 3 '08 #4
hkma08
11 New Member
Thats not working tho...
well... its working for me.. take a look in my testing page.
http://www.kenportfolio.com/testing/testing.php
can you post ur new code here to see what's wrong?

and question to acoder: when should i use 'javascript:' pre-fix for those inline js?
sometimes it works but sometimes not w/o this pre-fix. What is it for?
Apr 3 '08 #5
acoder
16,027 Recognized Expert Moderator MVP
and question to acoder: when should i use 'javascript:' pre-fix for those inline js?
sometimes it works but sometimes not w/o this pre-fix. What is it for?
Mostly, it isn't needed. Can you give an example when it doesn't work without?
Apr 3 '08 #6
hsriat
1,654 Recognized Expert Top Contributor
@Justn226
Something out of the flow, but do you really need to employ Ajax and PHP?

If that's not a requirement, then do your calculations in JavaScript only and print the result.[html]Fahrenheit<input id="f" type="text" onkeyup="document.getElementById('c').value=(this. value - 32) * 5 / 9"><br>
Celsius<input id="c" type="text" onkeyup="document.getElementById('f').value=this.v alue * 9 / 5 + 32">[/html]


Harpreet
Apr 3 '08 #7
acoder
16,027 Recognized Expert Moderator MVP
that example i put on there, it doesnt do anything it just sits there, doesnt process doesnt do anything...
Please read Do NOT PM questions to individual Experts, Moderators or Administrators. Thanks!

You will often find errors by checking the error console of a decent browser.
Apr 3 '08 #8
Justn226
14 New Member
I dont know why it doesnt work, i took your code and compared them side by side then I just copied them over into IIS and tried it but they dont work...
Apr 3 '08 #9
Justn226
14 New Member
yeah this is a school assignment that uses strictly ajax and php, if it was a javascript assignment it woulda been done 2 days ago, unfortunately for me the professor wont help me out...
Apr 3 '08 #10
hsriat
1,654 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1.  
  2. //-------------------LINE 14----------------------------
  3.  
  4. function convertTemp(argTemp, argType) {
  5.  
  6. //--------------------LINE 16---------------------------
  7.  
  8. http.open('get', 'convert.php?temp='+argTemp+'&type='+argType);
  9.  
  10. //---------------------LINE 19-------------------------
  11.  
  12. <input type="button" value="Submit" name="btnSubmit" onclick="convertTemp(document.forms['myForm'].temp.value, document.forms['myForm'].type.value);"/>
Do these changes and try again.

And as this was a part of your assignment (thus I was not supposed to help you), I strongly recommend you to understand why I made these changes.

1. When you say this inside an HTML tag, it means you are referring to that particular tag. So on line 19, this.value didn't mean the value of the form fields, but the value of the submit button (ie. Submit).

2. Inside your PHP, you are using two GET parameters, but you passed only one in your JavaScript (line 16).

3. For second parameter, you had to give two arguments to the function. (line 14)

4. The parameters in the url (line 16) need to be same as the ones which you retrieve by GET method. That's why its $_GET['temp'] and $_GET['type'] and not $_GET['ctype']


Regards,
Harpreet
Apr 3 '08 #11
Justn226
14 New Member
THank you, I do understand the changes you have made...

I didnt think it was a good idea to go to a forum but I was seriously out of options, but I just learned what AJAX was and couldnt get help anywhere else, so i thank you greatly....

Justin Gayheart
Apr 3 '08 #12
hsriat
1,654 Recognized Expert Top Contributor
You are welcome :)

Harpreet
Apr 3 '08 #13

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

Similar topics

5
by: darrel | last post by:
I've been playing with prototype.js and scriptaculous to create some nice drag-and-drop interaction on my applications GUI. That's working well. Now I want to take the next step and start passing...
9
by: =?Utf-8?B?SGFyZHkgV2FuZw==?= | last post by:
Hi all, I followed first walk through sample from http://ajax.asp.net/docs/tutorials/IntroductionUpdatePanel.aspx to create my first testing page, The problem is after I clicked that botton, it...
2
by: syam | last post by:
Hello Friends, I am a new to Ajax. Please send me some good URL's to sart with Ajax sample application... Thanks...
7
dmjpro
by: dmjpro | last post by:
i m new in AJAX ... i can't run a simple AJAX program .... plz send me some some sample code with browser-compatibility capability .... thanx
1
by: 47computers | last post by:
I have a couple of DropDownLists in a DetailsView control (your classic country/state lists) and I need one to be populated based on the current selection of the other. Currently this works fine...
15
by: JohnDriver | last post by:
Hello I am learning Ajax and I am following the tutorials found but for some reason, my code is not working. I think I am missing something in the code that I am posting below. Please have a look...
3
by: asmon | last post by:
I'm a PHP programmer but i'm pretty messed up at this moment with the following script. I have a page with a 'TEXTAREA' and a 'PLAY' text. when i click PLAY, values are being displayed in the...
2
by: malcster2 | last post by:
hello, i am a beginner to ajax. i have created a mysql database, which i would like to access from a web page. i have created 3 files, a html to display the data, a php file to extract the data,...
2
by: roanhn | last post by:
Hello. I've to to write a master's thesis. Currently I deal with php, mysql, ajax. Fate decreed that I've to choose one of this subjects: 1.gdi+ library in .net technology 2.ado.net technology...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.