User's facebook details not being received into hidden fields - php

I'm using the facebook javascript sdk to get a logged in user's public data. What I want to do is bind the values to hidden field in the form and when submitted, get the values in a php file which will insert into MySQL database. But when I submit the form, there are no values. Why is that? If I need to provide more info, please let me know. When I login, I can see my facebook name and profile pic on the web app.
//Hidden fields within form
<input type="hidden" name="hdnFacebookId" id="hdnFacebookId" value="">
<input type="hidden" name="hdnUsername" id="hdnUsername" value="">
<input type="hidden" name="hdnFirstName" id="hdnFirstName" value="">
<input type="hidden" name="hdnLastName" id="hdnLastName" value="">
<input type="hidden" name="hdnFacebookImg" id="hdnFacebookImg" value="">
<input type="hidden" name="hdnFacebookUrl" id="hdnFacebookUrl" value="">
<br/>
<input type="submit" id="submit" onclick="setValues()" data-theme="b" value="Submit" class="ui-btn-hidden" aria-disabled="false">
<script>
function updateUserInfo(response) {
FB.api('/me', function(response) {
document.getElementById('myFacebookImg').src = "https://graph.facebook.com/" + response.id + "/picture";
document.getElementById('myFacebookName').innerHTML = response.name;
function setValues(){
document.postAJourneyForm.hdnFacebookId.value = response.id;
document.postAJourneyForm.hdnUsername.value = response.username;
document.postAJourneyForm.hdnFirstName.value = response.first_name;
document.postAJourneyForm.hdnLastName.value = response.last_name;
document.postAJourneyForm.hdnFacebookImg.value = "https://graph.facebook.com/" + response.id + "/picture";
document.postAJourneyForm.hdnFacebookUrl.value = "http://graph.facebook.com/" + response.username;
//document.forms["postAJourneyForm"].submit();
}
});
}
//php file that gets the data to post to database
//I check in the code if it is empty before I can execute the query
//but it is always empty
//get the facebook data
$hdnFacebookId = $_POST['hdnFacebookId'];
$hdnUsername = $_POST['hdnUsername'];
$hdnFirstName = $_POST['hdnFirstName'];
$hdnLastName = $_POST['hdnLastName'];
$hdnFacebookImg = $_POST['hdnFacebookImg'];
$hdnFacebookUrl = $_POST['hdnFacebookUrl'];
//Updated with JSON
<script>
function updateUserInfo(response) {
FB.api('/me', function(response) {
document.getElementById('myFacebookImg').src = "https://graph.facebook.com/" + response.id + "/picture";
document.getElementById('myFacebookName').innerHTML = response.name;
document.getElementById('facebookUrl').href = "https://www.facebook.com/" + response.username;
var JSONObject = {
"facebookId":response.id,
"facebookName":response.username,
"facebookFirstName":response.first_name,
"facebookLastName":response.last_name
};
document.getElementById('hdnFacebookId').innerHTML = JSONObject.facebookId;
document.getElementById('hdnUsername').innerHTML = JSONObject.facebookName;
document.getElementById('hdnFirstName').innerHTML = JSONObject.facebookFirstName;
document.getElementById('hdnLastName').innerHTML = JSONObject.facebookLastName;
document.getElementById('hdnFacebookImg').innerHTML = JSONObject.facebookImage;
document.getElementById('hdnFacebookUrl').innerHTML = JSONObject.facebookUrl;
});
}
</script>

To set values.
$('#hdnFacebookId').val(response.id);
$('#hdnUsername').val(response.username);
$('#hdnFirstName').val(response.first_name);
$('#hdnLastName').val(response.last_name);
$('#hdnFacebookImg').val('https://graph.facebook.com/' + response.id + '/picture');
$('#hdnFacebookUrl').val('http://graph.facebook.com/' + response.username);

Related

login to website using PHP or another language and password_hash

I tried to log in with my username and my password on the site by doing it through my site, but I encounter the following difficulty, which obviously prevents me from using, for example, file_get_contents.
<script language="JavaScript">
<!--
function hash_password( form ) {
var login = form.elements['username'].value;
var passwd_md5 = hex_md5(form.elements['password'].value);
var passwd_hash = hex_md5(passwd_md5 + '66446' + login);
// uc = navigator.appCodeName + ' ' + navigator.appVersion;
// Set up redirect url
var url = '/login.php?';
var new_url = 'login.php';
// new_url += '?goto=' + escape(url);
new_url += '?login=' + escape(login);
new_url += '&pass=' + escape(passwd_hash);
// new_url += '&uc=' + escape(uc);
// Submit the form
window.location.href = new_url;
// Ignore default submit
return false;
}
// -->
</script>
The login form is this
<div class="LoginForm">
<form onsubmit="return hash_password(this)" id="login" class="login">
<div class="ttl">Вход</div>
<label>
<span>Потребител</span>
<input type="text" name="username">
</label>
<label>
<span>Парола</span>
<input type="password" name="password">
</label>
<input type="submit" value="Вход" class="ui-state-default ui-corner-all">
</form>
</div>
Uses the get method because toggle httpfox does not show a post method. If anyone can help with something I'm open to suggestions

Passing values without keys in the url when submitting a form

My form has 2 textboxes and 1 button.
When submitting my form, I'd like the url to be similar to this www.differentdomain.com/aa00/bb00 instead of www.differentdomain.com/?q1=aa00&q2=bb00
Here is my html code:
<form action="https://differentdomain.com/" id="myform" class="fs-form fs-form-full" autocomplete="off">
<input class="fs-anim-lower" id="q1" type="text" placeholder="aa00" required/>
<input class="fs-anim-lower" id="q2" type="text" placeholder="bb00" required/>
<button id="submit" class="fs-submit" type="submit">see result</button>
</form>
If I understood the question right, you can do something like this using jQuery:
$('form#myform').submit(function (e) {
e.preventDefault();
var urlData = '';
// order is very important so that you can recognize each value
urlData += '/' + $(this).find('#q1').val();
urlData += '/' + $(this).find('#q2').val();
$.get(domain + urlData, function (data) { alert('success'); });
});
Hope that helps
Simple solution would be to change form action every time its fields change:
document.querySelector('#myform').addEventListener('input', function () {
this.action = this.q1.value + '/' + this.q2.value
})

How to send data from one page to another without page load?

I have two page one customform.php and another is preview.php.
I want to send some data which are values of some text-fields on customform.php using jquery post method. But I dont want the page to load. So I have used some JQuery code for this work. But now working for me.
the html code on customform.php is:
<form action="#" method="post">
<input type="text" value="" id="text1">
<input type="text" value="" id="text2">
<input type="button" value="preview" id="preview">
</form>
The jQuery code on customform.php is:
$('#previewph').click( function() {
var v1 = $.('#text1').val();
var v2 = $.('#text2').val();
alert("Mail: " + v1 + " Message: " + v2);
$.post( "http://www.lexiconofsustainability.com/lex_tmp2/preview/" ,{ name : v1 , title :v2 });
window.open("http://www.lexiconofsustainability.com/lex_tmp2/preview/", '_blank');
});
And on the preview.php I want to retrieve value form post method and echo them.
<?php
echo $authorname = $_POST['name'];
echo $posttitle = $_POST['title'];
?>
Simply Try this
<form action="#" method="post" >
<input type="text" value="" id="text1">
<input type="text" value="" id="text2">
<input type="submit" value="preview" id="preview" onclick="senddata()" >
</form>
<div id="message"></div>
function senddata() {
var v1 = $.('#text1').val();
var v2 = $.('#text2').val();
$.post("http://www.lexiconofsustainability.com/lex_tmp2/preview/", { name : v1 , title :v2 },
function(data) {
document.getElementById("message").innerHTML = data;
});
}
You can use AJAX for this..
The jQuery code on customform.php should be:
$('#previewph').click( function() {
var v1 = $('#text1').val();
var v2 = $('#text2').val();
$.post("preview.php", {name:v1, title:v2}, function(data)
{
alert(data);
});
});
preview.php
<?php
if(isset($_POST['name'])&&($_POST['title']))
{
echo $authorname = $_POST['name'];
echo $posttitle = $_POST['title'];
}
?>

ajax request hidden fields

I'm developing a site (only for fun and learn programming with jquery)
and i'd like to know what's wrong with this :
$(window).unload(function(){
var myid = $('input#v1').attr('value'); // hidden
var playauth = $('input#v2').attr('value'); // hidden
var srvid = $('input#v3').attr('value'); // hidden
var result = 'myid='+ myid +'&auth='+ playauth +'&srvid='+ srvid;
$.ajax({
type: "GET",
data: result,
url: "closing.php",
complete: function(data) {
alert(data.responseText);
}
});
});
I'm trying to update a database table. When i close the window nothing happens.
With a previous version of this function :
window.onunload = function () {
var xhReq = new XMLHttpRequest();
var n = document.getElementById("v1").InnerHTML;
var o = document.getElementById("v2").InnerHTML;
var p = document.getElementById("v3").InnerHTML;
xhReq.open("GET", ("closing.php?myid=" + n + "&auth=" + o + "&srvid=" + p) , false);
xhReq.send(null);
var serverResponse = xhReq.responseText;
alert(serverResponse);
};
.. i saw the response alert but GET values were 'undefined'.
.... probably because the type of inputs is hidden..?
This is my form... maybe i miss something ?? I'm really new to jquery/ajax .. please help!!
<form method="get">
<input id="v1" type="hidden" name="val1" class="aget" value="<?php echo $_GET['myid']; ?>" />
<input id="v2" type="hidden" name="val2" class="bget" value="<?php echo $_GET['playauth']; ?>" />
<input id="v3" type="hidden" name="val3" class="cget" value="<?php echo $_SESSION['srvid']; ?>" />
</form>
change
var myid = $('input#v1').attr('value'); // hidden
var playauth = $('input#v2').attr('value'); // hidden
var srvid = $('input#v3').attr('value'); // hidden
to
var myid = $('input#v1').val(); // hidden
var playauth = $('input#v2').val(); // hidden
var srvid = $('input#v3').val(); // hidden
You must use
.val(); instead of .attr('value');

Adding multiple rows to database from dynamic field event

I have a dynamic event in JS in my form which adds another block of fields so my users can add another address:
<script type="text/javascript">
$(document).ready(function() {
$('#btnAdd').click(function() {
var $address = $('#address');
var num = $('.clonedAddress').length;
var newNum = new Number(num + 1);
var newElem = $address.clone().attr('id',
'address' + newNum).addClass('clonedAddress');
//set all div id's and the input id's
newElem.children('div').each (function (i) {
this.id = 'input' + (newNum*11 + i);
});
newElem.find('input').each (function () {
this.id = this.id + newNum;
this.name = this.name + newNum;
});
if (num > 0) {
$('.clonedAddress:last').after(newElem);
} else {
$address.after(newElem);
}
$('#btnDel').removeAttr('disabled');
if (newNum == 3) $('#btnAdd').attr('disabled', 'disabled');
});
$('#btnDel').click(function() {
$('.clonedAddress:last').remove();
$('#btnAdd').removeAttr('disabled');
if ($('.clonedAddress').length == 0) {
$('#btnDel').attr('disabled', 'disabled');
}
});
$('#btnDel').attr('disabled', 'disabled');
});
</script>
However, when I put my form action the page just refreshes when I click my 'add another address' button:
<form action="address.php" method="post" name="regForm" id="regForm" >
These are my fields:
if(empty($err)) {
for($i = 0; $i < 10; $i++)
{
$Street = $_POST['Street'][$i];
$Line2 = $_POST['Line2'][$i];
$Line3 = $_POST['Line3'][$i];
$Town = $_POST['Town'][$i];
$Postcode = $_POST['Postcode'][$i];
$Country = $_POST['Country'][$i];
$Tele = $_POST['Tele'][$i];
$Fax = $_POST['Fax'][$i];
$Type = $_POST['Type'][$i];
$Mobile = $_POST['Mobile'][$i];
$sql_insert = "INSERT into `address`
(`Street`,`Line2`,`Line3`,`Town`, `Postcode` ,`Country`,`Tele`,`Fax`,`Type`
,`Mobile` )
VALUES
('$Street','$Line2','$Line3','$Town','$Postcode','$Country',
'$Tele','$Fax','$Type', '$Mobile'
)";
mysql_query($sql_insert,$link) or die("Insertion Failed:" . mysql_error());
}
I want all addresses to go to mysql database.
I hope this is clear
Define buttons as followed: <input type="button" value="ButtonLabel" />.
My short test resulted in my <button> getting treated as submit type input by firefox. This means <button>FooBar</button> and <input type="submit" value="FooBar" /> are equivalent.
You might also want to simplify your javascript code. You can use the array notation for input names:
<input type="text" name="street[]" />
<input type="text" name="zip[]" />
<input type="text" name="street[]" />
<input type="text" name="zip[]" />
will result in $_POST["street"][0] and $_POST["street"][1] beeing filled with the user's input. This is what you want judging from your php code, anyway.
You don't need ids for all your input tags. Just keep one full set of inputs for one address and append this to your form. Maybe something like:
$('#address').append(
'<div>' +
'<input type="text" name="street[]" />' +
'<input type="text" name="zip[]" />' +
'</div>'
);
Or just have a full set hidden on your page and clone it, then append. I'm sure our jQuery pros will know a better solution.
And finally: Please sanatize your input with mysql_real_escape_string
$Street = mysql_real_escape_string($_POST['Street'][$i]);
// and so on for the other values.

Categories