Need help with php and ajax

No luck i dont think this is ever gonna work :-[

add your entire script here again as it is now so that we can run the code you have and see what’s wrong

okay ajax script whole is here

[code] $(document).ready(function(){
$(".infitem").offset({ top: 0, left: 0 });

	$(".item").hover(function(){
		var op = $(this);
		
		var pos = $(op).offset();
		var height = $(op).height();
		var width = $(op).width();
		
		$(".infname").html(op.attr("iname"));
		$(".infdesc").html("Level: "+op.attr("ilevel")+((op.attr("iu1") != "") ? "<br />#"+op.attr("iu1") : ""));
		
		if(op.attr("ilevel") != "")
			$(".infitem").show();
		
		$(".infitem").css({ "left": pos.left + (width / 2) - ($(".infdesc").width() / 2) + "px", "top": pos.top + height + "px" });
		
	},function(){
		$(".infitem").hide();
	});
	
	$(".item").click(function(){
		if($(this).attr("id") == "notselected"){
			$(this).appendTo("#selitems");
			$(this).attr("id", "selected");
		}else if($(this).attr("id") == "selected"){
			$(this).appendTo("#allitems");
			$(this).attr("id", "notselected");
		}else if($(this).attr("id") == "gamenotselected"){
			$(this).appendTo("#selitems");
			$(this).attr("id", "gameselected");
		}else if($(this).attr("id") == "gameselected"){
			$(this).appendTo("#allgames");
			$(this).attr("id", "gamenotselected");
		}
	});
	
	$("#rafBut").click(function(){
		$(this).attr("disabled", "disabled");
		$(this).attr("value", "Please wait...");
		
		var itms = new Array();
		var gmes = new Array();
		var ia = 0;
		var ga = 0;
		
		$(".item").each(function(i){
			if($(this).attr("id") == "selected"){
				itms[ia] = $(this).attr("iid")+":"+$(this).attr("iqual")+":"+$(this).attr("ilevel")+":"+$(this).attr("iu1");
				ia++;
			}
			if($(this).attr("id") == "gameselected"){
				gmes[ga] = $(this).attr("iid");
				ga++;
			}
		});
		
		$.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]
and php code is here
[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 =""; // enter your password.
$db = “test”; // Enter your database…
$user =“4”; // 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]

could you post an example of a form as well? just so we’re doing exactly the same…

Hello,
i have the whole html document here you as the forums has posting word limit.
http://tny.cz/ab87512a
Thanks!

Anything?

Your ajax callback is expecting JSON but you are not returning JSON.

When you echo $data in your php script you are really only echoing a string that says ‘Array’.

In your php script, comment every one of the mysql lines.
Then json_encode your $data echo, or better yet json_encode your $_POST and then console log that via ajax in your html script.

NEW PHP 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 = ""; // enter your password. $db = "test"; // Enter your database.. $user = "4"; // 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 json_encode( $_POST ); ?>

[/php]

and your ajax call…
[php]
$.ajax({
type: “post”,
url: “ajax.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) {
console.log(data);
}
});
[/php]

In the developer console you should see an JSON object returned with all the values that you sent on over.

Once you know that you are correctly sending and receiving the values, then you can start trying to insert them into the database.

Also, have you been checking to see if they are inserting? Or, were you assuming they weren’t because you weren’t seeing a response?

Nope.
Well i would just like to insert all values recived from ajax to mysql database.
Thanks!

Are you asking me to make that query for you?

I’m not sure what is needed now.

If you don’t know how to do a query to the db then this should get you started
http://us2.php.net/manual/en/mysqli.prepare.php#refsect1-mysqli.prepare-examples

Well is there any way i can input all the AJAX data to MYSQL database so i can use it further
Thanks!

Yes you can, but this is where you’re going to need to do some research on your own.
Right now you don’t need help solving a problem, you need to learn how to do something. If we give you the query to insert the data into a database then you are not learning anything.

I suggest you Google “php mysqli parameterized queries”, as that will get you a good start to querying the database.

If you run into problems we are always here to help you through them as long as you provide the code, we will not create the code for you.

Well is there any way i can save the data into an txt file on the HDD using php rather uploading to Mysql
Thanks!

Can you suggest me an good tutorial

https://www.google.com/search?q=good+mysqli+tutorial&oq=good+mysqli+tutorial&aqs=chrome..69i57j0j69i64.5774j0j4&sourceid=chrome&ie=UTF-8

http://www.xphp.info/mysqli-tutorial/

http://mattbango.com/notebook/code/prepared-statements-in-php-and-mysqli/

http://www.dreamincode.net/forums/topic/54239-introduction-to-mysqli-and-prepared-statements/

After doing lots of research i have done this change which doesnt seem to work
tell me what i am lacking in now
[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 = “”; // 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();
}

$array_items = mysqli_escape_string(serialize($raffle_items));

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

mysqli_query($con,$query);

echo $data;

?>[/php]

What errors are you receiving?

no errors its just not saving in database

Assuming “table” is your database’s table name.

TRY

[php]
$message = $_POST[‘message’];
$title = $_POST[‘title’];

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

Here is an example of a working insert query I use.

[php]
$notes_insert = “INSERT INTO Notes (note,college, name, type,award, emphasis, department)
VALUES (’”.$note_insert."’,’".$college_insert."’, ‘".$name_insert."’, ‘".$types_insert."’, ‘".$award_insert."’, ‘".$emphases_insert."’, ‘".$department_insert."’)";
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service