PHP/ jQuery - Pull data from database based on input

Status
Not open for further replies.

Kaz

BooYah
Staff member
Nov 16, 2010
3,064
1,025
Ok so jQuery isn't one of my strongest areas.

So far I have this -

What I intend to do is:
Input Box 1 = User enters a word
Input Box 2 = User enters amount (eg, 23 or 54 or 87)

So when the user enters a word, I am wanting to check a row in the database to see if that word is listed, if it is listed it gets the value for that word
(Row Structure = id, word, value)
Underneath I want it to say 'this word is listed and the value is *pulled value from the database*'

Then in box 2, the user enters an amount
Then underneath once again I am wanting the Users inputted value to be multiplied by the *words value from the database* which is found when entered in box 1
-- If the word is not listed (the part from box 1, the multiplication value will be 1 rather than the value found in the database)
(none of the text is displayed unless both boxes are filled in like in the example)

I have partially figured out the way to get it to work with 1 box, but not both

Any ideas how I could do this?

I hope it makes sense
 

Sysode

Front-End Developer
Dec 11, 2012
1,673
848
I'm of no use here but just to clarify something.. Are you wanting this all real time, no need to submi or enter o perform the search? If so its ajax searching you're asking for, isn't it?
 

Kaz

BooYah
Staff member
Nov 16, 2010
3,064
1,025
Yeh exactly that.
I managed to get it working for 1 input, but can't seem to get it to function with multiple inputs (like i have in the sample provided)
 

RastaLulz

fight teh power
Staff member
May 3, 2010
3,935
3,936
Do you have some sort of API to interface with the database so that you can get this information over a web request?

Also, could you explain what the amount and the value of the word are? I'm a bit confused.
 
Last edited:

Kaz

BooYah
Staff member
Nov 16, 2010
3,064
1,025
Do you have some sort of API to interface with the database so that you can get this information over a web request?
Also, could you explain what the amount and the value of the word are? I'm a bit confused.
Basically in a table I have (Row structure = id, word, value)
Example of the rows:
1, red, 5
2, blue, 10

So if a user enters red into box 1 and then 23 in box 2
Data to return would be = Red is in the database and its value is 5
Calculation underneath would then be = 5 (value of red)[blues value would be 10 if that was entered] * 23 (user input)

If a user entered the word 'yellow', there is no value for that in the table therefore its default value would be 1
Meaning the calculation would display the value of 1(default value) * 23 (user input)


Are you wanting something like this?
I have looked into something like that, however I can only seem for it to work with 1 of the inputs and not both simultaneously
 

RastaLulz

fight teh power
Staff member
May 3, 2010
3,935
3,936
Is this what you're looking for?

On a side note, I'd recommend using HTML5 standards, as oppose to XHTML. ;)
 

Kaz

BooYah
Staff member
Nov 16, 2010
3,064
1,025
Nope, the way I have it in the example where the text is only displayed when both fields are complete is how I wish for it to be.
The issue I am facing is how to link both the inputs to a php file to generate the data/info I posted in my previous reply where I explained what I needed and how it would work
 

RastaLulz

fight teh power
Staff member
May 3, 2010
3,935
3,936
Nope, the way I have it in the example where the text is only displayed when both fields are complete is how I wish for it to be.
The issue I am facing is how to link both the inputs to a php file to generate the data/info I posted in my previous reply where I explained what I needed and how it would work
So what you're trying to do is to get jQuery to request a page like the following?
Code:
http://api.example.com/info?word=<word>&amount=<amount>
Which will then return the required information?
 

Kaz

BooYah
Staff member
Nov 16, 2010
3,064
1,025
Excuse the code, just playing right now.

This is what I have tried -
I am trying to figure out how to post both of the input values to the following PHP file


Based on the data that the PHP file uses will determine the text which gets displayed under the text boxes -

Obviously the code posted is incomplete/fails to work, I am just trying to figure ways to get it to function before it gets cleaned up
 

Kaz

BooYah
Staff member
Nov 16, 2010
3,064
1,025
Yeh I already have that bit.
I just need to figure out how to link the inputs into the php file via jquery/ajax to determine the text which is shown underneath.

Like in the example it displays what is entered into box 1 and box 2.
However I need to check the database to see if that word is there to get its number value for that word, which will then be used to display the calculation also.
The text which then gets outputted is what is displayed in here - (much like the 'the word is XXX and its value is XXX' in the current example which was used in the first post)
 

RastaLulz

fight teh power
Staff member
May 3, 2010
3,935
3,936
This will get the information from the form, post it to your PHP file ("check.php"), and it will display the data that it gets.

If that's not what you want, well.. :confused:
 

Kaz

BooYah
Staff member
Nov 16, 2010
3,064
1,025
Boom that works, thanks.
Just a small issue.
When I change the word (when a word/value has been entered) it doesnt update the text (yet it does when the value is changed)


Also when I change the value the text below displays 3 lots of text of it rather than just the one, any idea why?
Like it should display
Code:
The word is hello and the amount is 34

Result: hello is a word andi ts value 3
3 * 34 = 102

Yet it shows (after a couple of value changes)
Code:
The word is hello and the amount is 34

Result: hello is a word andi ts value 3
3 * 34 = 102
Result: hello is a word andi ts value 3
3 * 34 = 102
Result: hello is a word andi ts value 3
3 * 34 = 102
Result: hello is a word andi ts value 3
3 * 34 = 102
 

RastaLulz

fight teh power
Staff member
May 3, 2010
3,935
3,936
This will fix the issue of you not being able to update the word after the first time:

As for why you're getting duplication's, I'm not entirely sure, unless you've changed it so that the result information is going in a different div.
 
  • Like
Reactions: Kaz

Kaz

BooYah
Staff member
Nov 16, 2010
3,064
1,025
This will fix the issue of you not being able to update the word after the first time:

As for why you're getting duplication's, I'm not entirely sure, unless you've changed it so that the result information is going in a different div.
Since adding the fix for updating the word, the duplication seems to have gone.
The duplication could of been caused by stacking the amount for 1 word and then getting the new value for the next word.
Cheers dude
 
Status
Not open for further replies.

Users who are viewing this thread

Top