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.
Related
im trying to redirect a user to a url with a php variable $userdata
However the script seems not to work, it does put it in the html (confirmed with inspect element) but it does not redirect.
the url needs to bee
$first = $user['first'];;
$second= $user['second'];;
$userdata = "'.$first.'&hello='.$second.'";
echo "<script> window.location = 'example/login?userid='.$userdata.'' </script>";
Use json_encode() to convert the PHP string to a properly formatted JavaScript string. Than use JavaScript concatenation to combine it with the string in the <script>.
$first = $user['first'];;
$second= $user['second'];;
$userdata = "$first&hello=$second";
echo "<script> window.location = 'example/login?userid=' + " . json_encode($userdata) . ";</script>";
I'm new in programming and API and I want to know how can I redirect to the URL sent by an API Response like below, please take note that the merchantURL changes everytime a customer submit new transaction.
decision = ACCEPT
reasonCode = 100
requestID = 5199043764236716204011
requestToken = AhjnrwSTGdc55dvfMd/rmBles0iRokq1GiOZKW+XDR/x6Q27g++QyaSZbpAcCSAkxnXOeXb3zHf6wAAAAQqe
apSaleReply->reasonCode = 100
apSaleReply->merchantURL = https://www.sofort.com/payment/go/3e54e5e50b5114f5c11ef40b2d45dbb7a4d808b3
This is how I requested the merchantURL:
printf( "apSaleReply->merchantURL = " . $reply->apSaleReply->merchantURL . "<br>");
and I have an IF statement that if the reason code is equal to 100 then it will redirect to the URL:
if ($reply->reasonCode == 100){
?>
<script>
window.location.href = "";
</script>
<?php
}
This is already working if I input any URL, I'm only stuck on how to get the value of the merchantURL since it is always changing and specify it in the window.location.href.
Hope you can help me out! Thank you in advance!
You can use php header
header("Location: " . $reply->apSaleReply->merchantUR );
Or you can alter your existing code as below to get this work
if ($reply->reasonCode == 100){
?>
<script>
window.location.href = "<?php echo $reply->apSaleReply->merchantUR; ?>";
</script>
<?php
}
What about this?
window.location.href = "<?= $reply->apSaleReply->merchantURL ?>";
(I assume your template and back-end code is in the same file, otherwise use a session data. Let me know if it's the case.)
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 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"}
i am sending some value to a php file like this:
$.post('php/xxx.php', { username: userName } );
and the php file echo'es out a var $test
how can i get it back into the js?
i was thinking to use this:
var get_back = $.get('php/xxx.php', ... );
but i am not sure how...
thanks
edit:
here is my php file:
include("connect.php");
$get_username = $_POST['username'];
$username = '';
$username = mysql_real_escape_string($get_username);
echo $get_username;
$result = mysql_fetch_assoc(mysql_query("SELECT username FROM database WHERE username='$username' LIMIT 1"));
if($result !== FALSE) {
echo $username;
}
i want to get back this echo $username; value
The various ajax functions in jQuery (ajax, post, get) accept callback functions where you can handle the returned data:
//define username outside so that
//it's available outside of the scope
//of the callback function
var username;
$.post('php/xxx.php',
{ username: userName },
function(data){
//per the comment below,
//assuming data will simply
//be the username
username = data;
});