/    Sign up×
Community /Pin to ProfileBookmark

Updating SQL using JQuery JSON

I am updating a SQL league table based on match scores. I am trying to send the teams and current score to php for processing. Any help appreciated. This is what I have so far:

“`
document.getElementById(“Home1Goals”).innerText = 0;
document.getElementById(“Away1Goals”).innerText = 0;
$(document).on(“click”, “#Week1Game1Start”, function() {
$.ajax({
method: “POST”,
url: “Operations.php”,
data: {
“Week1Game1Home”: “Home1Goals”,
“Week1Game1Away”: “Away1Goals”
},
dataType: “json”,

success: function(data) {
var result = JSON.parse(data);
alert(parsedJSON.canApprove);
if (data.statusCode == 200) {
alert(‘success’);

}
}
});
});
“`

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@rpjd1authorNov 12.2021 — With some help I got the JQuery/JSON working to the extent the call is failing. I'm using wamp (localhost). As all the files are in the same folder, is the name of the php file enough for the url?
Copy linkTweet thisAlerts:
@SempervivumNov 12.2021 — Yes, when the php file resides in the same folder you need not specify the folder.

What does "the call is failing" mean? Do you get a HTTP error like 404 or 500?

Open the developer tools in your browser and the network tab. You will find useful info there.
Copy linkTweet thisAlerts:
@rpjd1authorNov 14.2021 — I don't know if this means anything or not. I'm using VS Code as my editor, When I press "in" for input, VS Code displays the list of inputs. It should do the same when I press "n", it should display option starting with n, like name, but its not, I have to type it manually. It might be a completely separate issue. Anyway $_POST is not reading/recognizing name, so its giving undefined index for name, so array is null.
Copy linkTweet thisAlerts:
@rpjd1authorNov 14.2021 — I figured out (I think) why $__Post is empty. As mentioned, I'm using VS Code as editor, and it uses IntelliSense for predictive text I was wondering why it was displaying options when typing "i", but not for "n". On further investigation, I find out that name can only be applied to HTML elements, not table cells. Two of the four values I am want to submit to sql are in table cells with no input elements, which is I think is why I'm getting undefined index.
Copy linkTweet thisAlerts:
@rpjd1authorNov 14.2021 — OK, POST only accepts values from input elements, not table cells, I thought as long as it had a name attribute POST would read it. I found this on stack-overflow:

How can I get the value of a cell in the table to pass to a variable in PHP?

<table>

&lt;tr data-hostname-value="Hostname value 1" onclick="saveHostname()"&gt;<br/>
&lt;td&gt;Hostname value 1&lt;/td&gt;<br/>
&lt;/tr&gt;

&lt;tr data-hostname-value="Hostname value 2" onclick="saveHostname()"&gt;<br/>
&lt;td&gt;Hostname value 2&lt;/td&gt;<br/>
&lt;/tr&gt;


&lt;/table&gt;

&lt;script&gt;<br/>
function saveHostname() {

$('tr').on('click', function() {<br/>
var hostnameVal = $(this).attr('data-hostname-value');

<i> </i><CODE>$.ajax({
<i> </i> method: "POST",
<i> </i> data: "&amp;hostname=" + hostnameVal,
<i> </i> url: PHP_URL,
<i> </i> success: function(data) {
<i> </i> // success JS code.
<i> </i> }

<i> </i>});</CODE>
})

}


If this data-hostname-value was a td attribute, that could work.
×

Success!

Help @rpjd1 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...