Need help with php and ajax

Hello i would require help with the given script
AJAX

[code] $.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";
            }

        }
    });

[/code]
And here is the php script
PHP:
[php]<?php
$data = array(
‘status’ => ‘fail’,
‘message’ => ‘Failed to save data’,
);
echo json_encode($data)[/php];
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
Well currently it shows an error FAILED TO TRANSMIT DATA
Some help would be really appricated
i would like to echo out all the information from ajax to the php script
Thanks!

You are not showing all of your code. From the looks of what you have provided, you only give the option of the data failing.

Is there a way to echo all of the data?
Php code is fully shown here
and rest ill show below -
view-source:http://admin.tftrg.com/Prototype/raffle.html

You can echo everything in an array using var_dump(variable)

In your php script try doing…

[php]
echo ‘

’;
print_r($_POST);
echo ‘
’;
[/php]

it should show everything sent via ajax.
Then if correct you can do with it what you want.

Well is there a way to save all the data to an mysql database?

Absolutely there is. You would do it the same way as if you weren’t using ajax.

For example grab the title using $_POST[‘title’] and message using $_POST[‘message’] & etc…

[php]

$query = “INSERT INTO table (title, message) VALUES (’”.$_POST[‘title’]."’, ‘".$_POST[‘message’]."’)";

[/php]

and of course make sure you escape and validate the values being entered.

Well back to drawing board
i have written the script this doesn’t seem to work
[php]<?php

// getting data from AJAX
$raffle_title = $_POST[‘title’];
$raffle_message = $_POST[‘message’];
$raffle_maxentry = $_POST[‘maxentry’];
$raffle_duration = $_POST[‘duration’];
$raffle_filter = $_POST[‘filter’];
$raffle_split = $_POST[‘split’];
$raffle_pub = $_POST[‘pub’];
$raffle_stype = $_POST[‘stype’];
$raffle_items = $_POST[‘items[]’];

$done = false;

$data = array(
‘status’ => ‘ok’,
‘message’ => ‘saved! redirecting you!’,
‘datakey’ => ‘HALLEYO!’,
);

$host =“localhost”; // enter your host.
$pass ="…"; // enter your password.
$db = “test”; // Enter your database…
$user ="…"; // enter your username.

MYSQL Connection

$con=mysqli_connect($host,$user,$pass,$db);
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$query = “INSERT INTO table (heading,content,items) VALUES (’”.$_POST[‘title’]."’, ‘".$_POST[‘message’]."’,’".$raffle_items."’)";

mysqli_query($con,$query);

echo $data;

?>[/php]

here is ajax

[code]$.ajax({
type: “post”,
url: “http://admin.tftrg.com/Prototype/php/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”)
{
alert(data.message);
}

			}
		});[/code]

Thanks!

Which error(s) do you get?

Also you should look into parameterized queries, you’re leaving your code wide open for SQL injection

As you know the ajax script doesnt show any errors
well the data is not inserted into the database.
no errors in php script
you can see it live here
http://admin.tftrg.com/Prototype/raffle.html
Thanks for you help!

You should add error reporting to your script, you can find it here on the forums, it’s just to paste two lines at the beginning.

You have an undefined index in the line with $_POST[‘items[]’], so you will want to set it up to show all errors, warnings and notices.

using fieldName[] in forms will add values to an array called fieldName. So on the server side you can use $_POST[‘items’], which will then contain an array with all the values passed from the form.

Nah mate it didnt work i have edited
this
[php]$_POST[‘items[]’];[/php]
To
[php]$_POST[‘items’];[/php]
it doesn’t work and the php script has no errors check it out here -
http://admin.tftrg.com/Prototype/php/raffle.php
Thanks!

$_POST[‘items’] will return an array, something is returning an array. You need to use an index or loop to list the items. Or need to change the method you are using to show everything being held in the array.

how do i do that?

You could use print_r() to easily check to make sure the array is populated correctly.

To actually do something constructive with the values I suggest a foreach loop…

[php]
foreach($_POST[‘items’] as $item){
echo $item .’
’;
}
[/php]

http://us2.php.net/manual/en/control-structures.foreach.php

I’m still not convinced :stuck_out_tongue:

try to add this to the beginning of your script

[php]ini_set(‘error_reporting’, E_ALL);
ini_set(‘display_errors’, ‘1’);[/php]

edited version of the script
[php]<?php

// getting data from AJAX
$raffle_title = $_POST[‘title’];
$raffle_message = $_POST[‘message’];
$raffle_maxentry = $_POST[‘maxentry’];
$raffle_duration = $_POST[‘duration’];
$raffle_filter = $_POST[‘filter’];
$raffle_split = $_POST[‘split’];
$raffle_pub = $_POST[‘pub’];
$raffle_stype = $_POST[‘stype’];

$done = false;

$data = array(
‘status’ => ‘ok’,
‘message’ => ‘saved! redirecting you!’,
‘datakey’ => ‘HALLEYO!’,
);

$host =“localhost”; // enter your host.
$pass =“Hero1992”; // enter your password.
$db = “whatsup4_test”; // Enter your database…
$user =“whatsup4”; // enter your username.

MYSQL Connection

$con=mysqli_connect($host,$user,$pass,$db);
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

foreach($_POST[‘items’] as $item){
$query = “INSERT INTO table (heading,content,items) VALUES (’”.$_POST[‘title’]."’, ‘".$_POST[‘message’]."’,’".$item."’)";
}
mysqli_query($con,$query);

echo $data;

?>[/php]
well it doesnt work there seem to be another problem

Done sir still the script doesnt seem to work.

Ok, at least we have error reporting now :slight_smile: Please add error checking to your mysqli query

And you should move the actual query into the loop.

[php]<?php

// getting data from AJAX
$raffle_title = $_POST[‘title’];
$raffle_message = $_POST[‘message’];
$raffle_maxentry = $_POST[‘maxentry’];
$raffle_duration = $_POST[‘duration’];
$raffle_filter = $_POST[‘filter’];
$raffle_split = $_POST[‘split’];
$raffle_pub = $_POST[‘pub’];
$raffle_stype = $_POST[‘stype’];

$done = false;

$data = array(
‘status’ => ‘ok’,
‘message’ => ‘saved! redirecting you!’,
‘datakey’ => ‘HALLEYO!’,
);

$host =“localhost”; // enter your host.
$pass =“Hero1992”; // enter your password.
$db = “whatsup4_test”; // Enter your database…
$user =“whatsup4”; // enter your username.

MYSQL Connection

$con=mysqli_connect($host,$user,$pass,$db);
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

foreach($_POST[‘items’] as $item){
$query = “INSERT INTO table (heading,content,items) VALUES (’”.$_POST[‘title’]."’, ‘".$_POST[‘message’]."’,’".$item."’)";
// this should also be done for each item
if (!mysqli_query($con, $query)) {
printf(“Error: %s\n”, mysqli_error($con));
}
}

echo $data;

?>[/php]

Again, you should look into parameterized queries, it litereally hurts posting this code.

Sponsor our Newsletter | Privacy Policy | Terms of Service