Jump to content

Executing form and staying on the same page?


Jim R

Recommended Posts

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...

1892983394_ScreenShot2020-03-28at9_46_33AM.png.1ab89b5ea4c5ad8e594557b3638cc0aa.png

 

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.  

Link to comment
Share on other sites

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.  

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.