How to use AJAX POST request to trigger a series of PHP code? The Next CEO of Stack Overflowpost ajax data to PHP and return dataHow can I prevent SQL injection in PHP?How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?How to manage a redirect request after a jQuery Ajax callDetecting request type in PHP (GET, POST, PUT or DELETE)Abort Ajax requests using jQueryHow do I get PHP errors to display?How Do You Parse and Process HTML/XML in PHP?jQuery Ajax POST example with PHPHow do I send a POST request with PHP?How does PHP 'foreach' actually work?

Which one is the true statement?

Does Germany produce more waste than the US?

Is there a reasonable and studied concept of reduction between regular languages?

Can I board the first leg of the flight without having final country's visa?

Help/tips for a first time writer?

What is the difference between "hamstring tendon" and "common hamstring tendon"?

Is there a difference between "Fahrstuhl" and "Aufzug"?

Is it ever safe to open a suspicious HTML file (e.g. email attachment)?

Does higher Oxidation/ reduction potential translate to higher energy storage in battery?

Man transported from Alternate World into ours by a Neutrino Detector

Is it professional to write unrelated content in an almost-empty email?

Is a distribution that is normal, but highly skewed, considered Gaussian?

Spaces in which all closed sets are regular closed

It is correct to match light sources with the same color temperature?

Could a dragon use its wings to swim?

Graph of the history of databases

My ex-girlfriend uses my Apple ID to login to her iPad, do I have to give her my Apple ID password to reset it?

How to avoid supervisors with prejudiced views?

Does the Idaho Potato Commission associate potato skins with healthy eating?

From jafe to El-Guest

Is there such a thing as a proper verb, like a proper noun?

How to use ReplaceAll on an expression that contains a rule

What flight has the highest ratio of timezone difference to flight time?

A question about free fall, velocity, and the height of an object.



How to use AJAX POST request to trigger a series of PHP code?



The Next CEO of Stack Overflowpost ajax data to PHP and return dataHow can I prevent SQL injection in PHP?How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?How to manage a redirect request after a jQuery Ajax callDetecting request type in PHP (GET, POST, PUT or DELETE)Abort Ajax requests using jQueryHow do I get PHP errors to display?How Do You Parse and Process HTML/XML in PHP?jQuery Ajax POST example with PHPHow do I send a POST request with PHP?How does PHP 'foreach' actually work?










-1















So, right now I am trying to make it so that an AJAX post request can trigger this conditional statement:



if(isset($_POST['purse'])) 
if($_POST['purse'] == true) <---- Removed
if($nexttime <= time())
/* Some DB queries etc.. */
$output = array();
array_push($output, "<div class='alert alert-success'>You are successful!</div>");
echo(json_encode($output));
else
/* Other stuff... */


/* Echos work here */



And so far I have tried this:



 $(document).ready(function() 
$('#purse').click(function()
$.ajax(

type : "POST",
url: "petty.php",
data: purse: 'true',
dataType: 'json',
success: function(data)
alert(data);


);
);
);


But that doesn't seem to trigger it. This is all for an effort to make it so the page executes whatever it is supposed to, depending on which button the user clicks, without refreshing the page and I heard AJAX is the way to go for that, but I honestly can't figure it out and have tried tutorials but they don't do what I am trying to do, from what I have seen.



Form:



<form action="petty.php" method="post">
<div class="subheading mb-5 text-center"><br>Steal a few purses.
<button type="submit" name="purse" id="purse" class="btn btn-dark center-block">Submit</button>
<div class="subheading mb-5 text-center"><br>Forge checks.
<button type="submit" name="checks" id="checks" class="btn btn-dark center-block">Submit</button>
<div class="subheading mb-5 text-center"><br>Steal cars.
<button type="submit" name="cars" id="cars" class="btn btn-dark center-block">Submit</button>
</form>


EDIT: Well, my issue is that I am trying to push all messages, that the user will be able to see upon clicking a button, into an array and using echo(json_encode($output)) to push it all to AJAX so that it can be outputted to the browser, but it doesn't seem to be working.










share|improve this question
























  • Make the button type button instead of submit or the form will posts entire page..

    – bestinamir
    Mar 8 at 17:34











  • Making it a button instead of submit doesn't work, when I click it, nothing happens then.

    – user3148270
    Mar 8 at 17:40











  • You are sending key as 'pursel' in the data, but checking on key 'purse'. Also what is the value of purse in data: 'pursel' : purse?

    – Anurag Srivastava
    Mar 8 at 17:41











  • That's actually a good catch, I mean't 'purse' : purse. I was just experimenting with AJAX since I have never done them before. All I am trying to do with it is send that POST request so that the conditional sets off.

    – user3148270
    Mar 8 at 17:43











  • Remove the comma in “ type: POST,” line since it’s a last line, check for js errors and add console.log(123) to see if function gets called..

    – bestinamir
    Mar 8 at 17:52
















-1















So, right now I am trying to make it so that an AJAX post request can trigger this conditional statement:



if(isset($_POST['purse'])) 
if($_POST['purse'] == true) <---- Removed
if($nexttime <= time())
/* Some DB queries etc.. */
$output = array();
array_push($output, "<div class='alert alert-success'>You are successful!</div>");
echo(json_encode($output));
else
/* Other stuff... */


/* Echos work here */



And so far I have tried this:



 $(document).ready(function() 
$('#purse').click(function()
$.ajax(

type : "POST",
url: "petty.php",
data: purse: 'true',
dataType: 'json',
success: function(data)
alert(data);


);
);
);


But that doesn't seem to trigger it. This is all for an effort to make it so the page executes whatever it is supposed to, depending on which button the user clicks, without refreshing the page and I heard AJAX is the way to go for that, but I honestly can't figure it out and have tried tutorials but they don't do what I am trying to do, from what I have seen.



Form:



<form action="petty.php" method="post">
<div class="subheading mb-5 text-center"><br>Steal a few purses.
<button type="submit" name="purse" id="purse" class="btn btn-dark center-block">Submit</button>
<div class="subheading mb-5 text-center"><br>Forge checks.
<button type="submit" name="checks" id="checks" class="btn btn-dark center-block">Submit</button>
<div class="subheading mb-5 text-center"><br>Steal cars.
<button type="submit" name="cars" id="cars" class="btn btn-dark center-block">Submit</button>
</form>


EDIT: Well, my issue is that I am trying to push all messages, that the user will be able to see upon clicking a button, into an array and using echo(json_encode($output)) to push it all to AJAX so that it can be outputted to the browser, but it doesn't seem to be working.










share|improve this question
























  • Make the button type button instead of submit or the form will posts entire page..

    – bestinamir
    Mar 8 at 17:34











  • Making it a button instead of submit doesn't work, when I click it, nothing happens then.

    – user3148270
    Mar 8 at 17:40











  • You are sending key as 'pursel' in the data, but checking on key 'purse'. Also what is the value of purse in data: 'pursel' : purse?

    – Anurag Srivastava
    Mar 8 at 17:41











  • That's actually a good catch, I mean't 'purse' : purse. I was just experimenting with AJAX since I have never done them before. All I am trying to do with it is send that POST request so that the conditional sets off.

    – user3148270
    Mar 8 at 17:43











  • Remove the comma in “ type: POST,” line since it’s a last line, check for js errors and add console.log(123) to see if function gets called..

    – bestinamir
    Mar 8 at 17:52














-1












-1








-1








So, right now I am trying to make it so that an AJAX post request can trigger this conditional statement:



if(isset($_POST['purse'])) 
if($_POST['purse'] == true) <---- Removed
if($nexttime <= time())
/* Some DB queries etc.. */
$output = array();
array_push($output, "<div class='alert alert-success'>You are successful!</div>");
echo(json_encode($output));
else
/* Other stuff... */


/* Echos work here */



And so far I have tried this:



 $(document).ready(function() 
$('#purse').click(function()
$.ajax(

type : "POST",
url: "petty.php",
data: purse: 'true',
dataType: 'json',
success: function(data)
alert(data);


);
);
);


But that doesn't seem to trigger it. This is all for an effort to make it so the page executes whatever it is supposed to, depending on which button the user clicks, without refreshing the page and I heard AJAX is the way to go for that, but I honestly can't figure it out and have tried tutorials but they don't do what I am trying to do, from what I have seen.



Form:



<form action="petty.php" method="post">
<div class="subheading mb-5 text-center"><br>Steal a few purses.
<button type="submit" name="purse" id="purse" class="btn btn-dark center-block">Submit</button>
<div class="subheading mb-5 text-center"><br>Forge checks.
<button type="submit" name="checks" id="checks" class="btn btn-dark center-block">Submit</button>
<div class="subheading mb-5 text-center"><br>Steal cars.
<button type="submit" name="cars" id="cars" class="btn btn-dark center-block">Submit</button>
</form>


EDIT: Well, my issue is that I am trying to push all messages, that the user will be able to see upon clicking a button, into an array and using echo(json_encode($output)) to push it all to AJAX so that it can be outputted to the browser, but it doesn't seem to be working.










share|improve this question
















So, right now I am trying to make it so that an AJAX post request can trigger this conditional statement:



if(isset($_POST['purse'])) 
if($_POST['purse'] == true) <---- Removed
if($nexttime <= time())
/* Some DB queries etc.. */
$output = array();
array_push($output, "<div class='alert alert-success'>You are successful!</div>");
echo(json_encode($output));
else
/* Other stuff... */


/* Echos work here */



And so far I have tried this:



 $(document).ready(function() 
$('#purse').click(function()
$.ajax(

type : "POST",
url: "petty.php",
data: purse: 'true',
dataType: 'json',
success: function(data)
alert(data);


);
);
);


But that doesn't seem to trigger it. This is all for an effort to make it so the page executes whatever it is supposed to, depending on which button the user clicks, without refreshing the page and I heard AJAX is the way to go for that, but I honestly can't figure it out and have tried tutorials but they don't do what I am trying to do, from what I have seen.



Form:



<form action="petty.php" method="post">
<div class="subheading mb-5 text-center"><br>Steal a few purses.
<button type="submit" name="purse" id="purse" class="btn btn-dark center-block">Submit</button>
<div class="subheading mb-5 text-center"><br>Forge checks.
<button type="submit" name="checks" id="checks" class="btn btn-dark center-block">Submit</button>
<div class="subheading mb-5 text-center"><br>Steal cars.
<button type="submit" name="cars" id="cars" class="btn btn-dark center-block">Submit</button>
</form>


EDIT: Well, my issue is that I am trying to push all messages, that the user will be able to see upon clicking a button, into an array and using echo(json_encode($output)) to push it all to AJAX so that it can be outputted to the browser, but it doesn't seem to be working.







php ajax






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 21:01







user3148270

















asked Mar 8 at 17:28









user3148270user3148270

13




13












  • Make the button type button instead of submit or the form will posts entire page..

    – bestinamir
    Mar 8 at 17:34











  • Making it a button instead of submit doesn't work, when I click it, nothing happens then.

    – user3148270
    Mar 8 at 17:40











  • You are sending key as 'pursel' in the data, but checking on key 'purse'. Also what is the value of purse in data: 'pursel' : purse?

    – Anurag Srivastava
    Mar 8 at 17:41











  • That's actually a good catch, I mean't 'purse' : purse. I was just experimenting with AJAX since I have never done them before. All I am trying to do with it is send that POST request so that the conditional sets off.

    – user3148270
    Mar 8 at 17:43











  • Remove the comma in “ type: POST,” line since it’s a last line, check for js errors and add console.log(123) to see if function gets called..

    – bestinamir
    Mar 8 at 17:52


















  • Make the button type button instead of submit or the form will posts entire page..

    – bestinamir
    Mar 8 at 17:34











  • Making it a button instead of submit doesn't work, when I click it, nothing happens then.

    – user3148270
    Mar 8 at 17:40











  • You are sending key as 'pursel' in the data, but checking on key 'purse'. Also what is the value of purse in data: 'pursel' : purse?

    – Anurag Srivastava
    Mar 8 at 17:41











  • That's actually a good catch, I mean't 'purse' : purse. I was just experimenting with AJAX since I have never done them before. All I am trying to do with it is send that POST request so that the conditional sets off.

    – user3148270
    Mar 8 at 17:43











  • Remove the comma in “ type: POST,” line since it’s a last line, check for js errors and add console.log(123) to see if function gets called..

    – bestinamir
    Mar 8 at 17:52

















Make the button type button instead of submit or the form will posts entire page..

– bestinamir
Mar 8 at 17:34





Make the button type button instead of submit or the form will posts entire page..

– bestinamir
Mar 8 at 17:34













Making it a button instead of submit doesn't work, when I click it, nothing happens then.

– user3148270
Mar 8 at 17:40





Making it a button instead of submit doesn't work, when I click it, nothing happens then.

– user3148270
Mar 8 at 17:40













You are sending key as 'pursel' in the data, but checking on key 'purse'. Also what is the value of purse in data: 'pursel' : purse?

– Anurag Srivastava
Mar 8 at 17:41





You are sending key as 'pursel' in the data, but checking on key 'purse'. Also what is the value of purse in data: 'pursel' : purse?

– Anurag Srivastava
Mar 8 at 17:41













That's actually a good catch, I mean't 'purse' : purse. I was just experimenting with AJAX since I have never done them before. All I am trying to do with it is send that POST request so that the conditional sets off.

– user3148270
Mar 8 at 17:43





That's actually a good catch, I mean't 'purse' : purse. I was just experimenting with AJAX since I have never done them before. All I am trying to do with it is send that POST request so that the conditional sets off.

– user3148270
Mar 8 at 17:43













Remove the comma in “ type: POST,” line since it’s a last line, check for js errors and add console.log(123) to see if function gets called..

– bestinamir
Mar 8 at 17:52






Remove the comma in “ type: POST,” line since it’s a last line, check for js errors and add console.log(123) to see if function gets called..

– bestinamir
Mar 8 at 17:52













1 Answer
1






active

oldest

votes


















0














You're using



$.ajax(
url: url,
data: 'purse' : purse,
type : "POST"
);


but we don't see the definition of 'url' or 'purse' anywhere. Did you debug it using Chrome/Firefox Developer Tools and check if it actually sends the ajax request?



Also, you should first check the existence of the ['purse'] key in your php code and then check validity. You're only checking existence, so the if statement will be entered any time you send the ajax, regardless of whether 'purse' actually contains anything.






share|improve this answer

























  • Honestly, the 'purse' was just me messing around. All I was intending to do is for the user to click a button, for AJAX to recognize which button was clicked, according to id, and to send out a Post request so that a keyword like 'purse' could be read by the conditional isset($_POST['purse'])

    – user3148270
    Mar 8 at 18:17











  • Your code should work then. Did you debug it by now?

    – mhpcc
    Mar 8 at 21:38











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55068176%2fhow-to-use-ajax-post-request-to-trigger-a-series-of-php-code%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You're using



$.ajax(
url: url,
data: 'purse' : purse,
type : "POST"
);


but we don't see the definition of 'url' or 'purse' anywhere. Did you debug it using Chrome/Firefox Developer Tools and check if it actually sends the ajax request?



Also, you should first check the existence of the ['purse'] key in your php code and then check validity. You're only checking existence, so the if statement will be entered any time you send the ajax, regardless of whether 'purse' actually contains anything.






share|improve this answer

























  • Honestly, the 'purse' was just me messing around. All I was intending to do is for the user to click a button, for AJAX to recognize which button was clicked, according to id, and to send out a Post request so that a keyword like 'purse' could be read by the conditional isset($_POST['purse'])

    – user3148270
    Mar 8 at 18:17











  • Your code should work then. Did you debug it by now?

    – mhpcc
    Mar 8 at 21:38















0














You're using



$.ajax(
url: url,
data: 'purse' : purse,
type : "POST"
);


but we don't see the definition of 'url' or 'purse' anywhere. Did you debug it using Chrome/Firefox Developer Tools and check if it actually sends the ajax request?



Also, you should first check the existence of the ['purse'] key in your php code and then check validity. You're only checking existence, so the if statement will be entered any time you send the ajax, regardless of whether 'purse' actually contains anything.






share|improve this answer

























  • Honestly, the 'purse' was just me messing around. All I was intending to do is for the user to click a button, for AJAX to recognize which button was clicked, according to id, and to send out a Post request so that a keyword like 'purse' could be read by the conditional isset($_POST['purse'])

    – user3148270
    Mar 8 at 18:17











  • Your code should work then. Did you debug it by now?

    – mhpcc
    Mar 8 at 21:38













0












0








0







You're using



$.ajax(
url: url,
data: 'purse' : purse,
type : "POST"
);


but we don't see the definition of 'url' or 'purse' anywhere. Did you debug it using Chrome/Firefox Developer Tools and check if it actually sends the ajax request?



Also, you should first check the existence of the ['purse'] key in your php code and then check validity. You're only checking existence, so the if statement will be entered any time you send the ajax, regardless of whether 'purse' actually contains anything.






share|improve this answer















You're using



$.ajax(
url: url,
data: 'purse' : purse,
type : "POST"
);


but we don't see the definition of 'url' or 'purse' anywhere. Did you debug it using Chrome/Firefox Developer Tools and check if it actually sends the ajax request?



Also, you should first check the existence of the ['purse'] key in your php code and then check validity. You're only checking existence, so the if statement will be entered any time you send the ajax, regardless of whether 'purse' actually contains anything.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 8 at 22:05









Martijn Pieters

724k14125402343




724k14125402343










answered Mar 8 at 17:50









mhpccmhpcc

627




627












  • Honestly, the 'purse' was just me messing around. All I was intending to do is for the user to click a button, for AJAX to recognize which button was clicked, according to id, and to send out a Post request so that a keyword like 'purse' could be read by the conditional isset($_POST['purse'])

    – user3148270
    Mar 8 at 18:17











  • Your code should work then. Did you debug it by now?

    – mhpcc
    Mar 8 at 21:38

















  • Honestly, the 'purse' was just me messing around. All I was intending to do is for the user to click a button, for AJAX to recognize which button was clicked, according to id, and to send out a Post request so that a keyword like 'purse' could be read by the conditional isset($_POST['purse'])

    – user3148270
    Mar 8 at 18:17











  • Your code should work then. Did you debug it by now?

    – mhpcc
    Mar 8 at 21:38
















Honestly, the 'purse' was just me messing around. All I was intending to do is for the user to click a button, for AJAX to recognize which button was clicked, according to id, and to send out a Post request so that a keyword like 'purse' could be read by the conditional isset($_POST['purse'])

– user3148270
Mar 8 at 18:17





Honestly, the 'purse' was just me messing around. All I was intending to do is for the user to click a button, for AJAX to recognize which button was clicked, according to id, and to send out a Post request so that a keyword like 'purse' could be read by the conditional isset($_POST['purse'])

– user3148270
Mar 8 at 18:17













Your code should work then. Did you debug it by now?

– mhpcc
Mar 8 at 21:38





Your code should work then. Did you debug it by now?

– mhpcc
Mar 8 at 21:38



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55068176%2fhow-to-use-ajax-post-request-to-trigger-a-series-of-php-code%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived