Getting data from JQUERYpost in PHP - php

My javascript:
$(document).on("click", "#login-submit", function() {
var username = $('#username').val();
var password = $('#password').val();
$.post (
'db/ajax/login.php',
{username:username,password:password},
function(data) {
$('#login-container').html(data);
});
});
My PHP:
if ( $_REQUEST['password'] ) {
echo $_POST['password'];
}
However i am not getting anything back out in my div that the data is to be displayed to. I know Im getting values for the variables and that the on click works because i testing alerting the variables. I just cant seam to get it to the pp PHP. I know the path is correct also.

// PHP
if ( $_REQUEST['password'] ) {
echo $_POST['password'];
}else {
echo 1;
}
// JS
$.post (
'db/ajax/login.php',
{username:username,password:password},
function(data) {
alert(data);
$('#login-container').html(data);
});
Check the alert. If alert is still not working. Please make sure you don't have any js errors in your code

Related

Echo JS variable in Joomla JText

Ajax.php, Script tag in php file
<script>
success: function(data) {
var message = data.message; //Json data
if ( data.status == 1 ) {
jQuery("#msgError").html(<?php echo JText::_(' + message + ');?>);
}
}
Is it possible to Echo JS variable in php like above?
Here is a best example,
<?php
// In php write following code
JText::script('COM_HELLOWORLD_MESSAGE');
<script>
// Using in Javascript like this
Joomla.JText._('COM_HELLOWORLD_MESSAGE')
// For your example look like
success: function(data) {
if ( data.status == 1 ) {
jQuery("#msgError").html(Joomla.JText._('COM_HELLOWORLD_MESSAGE'));
}
}
;
</script>
I already sent code to merge in joomla to make it more advanced. Please check it here https://github.com/joomla/joomla-cms/pull/6006
Also there are many ways to do it which you will find in above link.

jquery get JSON issues

I'm new to server validation and I've looked all over the web and can't find a thing. I'm trying to validate a form with php by posting it to itself and returning a 1 or a 0 in an array depending on whether the form is valid or not. That works fine but the issue I'm having is I want to make the log in screen fade out if the result is 1. I can't get that to work.
Here's my code:
The PHP
if ($valid == 1) {
$isvalid = array('valid' => $valid);
echo json_encode(array_values($isvalid));
};
The javascript
<script>
$(document).ready(function(){
$('#login_button').click(function(){
$.getJSON("/index.php",function(result){
console.log(result);
});
});
});
</script>
nothing outputs into the console, I've also tried alert and nothing happens. The JSON seems to be working in the top left corner I get a [1] which should be what I need.
Here is my whole code maybe it's a matter of placing the script?
<script type="text/javascript" src="/jquery-1.9.1.min.js">
</script>
\\wait to load the script until the document is ready
<script>
$(document).ready(function(){
//on submission
$('#login_button').click(function(){
//check if login was valid
$.getJSON("localhost/index.php",function(result){
console.log(result) .error(function() {console.log('error');});
});
});
});
</script>
<?PHP
$valid = 0;
//Declare the database connection
$con = mysql_connect("localhost","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
//Has the form been submitted?
if(isset($_POST['Username'])){
//Check if Username and PW correct
mysql_select_db ("Users",$con);
$sqlCheckUser = mysql_query ( "select Username from userinfo where UserName = '$_POST[Username]' && Password = MD5('$_POST[Password]')");
//Parse Results
$row = mysql_fetch_assoc($sqlCheckUser);
//is the login valid
if (isset($row) && !empty ($row)) {
$valid = 1;
}
else {
$badpw = "Invalid Username or Password";
}
}
//handle if form has not been submitted
else {
echo "<center>Please Enter Username and Password</center><br />";
};
if ($valid == 1) {
$isvalid = array('valid' => $valid);
echo json_encode(array_values($isvalid));
};
?>
</head>
I validated my JSON, it looks good does anyone know what's going on? I really need this to work.
EDIT
Ok so appending this onto my code does give me the error.
the script now looks like the following
<script>
$(document).ready(function(){
$('#login_button').click(function(){
$.getJSON("index.php",$('#Login').serialize(),function(result){
alert(result);
})
.error(function(error) { alert(error); console.log(error); })
})
});
</script>
The alert text is [object Object]
The console output is this:
object ready state: 0
getResponseHeader: Function
getAllRequestHeaders: Function
SetRequestHeader: Function
overrideimetype: function
http://i.imgur.com/Ei2bBAR.jpg
(I took a screenshot because it wouldn't stay in the console long enough for me to read it)
your response data is on result variable as in your callback function, so change:
console.log(json);
to
console.log(result);

Having an issue with jQuery, AJAX and PHP (posting)

I am using JQuery and AJAX to post to a PHP file which will eventually parse the data and insert it in a database.
I'm having issue display trying to see what's wrong with my javascript that it will not post the PHP file.
I know it is not posting because I don't recieve and email which is the first function in the PHP file.
JS
$(document).ready(function() {
var ptitle = $("#name").val();
var pdesc = $("#desc").val();
var pemail = $("#email.").val();
$('#submit').click(function() {
sendValue(ptitle, pdesc, pemail);
});
});
function sendValue(ptitle, pdesc, pemail) {
$.post("<?=MOLLY.'update.php'?>", {
stitle: ptitle,
sdesc: pdesc,
semail: pemail
}, function(data) {
//
}, "json");
}
PHP
mail($myemail,'test','test');
if ($_POST){
$title = $_POST['stitle'];
$email = $_POST['semail'];
mail($myemail,$email,$title);
}
You have a typo:
$(document).ready(function() {
var ptitle = $("#name").val();
var pdesc = $("#desc").val();
var pemail = $("#email.").val(); // <---- I assume you meant "#email".
$('#submit').click(function() {
sendValue(ptitle, pdesc, pemail);
});
});
Start by checking that your PHP file runs, replace the content with something like:
PHP
echo "Im running";
Then in your JS do:
$.post("<?=MOLLY.'update.php'?>", function(data) {
console.log(data);
}, "json");
and see if the message is returned and logged in the console.
If it is, check your $_POST by echoing back the values you sent, also echo back your email variable to see that it outputs correctly.
If it all works fine, you need to check if your server is set up correctly for the mail command to actually be able to send an email, as the problem is most likely serverside when everything else is eliminated.
Oh, and you should do what cillosis says, use isset and check something in the $_POST superglobal, not just a if($_POST), but since your running the mail function before that in your PHP, that's probably not your main problem.

Javascript (jQuery) - Problem with $.post

So, for some reason my script refuses to work, although it seems to be correct.
I tried using $.ajax instead, but not working with that either. Any ideas what's gone wrong?
<script>
$(document).ready(function() {
$('#saveForm .submit').click(function() {
var _user = $('#saveForm .user');
_userId = $('#saveForm .userId');
_password = $('#saveForm .password');
$('#saveForm').append('<p>Loading...</p>');
$.post("ajax/fetchPhotos.php", {user:_user, userId:_userId, password:_password}, function(data) {
alert(data);
});
return false;
});
});
</script>
In ajax/fetchPhotos.php i have this:
<?php
set_time_limit(0);
session_start();
require_once("includes/functions.php");
require_once("includes/pclzip.lib.php");
/*
Huge block of code here (commented out for the moment)
*/
echo "wut?";
So, when clicking .submit, it should send a request to fetchPhotos.php with three params and then alert "wut?". Right now the page hangs in chrome.. nothing happens. In firefox the page just refreshes. I do get one error in the console, but i only see it for a split second as the page refreshes directly.
Thanks,
Pete
you must use the .val() method to get the value of the inputs.
try this way:
<script>
$(document).ready(function() {
$('#saveForm .submit').bind('click', function() {
var
user = $('#saveForm .user').val(),
id = $('#saveForm .userId').val(),
password = $('#saveForm .password').val();
$('#saveForm').append('<p>Loading...</p>');
$.post("ajax/fetchPhotos.php", {user:user, userId:id, password:password}, function(data) {
alert(data);
});
return false;
});
});
</script>
It seems syntax related problem and also try with absolute url or can do in this way
new Ajax.Request("ajax/fetchPhotos.php", {
method: 'post',
parameters: 'id='+id,
onComplete: function(transport) {
alert(transport.responseText);
}
});

Jquery Form Validation and Checking the values with Mysql Database through PHP Script

I have a form which has a input textbox and submit button.
On submission of the form the textbox value should get pass to an php script and check the values whether it exists in the Mysql Database. If it exists then we need to show an alert box stating that "Entered Value is already exists, Try something new". If the value not exists the form can be submitted to the php script which is in the form action.
I tried with the jquery and the code is below:
$(document).ready(function() {
$("#form_add").submit(function () {
var pval = $("#name").val();
var datastring = 'pname'+pval;
$.post("unique_valid.php", {pname:pval }, function (data){
alert('duplicate');
});
return false;
});
});
Problem with this code is It shows alert box on every case but its not allowing to submit the form if the values is not exists in the database.
Php code :
$pname = $_POST['pname'];
if( $pname == $row['name']){
echo "success";
}else{
echo "failure";
}
Suggest the better solution for this problem.
That's because you're alerting 'duplicate' no matter what the PHP output is. Try checking the value of data before alerting, like this:
$(document).ready(function() {
$("#form_add").submit(function () {
var pval = $("#name").val();
var datastring = 'pname'+pval;
$.post("unique_valid.php", {pname:pval },
function (data){
if(data == 'failure'){
alert('duplicate');
}else{
alert('not a duplicate');
}
});
return false;
});
});
And I'm assuming your PHP code will actually be saving the record if it's not a duplicate (your code doesn't indicate as much, though)?

Categories