7 replies
The website I'm designing requires a user to pick up a manufactuer first. and then to pick a product from a table of products retrieved from the database. I've already had a table of manufactuers written in HTML. An Ajax request is made to get product information back from the server.
Now I need another table or form of products for the 2nd level operation. Do you think I can use PHP to create dynamically a form or table with input types and onclick target specified etc. for product selection? If not, what options are availble to me?


Warren
#created #forms #php
  • Profile picture of the author mpruben
    Yes, you can do this easily in PHP. You can pull your products list from the database and build the form dynamically. It will look like plain HTML to the user.
    {{ DiscussionBoard.errors[1199705].message }}
  • Profile picture of the author TristanPerry
    Yep, this would be easy to do with PHP or any other server-side technology
    Signature
    Plagiarism Guard - Protect Against Content Theft
    {{ DiscussionBoard.errors[1199793].message }}
  • Profile picture of the author mywebwork
    Hi Warren

    Although you could do this in PHP you might be better off using JavaScript instead.

    In your description you said you are exchanging data using AJAX, which implies you are using asynchronous calls and calling a JavaScript function once the data is ready. Why not use that JavaScript function to modify the DOM and create the desired form or table?

    If you use PHP to do the job you'll end up redrawing the screen, which defeats the advantage of using AJAX. Of course you still use PHP on the server side to provide the data to the JavaScript function.

    Bill
    {{ DiscussionBoard.errors[1200713].message }}
  • Profile picture of the author WarrenW
    Bil

    Interresting you say that. I finished the first part of the code and end up with a table of buttons created by a PHP script. But clicking these buttons just gets an error saying "error on page". I suspect the onclick specification generated by PHP probably doesn't quite work and I'm trying to figure out a way out. Yes, I'd like to try out using Jagascript. Can you give a little more hint? I just did my first java script 3 days ago.


    Warren
    {{ DiscussionBoard.errors[1202477].message }}
  • Profile picture of the author mywebwork
    Hi Warren

    It would be helpful if I knew a bit more about the code you are using. As you said you employed AJAX that would mean that you have either written some JavaScript functions yourself or that you are using a code library like JQuery, MooTools or YUI.

    I assume that your existing code uses JavaScript to send data to a PHP page? And that PHP page is used to run the SQL query to get the requested information? If that's the case then you must have defined a "callback" function which the results will be submitted to. It's that "callback" function that you need to use to display your data.

    A couple of troubleshooting hints:

    1 - You can temporarily use a JavaScript "alert" function to display the data you are sending to the PHP page.

    2 - You can use an echo statement in the PHP page to see whet it is returning to your JavaScript.

    3 - If you need to "peek" into JavaScript objects the "toSource" method is very useful.

    4 - Firefox with the FireBug plugin is an essential troubleshooting tool, especially when working with the DOM.

    Not sure how you are communication your AJAX information. If you have a choice I'd recommend JSON over XML, it's much easier to read and integrates beautifully with JavaScript.

    Let me know if you need any assistance, AJAX is wonderful stuff but it can be a bit overwhelming if you're just learning JavaScript.

    Bill
    {{ DiscussionBoard.errors[1205585].message }}
  • Profile picture of the author WarrenW
    Bill,

    My website has 2 levels of operations. First, a user picks a manufacturer from a table of buttons that are HTML codeded. With the manufacturer known, a javascript function is called to request a PHP script (callback function?) to retrieve products information from the server. The PHP scripts filling up a table dynamically with product information. Each product is given a button inside a table cell by the PHP script. I'm running into troubles with this statement:

    echo '<td><button onclick="GetInfo(XXXXXX)">'.$model.'<button></td>';

    XXXXXX should be the model number $model. The next level PHP script just echo the string it receives, nothing else. The procedure is considered workng if the PHP scripts show the correct string.

    What should I put down for XXXXXX? The results I've got are

    XXXXXX = null value PHP says 'id undefined'
    xxxxxxx = /"$model/" nothing happens
    xxxxxxx = /'$model/' syntax error
    xxxxxxx = '$model' syntax error
    xxxxxxx = "$model" no display
    xxxxxxx = $model no display

    Don't have much with the other statement neither:

    echo '<td><button onclick="GetInfo(' . XXXXXX . ')">'.$model.'<button></td>';

    xxxxxx = $model syntax error
    xxxxxx = /'$model/' division by zero warning
    xxxxxx = /"$model/" syntax error
    xxxxxx = '$model' error on page
    xxxxxx = "$model" nothing

    I think I'm using XML not JSON.


    Warren
    {{ DiscussionBoard.errors[1206051].message }}
  • Profile picture of the author mywebwork
    Hi Warren

    A couple of observations:

    1 - You are using the "button" tag, which unfortunately is interpreted differently by different web browsers. You may want to use an "input" tag instead. In the event you want to continue to use "button" you may want to read this: HTML button tag

    2 - I think the syntax you are looking for is this (note the closing tag on the button), however it is lacking the "type" attribute for the button. Again see the article I referenced in item 1.

    echo '<td><button onclick="GetInfo('.$model.')">'.$model.'</button></td>';

    So far this doesn't look like AJAX, at least not from your description. Perhaps I'm missing something, but it sounds like you simply are using JavaScript to submit a selection value to a PHP page which then uses the selection to redraw a page?

    If PHP redraws the page then it is not AJAX - in AJAX the page does not get redrawn but instead JavaScript is used to dynamically modify the page by altering the DOM (Document Object Model). The result is a smooth transition, much more like a desktop application would respond.

    A sure sign that AJAX is employed is to examine your JavaScript and see if it using an XMLHttpRequest - this is the transport method that allows your page to communicate with another page or data source asynchronously.

    The "callback function" is a JavaScript function that "listens" for responses from the server. When it gets a response it takes action, in your case it would modify the values within your table. You specify the callback function in the "onreadystatechange" property of the XMLHttpRequest .

    JSON and XML are simply two popular methods of exchanging data between the JavaScript on the page and the PHP page that it calls (note that it doesn't have to be PHP - you could also use ASP or other languages). Both JSON & XML work equally well, JSON has an advantage in that it is native to both JavaScript and PHP (since version 5). I also find it a bit easier to read, even though I have been pretty familiar with XML for over a decade.

    Hope this long-winded reply helps to enlighten rather than confuse you! Feel free to PM me if you need some help with this.

    Bill
    {{ DiscussionBoard.errors[1209618].message }}

Trending Topics