Jump to content
Larry Ullman's Book Forums

Recommended Posts

Within a javascript function, if certain conditions are true, I need to utilize MySQL for recalculating some fields, and then return right back into the javascript function. If I use location.href....... as a line in the function which refers to a php file that utilizes MySQL, I can get the calculating done, but I don't get returned back to the function. On the other hand, if go to php within my function and use include...... this will also get the calculating done, and I even then return to the function, but I don't want the php include... to run unless certain conditions are true within the javascript function.

 

So, I either need to use something like location.href that also returns me to my javascript function, or I need to be able to keep the php logic from running unless the function conditions are true. I suppose I could do the latter by passing a javascript true/false variable to the php logic, but I can't figure out how to do this, and not even sure if this would work. Within javascript, don't php lines run first and regardless of what is in the function?

 

I'm thinking the answer is probably in one of the 2 php/mysql books of yours I have, but I can't think of what it would be.

 

Help?? Thanks!

Share this post


Link to post
Share on other sites

shaneg, what you've just described is Ajax.

 

Ajax isn't a language so much as a server-side language (generally PHP) interacting with JavaScript, so that you can get information from a server/database on the fly and without having to reload the page.

 

Larry's Ajax book is really good, actually. I highly recommend having finished his PHP/MySQL book first though. And having a little JavaScript knowledge doesn't hurt either. Anyway, if that's you, I'd definitely recommend the Ajax book. It's short, simple, and to the point.

 

If you have a more immediate question that you'd like to have resolved, feel free to ask, and I (or perhaps someone else) would be more than happy to help you.

 

Edit: More specifically, if you wouldn't mind describing in a little more detail exactly what you want to do, we can try to help you.

  • Upvote 1

Share this post


Link to post
Share on other sites

I bought the Ajax book, and you're right, it is very good. However, I still have not been able to figure out my problem. I'll try to restate it.

Step 1: I am using javascript to display 2 grids that allow me to input variables into multiple columns of records in 2 tables of a MySQL database.

Step 2: I am using php to apply math to variables from table 1 against variables from table 2 in the database and generate a mathematical product which is now saved in another field of the record in table 1.

Step 3: I then use javascript to redisplay the grid showing table 1, but instead of the variables, I now see the columns of the table resulting from step 2.

 

I know how to do step 1, and step 2, and step 3. And I know how to click a button in step 1 that initiates step 3 (three, not two). What I can't figure out is how to make that button initiate step 2 before initiating step 3. In other words, I don't know how to temporarily leave the javascript function of step 1, then briefly go into php to initiate step 2, then automatically return to the javascript function of step 3.

 

Currently, I click on the button of step 1 and I do successfully go to step 3, however I haven't run the php in step 2, therefore, my product hasn't been calculated and step 3 shows outdated results if I have made changes in step 1.

 

Thanks for your help. You guys are great!

Share this post


Link to post
Share on other sites

My first question would be what's stopping you building the whole thing in php without any JavaScript? Would be a lot cleaner.

  • Upvote 1

Share this post


Link to post
Share on other sites

Okay, I think I get what you're trying to say, but just for clarification:

 

1. When you say "two grids", do you mean two text fields (in a form)?

2. My assumption is that you're grabbing a bunch of data from two different tables, and then outputting it to various text fields.

3. From there, the user can perhaps edit the data, and upon pushing a button, can see the result of calculations performed on that data, right?

 

Well, if that's the gist of it, you probably don't need PHP at all beyond the initial grab for the data, for which you don't need Ajax. I imagine most (if not all) mathematical operators that are possible in PHP are also possible in JavaScript. As such, you could set up an event in JavaScript to fire when the form is submitted/when a button is pressed, which will then grab the various values from the input fields (in the form), perform calculations on them, and then output that resulting data to a location of your choice.

 

If that's what you want to do, I can give you more specifics regarding the code to do so.

 

Keep in mind that unless you're going to have to either reinsert/reacquire data from a database after the initial loading of the page, JavaScript alone should suffice, and you won't need PHP/Ajax.

 

Aside from accessing a database midstream, you also generally have to use Ajax to access text files/external websites, as JavaScript is not a very secure language by its very nature.

 

Anyway, please let me know if I'm on the right track, and I'll provide you with some starter code that should work for any situation. Thanks.

  • Upvote 1

Share this post


Link to post
Share on other sites

To Stuart,

 

You're 100% right, but as you well know, most people want to add that dynamic Web 2.0 feel to sites these days, so I think wanting to use Ajax is not unreasonable at all.

  • Upvote 1

Share this post


Link to post
Share on other sites

Okay, I think I get what you're trying to say, but just for clarification:

 

1. When you say "two grids", do you mean two text fields (in a form)?

I'm using a grid plugin. The plugin is excellent and enables me to do things I don't think I could figure out how to do in PHP alone.

There are actually 3 grids.

Grid 1 presents fields from table 1 in the database. These fields are edited by the user. The new data

is saved in table 1 when the user either exits the grid field or presses enter.

Grid 2 presents fields from table 2 in the database. These fields are edited by the user. The new data

is saved in table 2 when the user either exits the grid field or presses enter.

After editing the fields in Grid 1 and Grid 2, the user then clicks a button that causes the following

to happen:

- PHP multiplies fields from table 1 (grid 1) against fields from table 2 (grid 2) and saves the result

to other fields in table 1.

- Grid 3 presents the multiplied fields from table 1. Grid 3 is not editable.

I need the user to see the combination of Grid 1 and Grid 2 OR to see Grid 3, but not at the same time.

The reason I am saving the multiplied results to table 1 fields is so that I will eventually be able to run a

query listing table 1 rows, including both the user's inputs as well as the multiplied results.

2. My assumption is that you're grabbing a bunch of data from two different tables, and then outputting it to various text fields.

3. From there, the user can perhaps edit the data, and upon pushing a button, can see the result of calculations performed on that data, right?

 

Well, if that's the gist of it, you probably don't need PHP at all beyond the initial grab for the data, for which you don't need Ajax. PHP does the initial grab for data to be shown in Grid 1 and Grid 2. But then, after editing Grid 1 and Grid 2, PHP is again needed to go into MySQL and produce the new multiplied results in table 1, for later showing in the non-editable Grid 3. I imagine most (if not all) mathematical operators that are possible in PHP are also possible in JavaScript. As such, you could set up an event in JavaScript to fire when the form is submitted/when a button is pressed, which will then grab the various values from the input fields (in the form), perform calculations on them, and then output that resulting data to a location of your choice.

 

If that's what you want to do, I can give you more specifics regarding the code to do so.

 

Keep in mind that unless you're going to have to either reinsert/reacquire data from a database after the initial loading of the page, JavaScript alone should suffice, and you won't need PHP/Ajax. Again, PHP/Ajax is needed to calculate the multiplied fields and to then provide those fields for presentation in Grid 3.

 

I know there are other ways to do this (there always is, of course). But given the end result I am trying to obtain for the user, I am confident (although, trust me, I am very much listening to all your recommendations and allowing my thinking to be challenged) that the process as I have outlined is what I need to do. The only thing I don't know how to do is initiate the PHP. I can, of course, load a second page and run the PHP independently of the Javascript, but I want to run the PHP by clicking on a button in Javascript so that the user is able to:

- enter data in Grid 1.

- enter data in Grid 2.

- click the button.

- view the multiplied data in Grid 3.

 

Aside from accessing a database midstream, you also generally have to use Ajax to access text files/external websites, as JavaScript is not a very secure language by its very nature.

 

Anyway, please let me know if I'm on the right track, and I'll provide you with some starter code that should work for any situation. Thanks.

Thank you!

Share this post


Link to post
Share on other sites

Sorry to keep answering your posts with questions, but is the grid plug-in a PHP file or a JS file?

 

The reason I ask is because it sounds like your grid plug-in is already using Ajax. In fact, it would have to be if every time you click outside of a grid, the data is instantly re-uploaded to the database.

 

If that's the case, I imagine you could just edit the grid functions to perform the calculations you want.

 

Honestly though, if the values in grid 3 are just for the user to reference, and they're not stored in a database, then you do not need any Ajax beyond what the grid plug-in is doing for you. I think you have two options, really:

1) Use the pre-existing grid functions to perform the calculations and spit the info back out.

2) But probably more simple, attach a second function to the onblur event (or whatever JS event is triggering the database to be updated whenever the user clicks outside of a grid), and from that second function, perform your calculations in JS, and then further use JS to spit the values into grid 3.

 

I know you want to use Ajax here, but you don't seem to need it. In general, Ajax calls are expensive (resource-wise), and it's best to avoid them as much as possible.

 

If you're looking to practice Ajax for the sake of learning it, go through Larry's book first.

 

Anyway, perhaps I am missing something (in which case, please tell me), but I think the solution to your problem is pretty straightforward. Please let me know what you think, or if you have anymore questions.

  • Upvote 1

Share this post


Link to post
Share on other sites

Thank you! I think I have this resolved. And it seems to work great.

 

(Yes, the grid plugin is already running ajax.)

 

I gained knowledge from each of your replies and each time (after letting it sink in) added another piece to my understanding of how this can work. You have been thorough, and I really appreciate it!

Share this post


Link to post
Share on other sites

Cool. If you solved the problem, then I'm happy.

 

When you get the chance though, definitely check out Larry's Ajax book. It's very good, and gives you everything you need to know to use Ajax for just about anything (with a little outside-of-the-box thinking).

 

Actually, that's very much the order I went in: Larry's PHP/MySQL book, his Ajax book, and then O'Reilly's JavaScript book. (I would have gone with Larry's JS book, if he had one.)

 

And for the record, the O'Reilly's JS book is really good, and really thorough, but it takes some time and a lot of supplementary material to properly get through, I think.

  • Upvote 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...