Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 25th, 2005, 11:05 AM
Registered User
 
Join Date: Jan 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default PHP and Javascript

Hi,
I have an aplication that is suposed to show a text field to the user, based on that text field (supose its an ID), then I need to generate dynamic information based on that ID.
As an example, suposse you have a database that contains information about people (ID, name, telephone number, etc...), then, I have a page that contains a text field where the user inputs an ID (with a button "refresh") and a bunch of read only text fields where the person info (tel, name, so on...) is supossed to go; and the page automatically refreshes the information on the read only text fields once the ID is written and s submit button is pressed; all of this is supossed to occur in the same page, that is, both the ID text field that expects user input and the rest of the text fields (read only).

Now, if you understand the problem clearly, the question is, how do I call a php script from javascript, so that when the user enters the ID, somehow javascript calls php and php returns the values of that person in an array or something like that

Thanks beforehand, help really appreciated

 
Old January 25th, 2005, 12:16 PM
Friend of Wrox
 
Join Date: May 2003
Posts: 202
Thanks: 0
Thanked 1 Time in 1 Post
Default

You shouldn't need to use Javascript at all.

Just write the PHP script to be able to handle a form POST. It retrieves the ID from the submitted form, reads the data from the database and displays it.

Code:
<form method="POST" action="yourscript.php">
    <input type="text" name="ID">
    <input type="submit">
</form>
Bruce Luckcuck
Director, Applications & Support Services
Wiley Publishing, Inc.
 
Old January 25th, 2005, 12:33 PM
Registered User
 
Join Date: Jan 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No, What i meant was that I had to update the data in the *already* existent form fields in the page, not just to call a php script and then make it create an html page, I just need it to retrieve the values from the DB and then pass em to javascript with something like an array *somehow* (aka I dont know how)

 
Old January 25th, 2005, 12:43 PM
Friend of Wrox
 
Join Date: May 2003
Posts: 202
Thanks: 0
Thanked 1 Time in 1 Post
Default

Stop for a moment and change your approach.

Why can't the PHP script process the users input of the ID (from the form post), read the data from the database, and output a page that displays the information? When the user enters a new ID and clicks "submit" again, the process repeats.

In other words, there's nothing about the page that is static. The form is not persistent. It is built by the script to display the contents of the database.

There is no reason (or practical way) to use client-side Javascript to retrieve data from a database.

Bruce Luckcuck
Director, Applications & Support Services
Wiley Publishing, Inc.
 
Old January 25th, 2005, 12:54 PM
Registered User
 
Join Date: Jan 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by bluckcuck
 Stop for a moment and change your approach.

Why can't the PHP script process the users input of the ID (from the form post), read the data from the database, and output a page that displays the information? When the user enters a new ID and clicks "submit" again, the process repeats.

In other words, there's nothing about the page that is static. The form is not persistent. It is built by the script to display the contents of the database.

There is no reason (or practical way) to use client-side Javascript to retrieve data from a database.

Bruce Luckcuck
Director, Applications & Support Services
Wiley Publishing, Inc.
Yeah, that is the right approach, to make the user input the ID, then show another page, and even manage sessions, but unfortunately, Im at work, and they said to me "do it that way", since Im new, I cant just go telling em how stupid their approach is....so, thats where my question was born

 
Old May 6th, 2005, 05:33 AM
Registered User
 
Join Date: May 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If you really have to send the data via Javascript to a PHP script then you can do it the following way:

Use the JavaScript to bake a cookie with the required data and then retrieve with PHP on a different page.

Append the variable to the URL with JavaScript and redirect to the PHP script.

Have JavaScript set the value of a hidden field in the form and then submit the form to a PHP script.

However, the best way is to ignore JavaScript altogether as stated above and use $_SERVER['PHP_SELF'] when the form is posted.

Clive





Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript with php kuehhc Beginning PHP 8 February 1st, 2005 10:59 PM
How to use javascript with php? raichand_ray PHP How-To 2 November 12th, 2004 06:41 AM
javascript and php? Ashleek007 Beginning PHP 21 September 23rd, 2004 01:40 PM
PHP with Javascript Droobles Pro PHP 1 February 17th, 2004 03:08 PM
JavaScript and php Bandy PHP How-To 2 February 10th, 2004 04:32 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.