PHP $_GET with jQuery $.get call - php

I am currently learning jQuery and php.
So I have this php file that gets called with a jQuery function (there is another call somewhere else in the script where a=chkPw):
$.get("checkName.php?a=chkUser&user=" + rqUser, function(data) { ... });
My problem is that the php script doesn't seem to get in the if(isset($_GET)) { } block. I have debugged the value that it returns (1), so I don't understand what's wrong! I have also debugged the value for $_GET['a'] and it is indeed 'chkUser'.
Any help would be greatly appreciated.
<?php
$users = array('bill' => 'ore', 'ted' => 'wood');
if (isset($_GET)) {
if ($_GET['a'] == 'chkUser') {
if (!array_key_exists($_GET['user'], $users)) {
echo 'okay';
} else {
echo 'denied';
}
} elseif ($_GET['a'] == 'checkPw') {
$user = $_GET['user'];
$pw = $_GET['pw'];
$i = 0;
// get username id
foreach (array_keys($users) as $value) {
if ($value == $user) {
$user = $users[i];
}
i++;
}
// match pw
if ($pw == $user) {
echo 'okay';
} else {
echo 'denied'
}
}
}
?>

Hey it seems to me that this is working:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript">
$.get("checkNames.php?a=chkUser&user=bill", function(data) { console.log(data) });
$.get("checkNames.php?a=chkPass&user=bill&pw=ore", function(data) { console.log(data) });
</script>
<?php
$users = array('bill' => 'ore', 'ted' => 'wood');
if (isset($_GET["a"])) {
if ($_GET["a"] == 'chkUser') {
if (!array_key_exists($_GET['user'], $users)) {
echo 'okay';
} else {
echo 'denied';
}
} elseif ($_GET["a"] == 'chkPass') {
$user = $_GET['user'];
$pw = $_GET['pw'];
if(array_key_exists($user, $users))
{
if($users[$user] == $pw)
{
echo "okay";
}
else
{
echo "denied";
}
}
else
{
echo "denied";
}
}
}
?>

Try this if statement instead of current
if (count($_GET) > 0) {
// existing code
}
Hope this will help you.

With a url like www.something.com/index.html?a=1 This code does in fact work on testing:
$users = array('bill' => 'ore', 'ted' => 'wood');
if (isset($_GET)) {
echo 'something';
}
Try commenting it down further to let us know if it is the whole whole elseif or just a part of it

You could try this:
<?php
if(!empty($_GET)){
...
}
?>
To be sure about the GET payload use var_dump($_GET)

Related

Can't Check Checkbox Value With Php and AJAX

I have a form and I use one checkbox. Thanks to the Checkbox, a new field is added and the user fills it.
I ran into a problem with everything going perfectly. The checkbox keeps returning empty, which completely destroys my if else structure.
Checkbox HTML Code:
<input type="checkbox" class="custom-control-input headerElementDrop" id="header_element_drop" name="header_element_drop" onchange="valueChanged()">
AJAX Code:
function headerElementInsert() {
var headerElementAdd = $("#headerElementAdd").serialize();
$.ajax({
type: "POST",
data: headerElementAdd,
url: "actions/header-element-add.php",
success: function (insertSuccess) {
if ($.trim(insertSuccess) == "null") {
//
} else if ($.trim(insertSuccess) == "success") {
//
} else if ($.trim(insertSuccess) == "error") {
//
})
}
}
});
}
header-element-add.php code:
if ($_POST) {
$header_element_name = post('header_element_name');
$header_element_icon = post('header_element_icon');
$header_element_link = post('header_element_link');
$header_element_drop = (isset($_POST["header_element_drop"])) ? 1 : 2 ;
$header_element_drop_class_name = post('header_element_drop_class_name');
$header_element_drop_elements = post('header_element_drop_elements');
if($header_element_drop == 1) {
if(!$header_element_name || !$header_element_drop || !$header_element_drop_class_name || !$header_element_drop_elements) {
echo "null";
}else{
$header_element_drop_class = replace_tr($header_element_drop_class_name);
$header_elements = $db->prepare("INSERT IGNORE INTO header_elements SET header_element_name=?, header_element_icon=?, header_element_link=?, header_element_drop=?, header_element_drop_class_name=?, header_element_drop_class=?, header_element_drop_elements=?");
$insert = $header_elements->execute(array($header_element_name,$header_element_icon,$header_element_link,$header_element_drop,$header_element_drop_class_name,$header_element_drop_class,$header_element_drop_elements));
if($insert) {
echo "success";
}else{
echo "error";
}
}
}else{
if(!$header_element_name) {
echo "null";
}else {
$header_elements = $db->prepare("INSERT IGNORE INTO header_elements SET header_element_name=?, header_element_icon=?, header_element_link=?");
$insert = $header_elements->execute(array($header_element_name,$header_element_icon,$header_element_link));
if($insert) {
echo "success";
}else{
echo "error";
}
}
}
}
I tried many ways to solve this problem but none of them worked. I would really appreciate if you could help.
I found the problem. I forgot to give name value to one input. I apologize for the inconvenience caused.

trying to do facebook like automatic content scrolling but the data is not showing correctly

designing a Facebook like scrolling system the data shows but it does not show the first 5 values and it shows the values in repetition
here is my code:
view:
$(document).ready(function() {
var total_record = 0;
var total_groups = <?php echo $total_data; ?>;
$('#results').load("<?php echo base_url() ?>user/load_more", {'group_no':total_record}, function() {total_record++;});
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height())
{
if(total_record <= total_groups)
{
loading = true;
$('.loader_image').show();
$.post('<?php echo site_url() ?>user/load_more',{'group_no': total_record},
function(data){
if (data != "") {
$("#results").append(data);
$('.loader_image').hide();
total_record++;
}
});
}
}
});
});
then my controller
public function load_more()
{
$group_no = $this->input->post('group_no');
//print_r($group_no);
$content_per_page = 10;
$start = ceil($group_no * $content_per_page);
//print_r($start);
$this->load->model('Pmodel');
$user_id = $this->session->userdata('user_id');
$all_content = $this->Pmodel->get_all_content($user_id,$start,$content_per_page);
// echo '<pre>';
//print_r($all_content);
if(isset($all_content) && is_array($all_content) && count($all_content)) :
foreach ($all_content as $key => $content) :
echo '<li>'.$content->status.'</li>';
echo '<p>'.$content->status_image.'</p>';
endforeach;
endif;
// echo '<pre>'; print_r($this->data['labels_message']); exit;
}
and then my model
public function get_all_content($id,$start,$content_per_page)
{
$query=$this->db->select('*')
->from('post_status')
->where('user_id',$id)
->limit($start,$content_per_page)
->get();
//echo "<pre>";
//print_r($query);
//var_dump($query);
$result = $query->result();
//echo "<pre>";
//print_r($result);
return $result;
}
how to show the data according to the database
let me add a image for database

echo out php variable in jQuery

this is changepassword.php file
<?php
include 'core/init.php';
if (empty($_POST) === false) {
if (md5($_POST['current_password']) === $user_data['password']) {
} else {
$errors[] = 'Your current psasword is incorrect';
}
}
if (empty($_POST) === false && empty($errors) === true) {
change_password($session_user_id, $_POST['password']);
} else if (empty($errors) === false) {
$error = output_errors($errors);
this variable
}
?>
this is the jQuery file
$('#save_pass').click(function(){
var current_password = $('#current').val();
var password = $('#new').val();
var password_again = $('#confirm').val();
if ((password == password_again) && (password.length >= 8)) {
$.post('changepassword.php', {current_password: current_password, password: password, password_again: password_again });
$('#show').html('password changed').fadeIn(500).delay(2000).fadeOut(500);
} else if ((current_password ==0) || (password_again.length == 0) || (password.length == 8)) {
$('#show').html('all fields are required').fadeIn(500).delay(2000).fadeOut(500);
} else {
$('#show').html('your password must be at least 8 characters').fadeIn(500).delay(2000).fadeOut(500); }
$('#current').val(null);
$('#new').val(null);
$('#confirm').val(null);
});
i want to echo out $error variable when a user enters an incorrect password and click on the change password button with id="#save_pass"
You cannot echo php variables within a javascript file. Instead, put the javascript in your php file and echo it there - eg:
<script>
function something() {
alert('<?php echo $error; ?>');
}
</script>
In order to get back the errors from your $.post() ajax call, you need to echo $errors in your php script. Then add a success/done function to your $.post():
$.post('changepassword.php', {current_password: current_password ...})
.done(function (data) {
alert(data);
}
This should be the basics for getting back the raw echo data, but look at $.post documentation for more guidance on how to refine this.

Captcha php to js validation

I have written in php I want to write this code, but there is an error in the function onclick javascript
<?php
if(isset($_POST["captcha"]))
{
if($_POST["captcha"]==$_SESSION["code"])
{
echo "Correct";
}
else
{
echo "Not Correct";
}
}
?>
Javascript
function ja_k2filter_submit()
{
var captcha = $('captcha');
var code = $('code');
if ($code = '');
{
alert("Güvenlik Kodunu Doğrulayınız!");
}
return false;
if ($code = captcha);
if($('input_searchword'))
{
input = $('input_searchword');
v_label = ($$('label[for=input_searchword]')[0].innerHTML)+'...';
if((input.value !='')&&(input.value!=v_label))
{
$('mod_ja_searchword').value =input.value;
}
}
javascript values ​​but does not

Using ajax to grab information from database

I m using codeigniter and would like to grab some user info with ajax. This is what I have but it s not working
In the view I have a defined variable:
<script type="text/javascript">
var end_user = "<? echo $user_id; ?>";
</script>
<div id="tabs6"></div>
js file:
function get_experience()
{
$.post(base_url + "index.php/home/get_experience", { user : end_user }, function(data) {
if (data.status == 'ok')
{
$("div#tabs6").html(data);
}
else
{ //nothing }
}, "json");
}
get_experience();
controller:
public function get_experience()
{
$this->load->model('experience_model');
$end_user = $this->input->post('user');
$one_exp = $this->experience_model->one_exp($end_user);
if ($one_exp->num_rows() > 0)
{
$one_exp_html = '<ul>';
foreach($one_exp->result() as $exp)
{
$one_exp_html .= '<li>';
$one_exp_html .= $exp->experience;
$one_exp_html .= '</li>';
}
$one_exp_html .= '</ul>';
$result = array('status' => 'ok', 'content' => $one_exp_html);
return json_encode($result);
exit();
}
else
{
$result = array('status' => 'ok', 'content' => 'nothing here');
return json_encode($result);
exit();
}
}
model:
function one_exp($end_user)
{
$query_str = "SELECT experience FROM exp WHERE user_id = ?";
$query = $this->db->query($query_str, $end_user);
}
You need to add return $query to your one_exp method.
EDIT
You're setting user_id in your view, but then using end_user in your javascript function get_experience().
Also, since it's json you'll need to change the html fill to
$("div#tabs6").html(data.content);
For more debugging add an alert to your callback (right before if (data.status == 'ok') add alert(data);)
You've got to echo the result out I think, not return it.
I am not sure but problem occurs in end_user value in js.Try this oneView File:
<script type="text/javascript">
var end_user = "<? echo $user_id; ?>";
get_experience(end_user);
</script>
<div id="tabs6"></div>
The js file:
function get_experience(foo)
{
$.post(base_url + "index.php/home/get_experience", { user : foo }, function(data) {
if (data.status == 'ok')
{
$("div#tabs6").html(data);
}
else
{ //nothing }
}, "json");
}

Categories