Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialPrince eudes Ononiwu
410 Pointsi am having problems trying to submit a form using ajax function ,, how can i read the returned json array f
i can seem to read to the returned json array from the server ??
3 Answers
Serhii Lihus
6,352 PointsCan you show us both back-end and front-end part? Paste all your code here. It's hard to say where the problem is, when you do not see the whole picture.
Prince eudes Ononiwu
410 Points<?php include('config/connect.php'); $error=array(); $firstname=$database->cleaner(isset($_POST["firstname"]))?true:false; $lastname=$datbase->cleaner(isset($_POST["lastname"]))?true:false; $email_address=$datbase->cleaner(isset($_POST["email_address"]))?true:false;
/* the validation rules */
if(empty($firstname)){
$error[]="the firstname can not be left blank";
}
if(empty($lastname)){
$error[]="the lastnamecan not be left blank";
}
if(empty($email_address)){
$error[]="please type in an email address";
} if(empty($message)){
$error[]="the message field can not be left blank";
} if(!empty($error)){ foreach($error as $key=>$value){ $error_json=array("error"=>$value); echo json_encode($error_json); } }else{ $query="INSERT INTO contact_messages (firstname,lastname,email_address,message,date) VALUES('$firstname','$lastname','$email_address','$message',NOW())"; $query_connect=$database->query($query); if(mysqli_num_rows($query_connect)>0){ echo "thanks and we will get back to yoou "; } }
?>
/* jquery*/
$('form').submit(function (event){ event.preventDefault(); var form=$(this); var formData=form.serialize(); $.post('server.php',formData,function (data){ if(data.error){ $.each(data,function(index,value){ var outputDiv=$('#output'); outputDiv.html(value); }); }else{ outputDiv.html(data); } });
});
NOTE::i am really new in the stuff and would really appreciate every mistake i made in my code ..thanks
Serhii Lihus
6,352 PointsHi there Prince eudes Ononiwu!
There is always a room for mistake :) But, lets get to the point.
In this part of code, there is a few things that can be redone.
if(!empty($error)){
foreach($error as $key=>$value)
{
$error_json=array("error"=>$value); // 1)
echo json_encode($error_json); // 2)
}
}
// you can find descriptions below
1) There is no need to form $error array again inside of foreach loop. You already have it.
2) After each iteration of foreach loop(depending how many errors you will get) you are forming json response which is not correct(or lets say 'not a good practice'). It's better to move json_encode() out of foreach loop. In order to send only one object(not many). So, this part can look like this.
if(!empty($error)){
echo json_encode($error);
}
Small and accurate ;)
3) Another small but very important thing.
In order to name your errors, which will be needed to identify what is what, you can add names to your errors. Like this.
if(empty($firstname)){
$error['error']['firstname']="the firstname can not be left blank";
}
After object is sended you can get it property value by name.
console.log(data.error['firstname']);
It seems like your JS is ok. But after you do this small changes tell me about results that you received.
Best regards.