AngularJS , ionic and PHP Undefined property: stdClass - javascript

I'm inserting variables into database and all values get inserted in the fields with expection of one field (pic). i get this error
<b>Notice</b>: Undefined property: stdClass::$pic in <b>C:\xampp\htdocs\work\app_ion\templates\index_files\addphoto_insert.php</b> on line <b>5</b><br />
JS
.controller("camera_controller",['$scope','$http','$cordovaCamera','$ionicLoading','$ionicPopup','$timeout','$location', function ($scope,$http,$cordovaCamera,$ionicLoading,$ionicPopup,$timeout,$location) {
$scope.email=localStorage.getItem("email");
$http.get('http://localhost/work/app_ion/templates/index_files/addphoto_grab.php?email='+$scope.email).success(function(data){
console.log(JSON.stringify(data));
$scope.grab=data;
});
$http.post("http://localhost/work/app_ion/templates/index_files/addphoto_insert.php",
{'pic':$scope.pic,'email':$scope.item.email,'user_id':$scope.item.profile_id,'country':$scope.item.country,'fpage':$scope.item.fpage})
.success(function(data,status,headers,config){
console.log(data)
});
HTML
<div ng-controller="camera_controller" ng-repeat="item in grab">
<form id="form1" name="form1" method="post">
<input name="user_id" type="hidden" id="user_id" ng-model="item.profile_id" />
<input name="country" type="hidden" id="country" ng-model="item.country" />
<input name="fpage" type="hidden" id="fpage" ng-model="item.fpage" />
<input name="pic" type="hidden" id="pic" ng-model="item.pic" />
<input name="email" type="hidden" id="email" ng-model="item.email" />
</form>
</div>
PHP
<?php require_once('../../../Connections/work.php'); ?>
<?php
$data= json_decode(file_get_contents("php://input"));
$pic= mysql_real_escape_string($data->pic);
$email= mysql_real_escape_string($data->email);
$user_id= mysql_real_escape_string($data->user_id);
$country= mysql_real_escape_string($data->country);
$fpage= mysql_real_escape_string($data->fpage);
mysql_query("INSERT INTO work.pp_change (`pic`,`email`,`user_id`,`country`,`fpage`)VALUES('".$pic."','".$email."','".$user_id."','".$country."','".$fpage."')");
?>
Any help with this error?

Related

Post Ckeditor data to mysql

My Ckeditor post variable returns as having the input I need. When I insert it into the column everything inserts, BUT the ckeditor data.
This is my function to insert:
public function SubmitReply() {
$query = <<<SQL
INSERT INTO {$this->tprefix}posts(guid,poster,content,postdate)
VALUES(:guid,:poster,:content,:postdate)
SQL;
$resource = $this->db->db->prepare( $query );
$resource->execute( array (
':guid' => $_POST['guid'],
':poster' => $_POST['poster'],
':content' => htmlspecialchars($_POST['editor1']),
':postdate' => $_POST['postdate'],
));
return $resource->rowCount();
}
And my form has:
<form id="reply" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="process" id="process" value="DEF_FORUM_SUBMIT_REPLY">
<input type="hidden" name="guid" id="guid" value="<?php DEF_TOPIC_NAME($_GET['topic']); ?>">
<input type="hidden" name="poster" id="poster" value="<?php echo $_SESSION['key']['userid']; ?>">
<input type="hidden" name="postdate" id="postdate" value="<?php echo gmdate('Y-m-d H:i:s'); ?>">
<textarea name="editor1" id="editor1" rows="10" cols="80"></textarea>
</form>
<p style="text-align:center; margin-top:10px;">
<input type="submit" class="btn btn-success" value="Post Reply" id="submit_reply">
</p>
It seems that the post for editor1 actually isn't passing now. I have no clue as to why at this point.

multi form ajax php social comment system

I have a lot of these forms that I post with PHP. They are under every article in my page, but when I submit the form with these ajax functions it only sends to the first form of the page. Why? I want send these form separately what am I doing wrong?
<script type="text/javascript" >
$(function() {
$(".submit").click(function()
{
var name = $("#name").val();
var comment = $("#comment").val();
var post_id = $("#post").val();
var dataString = 'name='+ name + '&comment=' + comment+ '&post_id=' + post_id;
if(name=='' || comment=='')
{
alert('Please Give Valid Details');
}
else
{
$("#flash").show();
$("#flash").fadeIn(400).html('<img src="images/loading.gif" />Loading Comment...');
$.ajax({
type: "POST",
url: "commenti.php",
data: dataString,
cache: false,
success: function(html){
$("ol#update").append(html);
$("ol#update li:last").fadeIn("slow");
$("#flash").hide();
}
});
}return false;
});
});
<script>
<ol id="update" class="timeline">
</ol>
<div id="flash"></div>
<div >
<form action="#" method="post">
<input type="hidden" id="post" value="<?php echo $post_id; ?>"/>
<input type="hidden" id="name" value="<?php echo $username; ?>"/>
<textarea id="comment"></textarea><br />
<input type="submit" class="submit" value=" Submit Comment " />
</form>
</div>
<ol id="update" class="timeline">
</ol>
<div id="flash"></div>
<div >
<form action="#" method="post">
<input type="hidden" id="post" value="<?php echo $post_id; ?>"/>
<input type="hidden" id="name" value="<?php echo $username; ?>"/>
<textarea id="comment"></textarea><br />
<input type="submit" class="submit" value=" Submit Comment " />
</form>
</div>
<ol id="update" class="timeline">
</ol>
<div id="flash"></div>
<div >
<form action="#" method="post">
<input type="hidden" id="post" value="<?php echo $post_id; ?>"/>
<input type="hidden" id="name" value="<?php echo $username; ?>"/>
<textarea id="comment"></textarea><br />
<input type="submit" class="submit" value=" Submit Comment " />
</form>
</div>
First of all, as has been said in the comments, don't use repetead IDs. If you don't intend to use Ids as a unique identifier, use classes.
<ol class="timeline update">
<div id="flash"></div>
<div >
<form action="#" id="form" method="post">
<input type="hidden" class="post" value="<?php echo $post_id; ?>"/>
<input type="hidden" class="name" value="<?php echo $username; ?>"/>
<textarea class="comment"></textarea><br />
<input type="submit" class="submit" value=" Submit Comment " />
</form>
</div>
</ol>
In your javascript code, you are trying to get the id name with var name = $('#name'), right? What name?
Also, try to send your requeste with an array rather than a string.
Something like this:
$(".submit").click(function()
{
var name = $(this).parents('form:input.name').val();
var comment = $(this).parents('form:input.comment').val();
var post_id = $(this).parents('form:input.post').val();
var data = {'name': name,
'comment': comment,
'post_id':post_id},
$.ajax({
type: "POST",
url: "commenti.php",
data: data,
success: function(html){
//do stuff
}
}
Access your variable in PHP with $_POST['varName'];
Hope it helps.

Passing a JavaScript Array To PHP Through JQuery $.post

This piece of code gets all the form variables and sends them via AJAX to the PHP script. But I want the calculated results from the PHP script that is being returned to the javascript via a JSON encoded array to be in the form of "post":{"uname":"someNamefromForm","email":"someEmail","fname":"namefromtheform","lname":"lastnamefromform"
}... The output I'm getting now is "uname=e&email=e&fname=e&lname=euname".. This is the JSON array I want to displayed at the bottom of the page for debugging purposes. Can someone tell me how to format it please
This is my HTML form
<div id="wrapper">
<h2> Validation with AJAX,JQuery,JSON and PHP</h2>
<div class="form-container">
<span id="ajax-message"></span>
<form id="ajax-form" onsubmit="return false;">
<p class="legend">All fields marked with an asterisk are required.</p>
<fieldset>
<legend>User Details</legend>
<div>
<label for="uname">Username <em>*</em></label>
<input id="uname" type="text" name="uname" value="" />
</div>
<div>
<label for="email">Email Address <em>*</em></label>
<input id="email" type="text" name="email" value="" />
</div>
<div>
<label for="fname" class="error">First Name <em>*</em></label>
<input id="fname" type="text" name="fname" value="" size="50" class="error"/>
<p class='note'>All error message go here </p>
</div>
<div>
<label for="lname">Last Name <em>*</em></label>
<input id="lname" type="text" name="lname" value="" size="50" />
</div>
</fieldset>
<div class="buttonrow">
<input type="submit" value="Submit This Form via AJAX" class="button" />
<input type="button" value="Start Again" class="button" />
<a >Refresh this Page</a>
</div>
</form>
</div>
<h3>JSON Array</h3>
<pre id='debug'></pre>
</div>
This is my javascript
$("#ajax-form").submit(function(){
var variableToSend = $(this).serialize();
$.post(
'ajaxformval_post.php',
{variable: variableToSend},
function(data){$("#debug").html(data)},
"json"
);
})
This is the php
<?php
$variable = $_POST['variable'];
echo json_encode($variable);
/*$json = array(
'booleanFlag' => TRUE,
'someText' => "AJAX should be renamed AJAJ",
'anArrayOfData' => array('name' => 'Mickey', 'ears' => 'very Big')
);*/
?>
You can send your serialized variable directly like this:
$("#ajax-form").submit(function(){
var variableToSend = $(this).serialize();
$.post(
'ajaxformval_post.php',
variableToSend,
function(data){$("#debug").html(data)},
"json"
);
});
Then on the server side simply output the json_encoded post:
<?php
$variable = $_POST;
echo json_encode($variable);
?>
Did you try changing the "json"-parameter from the $.post-method?
According to documentation https://api.jquery.com/jQuery.post/
dataType
Type: String
The type of data expected from the server. Default: Intelligent Guess (xml, json, script, text, html).
So I guess if you give him "json" he will automatically decode the returned data.

auto show data after search

I want to my code to do is if i search for batchcode the datas that equals to that batchcode will automatically shows in the texboxes basically each batchcodes has (4)datas in each category thats why i have (4) textboxes...and as you can see i separate the textboxes from the php code because i have plans for it and use javascript for each texboxes.
In my current code its not working and i get an error that says: "Notice: Undefined index: code in C:\xampp\htdocs\test\index.php on line 19"
my current code:
<?php
ob_start();
session_start();
?>
<script type="text/javascript" src="jquery/jquery.js"> </script>
<script type="text/javascript" src="jqueryui/js/jquery-ui-1.10.3.custom.min.js"></script>
<script type="text/javascript" src="Zebra_Dialog-master/public/javascript/zebra_dialog.js"></script>
<link rel="stylesheet" href="Zebra_Dialog-master/public/css/default/zebra_dialog.css" type="text/css">
<link type="text/css" href="jqueryui/css/ui-lightness/jquery-ui-1.10.3.custom.min.css" rel="stylesheet" />
<?php
include('include/connect.php');
$batchcode = $_POST['code'];
$sql = mysql_query("SELECT aic,batchcode,address,name FROM tb_app WHERE batchcode LIKE '$batchcode'");
while($rows = mysql_fetch_array($sql)){
$aic[] = $rows['aic'];
$name[] = $rows['name'];
$address[] = $rows['address'];
}
?>
<html>
<head>
<title>test</title>
</head>
<body>
Search Batchcode:<input type="text" name="code" id="query" /><br />
<form>
<table>
<tr>
<td>
aic: <br />
<input type="text" value="<?php echo $aic[0] ?>" /> <br />
<input type="text" value="<?php echo $aic[1] ?>" /> <br />
<input type="text" value="<?php echo $aic[2] ?>" /> <br />
<input type="text" value="<?php echo $aic[3] ?>" /> <br />
</td>
<td>
Name List: <br />
<input type="text" value="<?php echo $name[0] ?>" /> <br />
<input type="text" value="<?php echo $name[1] ?>" /> <br />
<input type="text" value="<?php echo $name[2] ?>" /> <br />
<input type="text" value="<?php echo $name[3] ?>" /> <br />
</td>
<td>
Address: <br />
<input type="text" value="<?php echo $address[0] ?>" /> <br />
<input type="text" value="<?php echo $address[1] ?>" /> <br />
<input type="text" value="<?php echo $address[2] ?>" /> <br />
<input type="text" value="<?php echo $address[3] ?>" /> <br />
</td>
</form>
<!--search function code-->
<script type="text/javascript">
$(document).ready(function(){
$("#query").autocomplete({
source : 'search.php',
select : function(event,ui){
$("#query").html(ui.item.value);
}
});
});
</script>
</body>
</html>
search.php code:
<?php
$q = $_GET['term'];
mysql_connect("localhost","root","");
mysql_select_db("test");
$query = mysql_query("SELECT batchcode FROM tb_app WHERE batchcode LIKE '$q%'");
$data = array();
while($row = mysql_fetch_array($query)){
$data[]=array('value'=>$row['batchcode']);
}
echo json_encode($data);
?>
I want it to do is after searching and clicking to the specific batchcode in the search textbox the data will automatically shows in the textboxes.
Woah Woah Woah Chappy, direct POST data to sql queries a big no no..
Your error on the other hand is caused because the Global Post array has no array index called 'code'. Mainly because no post data has been sent to the page yet.
You would need to wrap all of that in a if statement.
if(isset($_POST['code'])){
///Logic.
I would do some reading on mysql injections and sanatising post data first though.

(HTML) JavaScript + PHP - $_GET Error

What I would like to achieve:
A index page (index.html), which allows the user to register, which runs on JavaScript (index.js) to check the fields (not mentioned in snippet index.js), and then to redirect to a register page (scripts/register.php), which then adds the values to the database.
What is actually happening:
It redirects to the PHP page correctly, however none of the values seem to be transferred when using the $_GET method: I get an empty page.
What am I doing wrong?
Code:
index.html (only a snippet)
<input name="user" type="text" id="user" size="25" />
<input name="email" type="text" id="email" size="25" />
<input name="pass" type="password" id="pass" size="25" />
<input type="submit" name="signup" id="signup" value="Sign Up" />
<script type = "text/javascript", src = "index.js">
</script>
index.js (only a snippet)
document.getElementById("signup").onclick = signup;
var aref = "refcode";
function signup()
{
window.location.href = 'scripts/register.php?emailaddress=' + document.getElementById("email").value + '&username=' + document.getElementById("user").value + '&password=' + document.getElementById("pass").value + '&aref=' + aref;
}
scripts/register.php (only a snippet)
<?php
echo $_GET['emailaddress'];
echo $_GET['username'];
echo $_GET['password'];
echo $_GET['aref'];
?>
EDIT: I accidentally copied the wrong code for 'scripts/register.php', sorry to all the answers who corrected it for me
You're never submitting the form (because you don't seem to have one), thus never getting anything but the data that you embed into the URL (which is very unsecure, not a good idea to send sensitive data like passwords like that).
I'm not sure, however, why are you complicating things like that.
If you want to use GET, no need to build the URL yourself, just set up the form with GET method and use regular submit to send it, no javascript needed. Use the hidden field for the aref value (you can populate it when the form is generated, before submitting, etc, whatever works for you):
<form method="GET" action="scripts/register.php">
<input name="aref" type="hidden" value="refcode" />
<input name="user" type="text" id="user" size="25" />
<input name="email" type="text" id="email" size="25" />
<input name="pass" type="password" id="pass" size="25" />
<input type="submit" name="signup" id="signup" value="Sign Up" />
</form>
Again, changing the method to POST would be a much better idea. Of course, then you need to access the variables like $_POST['aref'], etc. Just like this:
<form method="POST" action="scripts/register.php">
<input name="aref" type="hidden" value="refcode" />
<input name="user" type="text" id="user" size="25" />
<input name="email" type="text" id="email" size="25" />
<input name="pass" type="password" id="pass" size="25" />
<input type="submit" name="signup" id="signup" value="Sign Up" />
</form>
And the PHP (for POST):
<?php
echo $_POST['email'];
echo $_POST['user'];
echo $_POST['pass'];
echo $_POST['aref'];
?>
Your fields are not named the same way in the URL and in register.php. Try this.
<?php
echo $_GET['emailaddress'];
echo $_GET['username'];
echo $_GET['password'];
echo $_GET['aref'];
?>
window.location.href = 'scripts/register.php?emailaddress=' + document.getElementById("email").value + '&username=' + document.getElementById("user").value + '&password=' + document.getElementById("pass").value + '&aref=' + aref;
To access them:
$_GET['username']
$_GET['password']
etc...
In your code you never use the good variable names:
<?php
echo $_GET['email'];
echo $_GET['user'];
echo $_GET['pass'];
echo $_GET['accountref'];
?>
For an Solution without JS, and PHP instead:
<form action="scripts/register.php?<? echo $refcode /* HERES YOUR REFCODE */?>" method="GET">
<input name="user" type="text" id="user" size="25" />
<input name="email" type="text" id="email" size="25" />
<input name="pass" type="password" id="pass" size="25" />
<input type="submit" name="signup" id="signup" value="Sign Up" />
</form>
The normal way to do what you want is using method Attribute in your form or the .submit() event in jquery. I'll show how I would do that:
HTML
without javascript using POST
<form method="post" id="login_form" action='register.php'>
<input name="user" type="text" id="user" size="25" />
<input name="email" type="text" id="email" size="25" />
<input name="pass" type="password" id="pass" size="25" />
<input type="submit" name="signup" id="signup" value="Sign Up" />
</form>
php using $_POST
$user = isset($_Post['user']) ? $_Post['user'] : NULL;
$email = isset($_Post['email']) ? $_Post['email'] : NULL;
$pass = isset($_Post['pass']) ? $_Post['pass'] : NULL;
HTML using Jquery
<form id="login_form" method="post" action="">
<input name="user" type="text" id="user" size="25" />
<input name="email" type="text" id="email" size="25" />
<input name="pass" type="password" id="pass" size="25" />
<input type="submit" name="signup" id="signup" value="Sign Up" />
</form>
<script type = "text/javascript" src = "index.js"></script>
//You have a type with a comma
JS
$('#login_form').submit(function(e){
var data = $(this).serializeArray();
$.post('register.php',data,
function(result){
//Your callback function
})
})
NOTE
My advise to you is that you should use POST method in this case
GET requests a representation of the specified resource. Note that GET should not be used for operations that cause side-effects, such as using it for taking actions in web applications. One reason for this is that GET may be used arbitrarily by robots or crawlers, which should not need to consider the side effects that a request should cause.
and
POST submits data to be processed (e.g., from an HTML form) to the identified resource. The data is included in the body of the request. This may result in the creation of a new resource or the updates of existing resources or both.
So essentially GET is used to retrieve remote data, and POST is used to insert/update remote data.

Categories