I am creating a form for users to change their passwords and to do this I get their username from the url and $_GET the data.
For example:
Website.com/passwordreset.php?username=moonman&key=d77e39f7f6ea2410bfbb59e7f96320ea81eed443fa31d9d327c37635ce28c9a2
// PHP //
$key = $_GET['key'];
$username = $_GET['username'];
if(isset($_POST["password"])){
echo $key;
echo $username;
echo $_GET['key'];
echo $_GET['username'];
echo "Test";
}
//Ajax//
function passwordchange(){
var password1 = _("password1").value;
var ajax = ajaxObj("POST", "passwordreset.php");
ajax.onreadystatechange = function(){
ajax.send("password="+password1);
}
However the previous data seems to be unreadable when users try to send their new data through AJAX.
The output will be "Test" and nothing else.
Keep in mind that AJAX data can be sent perfectly and I tried working through with this by using sessions but failed.
var ajax = ajaxObj("POST", "<?= $_SERVER['REQUEST_URI'] ?>");
Problem was that i didn't take in the url which had the $_GET data.
Thank you to Alejandro Iván for helping me solve this problem.
I want to validate if username exists in database using jQuery.validate so here's what I have so far:
jQuery:
$("#signupForm").validate({
rules: {
username: {
required: true,
minlength: 3,
remote: "check-username.php"
}
},
messages: {
username:{
remote: "This username is already taken! Try another."
}
}
});
check-username.php:
<?php
require_once "./source/includes/data.php";
header('Content-type: application/json');
$name = mysql_real_escape_string($_POST['username']);
$check_for_username = mysql_query("SELECT username FROM mmh_user_info WHERE username='$name'");
if (mysql_num_rows($check_for_username) > 0) {
$output = true;
} else {
$output = false;
}
echo json_encode($output);
?>
This code always shows an error that the username is taken even if it's not.
I'm using jQuery Mobile 1.9.1
Thanks in advance.
I've managed to get this to work by changing the PHP technique I was using, here's my PHP:
<?php
require_once "./source/includes/data.php";
header('Content-type: application/json');
$request = $_REQUEST['username'];
$query = mysql_query("SELECT * FROM mmh_user_info WHERE username ='$username'");
$result = mysql_num_rows($query);
if ($result == 0){
$valid = 'true';}
else{
$valid = 'false';
}
echo $valid;
?>
Thanks everyone here for your help :)
I have two resources for to look at.
Official example from the validate plugin:
http://jquery.bassistance.de/validate/demo/milk/
jQuery forum solution:
http://forum.jquery.com/topic/jquery-validation-plugin-remote-validation-problem-locks-up
The possible solution is that the response does not need to be json encoded as far as I can tell. Since json needs key value pairs, suppling just the value won't work. So try to just echo it out as 'true' or 'false' strings.
Second, the validate uses GET for the form submission method, not POST.
NOTE: JUST FOUND POSSIBLE SOLUTION QUESTION
jQuery Remote validation
I'm currently trying to make a email verification sender in case an individual didn't receive the email to verify their account. Where my issue comes in is that I have to echo the individuals email into javascript (along with their name and other things). I don't want individuals to open the source code and see that their email is there within javascript, is there a way that I can encrypt this json encode? Here is my script for
Verification.php
<script type="text/javascript">
$(document).ready(function() {
$("#cliq").click(function() {
//get input field values
var ndxr = <?php $to_Email = $_SESSION['SESS_CONTROL_EMAIL']; echo json_encode($to_Email); ?>;
var fvje = <?php $vcode = $_SESSION['SESS_CONTROL_VCODE']; echo json_encode($vcode); ?>;
var name = <?php $name = $_SESSION['SESS_CONTROL_FIRST']; echo json_encode($name); ?>;
var proceed = true;
if(proceed)
{
post_data = {'ndxr':ndxr, 'fvje':fvje, 'name':name};
$.post('verifye.php', post_data, function(response){
if(response.type == 'error')
{
output = '<div class="error">'+response.text+'</div>';
}else{
output = '<div class="success">'+response.text+'</div>';
}
$("#result").hide().html(output).slideDown();
}, 'json');
}
});
});
</script>
And here is the verification page Verifye.php
$name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$user_Email = filter_var($_POST["ndxr"], FILTER_SANITIZE_EMAIL);
$vcode = filter_var($_POST["fvje"], FILTER_SANITIZE_STRING);
Any way of preventing the user's email from displaying?
Don't display it at all! If all this data is present in your session, there's no need to pass it to the user in the first place — the script that you're POSTing to from AJAX can pull that data out of the session itself.
the way I would do this is by using base64_encode function and the base64_decode function on the other end.
i have some problem, i made some local website using php.
I have a file called functions.php which this is the code:
public function saveAll($idmt_salesarea, $thn_bln, $no_pesanan, $tgl_pesan, $idms_langganan, $idms_kodebarang, $quantity, $harga, $jumlah, $disc_cash, $disc_kredit){
$message = "Waiting input...";
try{
$con = new db();
$conn = $con->connect();
$query = "INSERT INTO mt_pesanan(idmt_salesarea,thn_bln,no_pesanan,tgl_pesan,idms_langganan, idms_kodebarang,quantity,harga,jumlah,disc_cash,disc_kredit) VALUES ($idmt_salesarea, '$thn_bln', '$no_pesanan', '$tgl_pesan', $idms_langganan, $idms_kodebarang, '$quantity', $harga, $jumlah, '$disc_cash', '$disc_kredit')";
$result = mysqli_query($conn, $query) or die(mysqli_error($conn) . " " . mysqli_errno());
if($result == 1){
$message = "Success";
} else if($result == 0){
$message = "Failed";
}
}catch(Exception $exc){
echo $exc->getCode();
}
$con->disconnect();
return $message;
}
i Take the input parameter from file called: index.php and pass the parameter using AJAX Jquery. The parameter itself is sent and pointing to file called insert.php
here's the insert.php file:
<?php
include_once 'functions.php';
$idmt_salesarea = isset($_GET['salesarea']);
$thn_bln = isset($_GET['thn_bln']);
$no_pesanan = isset($_GET['nopes']);
$tgl_pesan = isset($_GET['tglpes']);
$idms_langganan = isset($_GET['idlangganan']);
$idms_kodebarang = isset($_GET['idbarang']);
$quantity = isset($_GET['quantity']);
$harga = isset($_GET['harga']);
$jumlah = isset($_GET['jumlah']);
$disc_cash = isset($_GET['disc_cash']);
$disc_kredit = isset($_GET['disc_kredit']);
if (($disc_cash == null) || ($disc_kredit == null)) {
$disc_cash = 0;
$disc_kredit = 0;
}
$insert = new functions();
$insert->saveAll($idmt_salesarea, $thn_bln, $no_pesanan, $tgl_pesan, $idms_langganan, $idms_kodebarang, $quantity, $harga, $jumlah, $disc_cash, $disc_kredit);
?>
but when i check the error, that is the variable that cannot get from insert.php file (using $_GET statement).
How proper way to gain the variable? because all the parameter is set.
I know this is combining object oriented style and old fashion php coding. Any ideas?
thanks in advance.
UPDATE
here's the index.php file using jquery ajax to sent the data
function sendAll(){
var tgl_pesan = $('#dpc').val();
var sales_area = $('#sales_area').val();
var nopes = $('#no_pesanan').val();
var thnbln = getTahunBulan();
var id_langganan = $('#kode_langganan').val();
var id_barang = $('#kode_barang').val();
var quantity = getQuantity();
var harga = $('#harga').val();
var jumlah = $('#jumlah').val();
var disc_cash = $('#cash').val();
var disc_kredit = $('#kredit').val();
var max = $('#max').val();
$.ajax({
type:"POST",
**url:"insert.php",**
data:{
salesarea:sales_area,
thn_bln:thnbln,
nopes:nopes,
tglpes:tgl_pesan,
idlangganan:id_langganan,
idbarang:id_barang,
quantity:quantity,
harga:harga,
jumlah:jumlah,
disc_cash:disc_cash,
disc_kredit:disc_kredit,
max:max
},
success:function(msg){
alert("Data Inserted");
},
error:function(msg){
alert("Data Failed to save" + msg);
}
});
the ajax itself is pointing to file insert.php which the insert.php is executing function from another file called functions.php
The problem is with this code:
$idmt_salesarea = isset($_GET['salesarea']);
$thn_bln = isset($_GET['thn_bln']);
$no_pesanan = isset($_GET['nopes']);
$tgl_pesan = isset($_GET['tglpes']);
$idms_langganan = isset($_GET['idlangganan']);
$idms_kodebarang = isset($_GET['idbarang']);
$quantity = isset($_GET['quantity']);
$harga = isset($_GET['harga']);
$jumlah = isset($_GET['jumlah']);
$disc_cash = isset($_GET['disc_cash']);
$disc_kredit = isset($_GET['disc_kredit']);
For each of those variables, you are assigning the result of isset(), which will evaluate to either TRUE or FALSE. If you want to bind the actual value of your $_GET input, change each line from this syntax:
$idmt_salesarea = isset($_GET['salesarea']);
To
$idmt_salesarea = isset($_GET['salesarea']) ? $_GET['salesarea'] : '';
However this code isn't really maintainable, and I would also recommend using arrays instead of passing that many arguments to your saveAll() method.
In response to your update
If you are sending an AJAX request with type: "POST", you cannot access your input data via the PHP $_GET super global, you have to use $_POST. However, what I said before is still valid, as you aren't binding values to your variables properly.
Although I do not get what you are saying completely, still based on your description the problem can be you sending the variable using ajax call. If the ajax call is async you might not get the value by the time you use it.
So you can either set async:false in the ajax call (which is not a good way) or trigger any thing that uses that variable from within the success callback handler of the ajax call.
It would be really helpful if you can share the piece of code which explains the following statement of yours.... "i Take the input parameter from file called: index.php and pass the parameter using AJAX Jquery. The parameter itself is sent and pointing to file called insert.php"
I am using the $.getJSON jQuery function to allow my site to interact with a server at a different domain. However, I've been stuck fixing a bug which causes the third and fourth data item to be undefined. I’m not sure where the problem is.
JavaScript:
$.getJSON(domain_path + 'display.php?url=' + purl + '&callback=?', function(data) {
var username = data['uname'];
var point = data['point'];
var email = data['email'];
var title = data['title'];
});
The email and title are undefined, but uname and point are correct.
JSON is generated by this PHP:
$url = mysql_real_escape_string($_GET['url']);
$result = mysql_query("SELECT * FROM user url='$url'")or die(mysql_error());
$row = mysql_fetch_array($result);
if($row){
$uname = $row['uname'];
$point = $row['point'];
$email = $row['email'];
$title = $row['title'];
$output = array('uname'=>$uname,'point'=>$point,'email'=>$email,'title'=>$title);
$out_string = json_encode($output);
echo $callback.'('.$out_string.');';
}
JSON results look like this:
284927410({"uname":"john","point":"104","email":"john482#yahoo.com","title":"teacher"});
I am able to get john and 104, but email and title come up undefined when I put them in an alert().
Is there some rule I missed? Or is there something wrong in the code?
I reproduced your environment and everything is working fine using this code:
$.getJSON("http://localhost:8081/test", function(data) {
alert(data.uname);
alert(data.point);
alert(data.email);
alert(data.title);
});
Where the path http://localhost:8081/test returns only
{"point":"104","title":"teacher","email":"john482#yahoo.com","uname":"john"}