seupoj Posted January 25, 2014 Share Posted January 25, 2014 Hello i would require help with the given script AJAX Code (text): $.ajax({ type: "post", url: "http://localhost/raffle.php", dataType: "json", data: { "postraffle": "true", "title": $("#rtitle").val(), "message": $("#mess").val(), "maxentry": $("#maxentry").val(), "duration": $("#durr").val(), "filter": $("#reffil").val(), "split": $("input[name=split]:checked").val(), "pub": $("input[name=rafflepub]:checked").val(), "stype": $("input[name=stype]:checked").val(), "invo": $("input[name=invo]:checked").val(), "items[]": itms, "games[]": gmes, }, success: function(data){ if(data.status == "fail") { alert(data.message); $("#rafBut").removeAttr("disabled"); $("#rafBut").attr("value", "Raffle it!"); } else if(data.status == "ok") { window.location.href = "http://localhost/raffle.php"; } } }); </script> And here is the php script PHP: <?php $data = array( 'status' => 'fail', 'message' => 'Failed to save data', ); echo json_encode($data); You can see the script live here admin.tftrg.com/Prototype/raffle.html I want all of the above data to transmit from HTML using ajax to php i would like to echo all the information from the ajax script to the php script Well currently it shows an error FAILED TO TRANSMIT DATASome help would be really appreciated Thanks! Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 25, 2014 Share Posted January 25, 2014 (edited) Well currently it shows an error FAILED TO TRANSMIT DATA From the PHP code you posted that is all it currently does It sets the status to "Fail" and the error message to "Failed to save data". What do you want the PHP script to do with the data it receives? Edited January 25, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
seupoj Posted January 25, 2014 Author Share Posted January 25, 2014 From the PHP code you posted that is all it currently does It sets the status to "Fail" and the error message to "Failed to save data". What do you want the PHP script to do with the data it receives? i want all of the data to be echoed in json format Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 25, 2014 Share Posted January 25, 2014 (edited) The data is being transferred via POST. So use json_encode on $_POST superglobal array. echo json_encode($_POST); Edited January 25, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
seupoj Posted January 25, 2014 Author Share Posted January 25, 2014 The data is being transferred via POST. So use json_encode on $_POST superglobal array. echo json_encode($_POST); Should i change this also? From success: function(data){ if(data.status == "fail") { alert(data.message); $("#rafBut").removeAttr("disabled"); $("#rafBut").attr("value", "Raffle it!"); } else if(data.status == "ok") { window.location.href = "http://localhost/raffle.php"; } To success: function(data){ window.location.href = "http://localhost/raffle.php"; } So i can also href when the data is transmitted to the PHP Script Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 25, 2014 Share Posted January 25, 2014 (edited) I dont understand you still haven't done anything with the data. You have coverted the $_POST array to json, but nothing else. window.location.href will redirect the user, which will cause the data in the ajax request to be forgotten. The success: will be called when the ajax request is successful. the data variable will contain the information PHP has echo'd. In your case the json Edited January 25, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
seupoj Posted January 25, 2014 Author Share Posted January 25, 2014 I dont understand you still haven't done anything with the data. You have coverted the $_POST array to json, but nothing else. window.location.href will redirect the user, which will cause the data in the ajax request to be forgotten. The success: will be called when the ajax request is successful. the data variable will contain the information PHP has echo'd. In your case the json Okay i get it what are you saying so should i save it on a mysql database? and how do i do that Thanks for your help i really appreciate it Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 25, 2014 Share Posted January 25, 2014 (edited) I cant answer that. So far you providing a form so users can select items and provide options for a raffle. When the Create Raffle button is clicked you are submitting the data to PHP using ajax. When PHP retrieves the POST data you are json encoding it. What is the next step you are wanting to do? Edited January 25, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
seupoj Posted January 26, 2014 Author Share Posted January 26, 2014 I cant answer that. So far you providing a form so users can select items and provide options for a raffle. When the Create Raffle button is clicked you are submitting the data to PHP using ajax. When PHP retrieves the POST data you are json encoding it. What is the next step you are wanting to do? Well can i save it on my server like a .json file after every form is submitted? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 26, 2014 Share Posted January 26, 2014 (edited) Yes you can do what ever you want with it. To write data to a file use file_put_contents Edited January 26, 2014 by Ch0cu3r 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.