Jim R Posted March 28, 2020 Author Share Posted March 28, 2020 2 minutes ago, Barand said: That query will insert 1 row. If it's inserting more than that then you are running that code more than once. Is it inside a loop that isn't shown? ha...so the query is good, but the placement isn't. It is inside the loop that's creating this table... I have a + image trying to replace the Submit button (working on that), and I have it put next to each player's name. My thinking after seeing the simple INSERT query produce 64 rows (one for each player), was creating the join with a_players on the insert so I could create one match. (The ID number showing was just my way of knowing it was being passed. Quote Link to comment Share on other sites More sharing options...
Jim R Posted March 28, 2020 Author Share Posted March 28, 2020 19 hours ago, kicken said: There's two parts to this, so solve one then the other rather than doing both at the same time. First problem is saving your data. Create a function that you can call with your data and it will save it to your database. Use parameters for the function rather than accessing $_POST directly. For example: function bookmark_add($userId, $itemId){ //run INSERT statement } While getting that working, just use a normal form that will submit the data and refresh the page, eg: <?php if ($_SERVER['REQUEST_METHOD']==='POST'){ //Call your function with the data bookmark_add($_POST['userId'], $_POST['itemId']); } ?> <form method="page.php" action=""> <p>ItemID: <input type="text" name="itemId"></p> <p>UserID: <input type="text" name="userId"></p> <p><button type="submit">Save</button></p> </form> Once you have your function working exactly how you want it, then move on to your "without reloading the page problem." To do this without reloading the page, you need to use javascript to submit the values in the background using AJAX/XHR. Grab something like jQuery and use it's .post method to make this super simple. Now you create a javascript function which will gather your form data and submit it to a PHP page which then calls your PHP function to save it. For example: function submitBookmark(){ var postData = { userId: $('#userId').val() , //.... }; $.post('page.php', postData) .then(/* Success handler */) .catch(/* Failure handler */) ; } Finally setup that function to be executed whenever the user clicks your + image. So the AJAX is in addition to the form and query? I've never dealt with if ($_SERVER['REQUEST_METHOD']==='POST') while having the form method being a file. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.