Jump to content

Archived

This topic is now archived and is closed to further replies.

barbs75

calling php and javascript in body onload tag?

Recommended Posts

Hey guys,

 

Been trying to sort this out for a couple of days now, with no prevail.

 

What i am trying to do is call a php while loop within my body onload tag, which then calls javascript function within that while loop.

 

But i cannot get it to work at all!!

 

here's what i have so far, but it is just not working:

 

<body onload="create_fields('1','bedrooms');<?php
while($bedroom_data = mysql_fetch_array($query_bed)){
echo "autofill(".$bedroom_data['room_width'].",".$bedroom_data['room_length'].",'".$bedroom_data['description']."');";
}?>
">

 

is what i am trying to here even possible? is the problem with calling the javascript functions within the php??

 

I have put an alert line in my functions to see if they are being called, and maybe a problem with the javascript, but its not that because the alerts aren't showing..

 

If anyone can help that would be great!!

 

cheers

 

Craig

Share this post


Link to post
Share on other sites

Try with a manual event handler such as a link click: onClick=

If that works I am guessing the JS event is called prior to the server side php so wont work with onLoad. Never attempted this method.

Share this post


Link to post
Share on other sites

hey Neil,

 

Yeah i have tried with an onClick event, and it works fine. But what i need it to so is run these commands auto when the page is loaded!

 

Or is there another event handler which uses a pageload event?

 

cheers

 

Craig

Share this post


Link to post
Share on other sites

Ummm. PHP code is executed on the server when the page is requested. When a page reaches a browser, where any javascript is executed, there is no php code present (do a "view source" in your browser to confirm this fact.)

 

To get javascript in a browser to interact with php on a web server without causing a page reload would generally require you to use AJAX. If you don't mind the page reloading, you would use a links or a form to send data to php on the web server.

Share this post


Link to post
Share on other sites

ok, so AJAX....don't suppose you could tell me what part of AJAX i would need to look into? or even a URL to a page which has the information on that i would need to research and look into??

 

I haven't really used AJAX much before, practicaly have 0% experience with AJAX.

 

cheers

 

Craig

Share this post


Link to post
Share on other sites

Hey PFMaBiSmAd!

 

Ok, so i read up on AJAX, and did the tutorial where you have to create a suggestion box, which is cool! and i definitely want to use AJAX in the future, for form error checking? rather than going to a php page where the form is validated and returned to form page with errors stored in sessions....AJAX seems like a really useful tool..

 

However, i dont understand how i would use it in the case i need it to work?? don't suppose you can help me?

 

What i need to do is in a while loop, go through each room in my mysql fetch array and then for each, generate a room, and then put the data from my mysql into it....

 

So i have a javascript page which has the function 'GetXmlHttpObject' to create the xmlHttp object.

Then i would another method which preforms the create_fields() function and then the autofill() function to put the data in.

But this needs to be done within a php while loop, how would i get the javascript to interact with this while loop? do i need to put the while loop in a variable and then return the variable into a javascript variable?

 

I'm really confused here! any help would be great!!

 

thanks

 

Craig

Share this post


Link to post
Share on other sites

I re-read your first post and read your last post and I cannot determine what you are trying to do, but the purpose of php is to output content (HTML/Javascript/CSS) to the browser. I think you are trying to use javascript to build that content in the browser instead of letting php build it on the server.

 

Can you describe what it is you are trying to accomplish.

Share this post


Link to post
Share on other sites

Ok,

 

What i have is a page called 'house_upload2.php'. Which is a page which the stage 2 of a 3 stage property upload system. Where a user uploads details and then uploads images to my database using mysql.

 

What i am trying to do is create an edit property function where the user can go through all the steps again and change the property details. So what i want to do is use the same pages for my upload facility, but i have sent a variable along the url for the pages, where i use PHP to capture that variable using 'GET'. the variable is called function, and if they click to edit a property, the url would be as follows:

 

'propertyManager/house_upload2.php?function=edit'

 

So i have if statements on my pages where if the $function variable is equal to edit, then it collects the property data from my database, so when the page loads, the input fields display the data of that property that they are editing!!

 

Does that make sense??! i hope so!

 

So on house_upload2.php, i had a function using javascript which when a user selects how many bedrooms is in their property from a drop down box, it generates the input fields and description box for that many rooms.

 

So what i want to do, is if they are EDITING the property, i want to pull the number of bedrooms the property has, and then output the input boxes etc as before, but then put all the data for these rooms in those fields by taking the data from my database....

 

Obviously i tried to do this from when the page loads, i want it to do it automatically when the page is opened.

But i need to somehow loop through each bedrooms data and put it in the right boxes..

 

Does that clear up what i'm trying to do??

 

Is this a case for AJAX? or am i missing something here? do i need basic php? i tried looking for examples of what i'm trying to do but couldn't find anything.....

 

if you can provide some help that would be excellent!

 

cheers

 

Craig

Share this post


Link to post
Share on other sites

Hey guys,

 

Can anyone help me out with this problem??

 

really confused with how to use AJAX to call a php while loop and then call javascript functions within that....

 

cheers

 

Craig

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.