Jump to content

Php ajax help required


seupoj

Recommended Posts

Hello i would require help with the given script 
AJAX

Code (text):
  1.  
  2.         $.ajax({
  3.             type: "post",
  4.             url: "http://localhost/raffle.php",
  5.             dataType: "json",
  6.             data: {
  7.                 "postraffle": "true",
  8.                 "title": $("#rtitle").val(),
  9.                 "message": $("#mess").val(),
  10.                 "maxentry": $("#maxentry").val(),
  11.                 "duration": $("#durr").val(),
  12.                 "filter": $("#reffil").val(),
  13.                 "split": $("input[name=split]:checked").val(),
  14.                 "pub": $("input[name=rafflepub]:checked").val(),
  15.                 "stype": $("input[name=stype]:checked").val(),
  16.                 "invo": $("input[name=invo]:checked").val(),
  17.                 "items[]": itms,
  18.                 "games[]": gmes,
  19.                 },
  20.             success: function(data){
  21.                 if(data.status == "fail")
  22.                 {
  23.                     alert(data.message);
  24.                     $("#rafBut").removeAttr("disabled");
  25.                     $("#rafBut").attr("value", "Raffle it!");
  26.                 }
  27.  
  28.                 else if(data.status == "ok")
  29.                 {
  30.                 window.location.href = "http://localhost/raffle.php";
  31.                 }
  32.  
  33.             }
  34.         });
  35.  
  36. </script>

And here is the php script 

PHP:
  1. <?php
  2. $data = array(
  3.     'status' => 'fail',
  4.     'message' => 'Failed to save data',
  5. );
  6. 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 DATA
Some help would be really appreciated 
Thanks!

Link to comment
https://forums.phpfreaks.com/topic/285661-php-ajax-help-required/
Share on other sites

 

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?

 

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

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

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 

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?

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?

Archived

This topic is now archived and is closed to further replies.

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