- 724 Posts
- 3 Reply Likes
Posted 4 years ago
Petra V., Champion
- 7794 Posts
- 1391 Reply Likes
But several issues are notable:
var image = $("#smallImage").val();
What does this variable contain after this statement?
$.post("postcomplaintinfo.php", {
We have discussed this before. The statement can never work as expected, because it points to a local file within your assets, not a file on your server.
if (!filter_var($emailphn2, FILTER_VALIDATE_EMAIL)) {
This validation is different from your client-side Regexp. Are you intentionally using different validations, or wouldn't you rather use the same client-side validation with a Regexp in your PHP script?
Also, if this field contains a valid phone number, the validation will return false, which is not what you want.
if (empty($_POST["complaintlist1"])) {
$complaintlistErr = "Please Chose Complaint type!";
if (empty($_POST["otherdescription1"])) {
$otherdescriptionErr = "Please Specify other complaint type!";
I thought you wanted either one, not both?
if($con)
echo $complainno;
else
echo "Insertion Failed";
In your php script, you have collected several error messages in various variables, like $nameErr, $statusErr, etc. Now, at the end of the script, you seem to ignore all those errors and just return a complaint number or an fixed error message.
Why? Wouldn't you rather return a json object with all relevant messages and values?
(I suggest you correct these issues (and test thoroughly) first, before going to the other script)
- 724 Posts
- 3 Reply Likes
Thanks to point out all those mistakes, yes some was in my knowledge that Err variable i didn't printed and some was not. Thank you I will correct them first.
- 724 Posts
- 3 Reply Likes
Means, How i can return multiple variable values from php? I also googled but not special result found. All depreciated posts.
When i am returning values of all the error variables, the same error printing on all the <div>..
Like Name is missing, in email and other fields.
and when i am doing like this:
So in one field all variable values are showing,
Just a little hint petra, that how i can print different variable's values in different <div> element with different ids?
function(data) {
$('#response').html(data);
$('#phpnameErr').html(data);
$('#confirmation').show();
.
.
data is holding all the php result. how i can show one variable values at one time?
I tried like this:
But it's wrong i think.
function(data) {
$('#response').html(data.complainno);
$('#phpnameErr').html(data.nameErr);
$('#confirmation').show();
sorry i felt shame to ask u again n again.... :(
Petra V., Champion
- 7794 Posts
- 1391 Reply Likes
At the server side, you create an array with the name/value pairs that you would like to keep. Then convert the array to JSON.
Something like:
$arr = array(
"errName"=>"The name is wrong",
"errPosition"=>"Position not numeric",
"complaintNo"=>3345529,
...);
$jsonOutput = json_encode($arr);See http://php.net/manual/en/function.json-encode.php
Then in the client, you loop through the JSON object and set the values wherever you like:
http://api.jquery.com/jquery.ajax/
Make sure both server and client are configured to work with JSON.
http://stackoverflow.com/questions/19155192/jquery-ajax-call-to-php-script-with-json-return
- 724 Posts
- 3 Reply Likes
- 724 Posts
- 3 Reply Likes
GOOD DAY
See I did like this:
But because I hard coded the error messages to array. It'ss showing all error messages when i m submitting the form.
SEe:
.
.
, function(data) { $('#phpnameErr').html(data.nameErr);
$('#phpemailErr').html(data.emailphnErr);
$('#phpemailformatErr').html(data.emailformatErr);
$('#phplocationErr').html(data.locationErr);
$('#phpcomplaintlistErr').html(data.complaintlistErr);
$('#phpotherdescriptionErr').html(data.otherdescriptionErr);
$('#response').html(data.complaintNo);
$('#confirmation').show();
$('#myForm')[0].reset();
},"json")
.fail(function() {
alert( "Posting failed." );
});
return false;
});
});
</script>
</body></html>

and here is php:
<?php$con = mysqli_connect("localhost","root","","amroclean");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$otherdescription2=$smallImage2=$personname2=$emailphn2=$locationtxt2=$complaintlist2=$complainno="";
$error_report = false;
$nameErr=$emailphnErr=$locationtxtErr=$complaintlistErr=$otherdescriptionErr=$imageErr="";
$arr = array('nameErr'=>'Please Enter Your Name!',
'emailphnErr'=>'Please Enter Your Email or Phone Number!',
'emailformatErr'=>'Please Enter Valid Email or Phone Number!',
'locationErr'=>'Please Enter location!',
'complaintlistErr'=>'Please Select Complaint Type',
'complaintNo'=>$complainno
);
$jsonOutput = json_encode($arr);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["personname1"])) {
$error_report = true;
}else {
$personname2 =test_input($_POST["personname1"]);
}
if (empty($_POST["emailphn1"])) {
$error_report = true;
}else {
$emailphn2 =test_input($_POST["emailphn1"]);
}
// check if e-mail or phone is well-formed
if (!preg_match('/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})|([7-9]{1}[0-9]{9})+$/',
$emailphn2)){
$error_report = true;
}
else {
$emailphn2 =test_input($_POST["emailphn1"]);
}
if (empty($_POST["locationtxt1"])) {
$error_report = true;
}
else {
$locationtxt2 =test_input($_POST["locationtxt1"]);
}
if (empty($_POST["complaintlist1"])) {
$error_report = true;
}else {
$complaintlist2 = test_input($_POST["complaintlist1"]);
}
if ($_POST["complaintlist1"]=="Other" && empty($_POST["otherdescription1"])) {
$error_report = true;
}else {
$otherdescription2 = test_input($_POST["otherdescription1"]);
} }
if($error_report != true) {
$complainno=mt_rand(10000000,99999999);
mysqli_query($con,"INSERT INTO `complaint` (`peresonname`,`emailphn`,`locationtxt`,`compltypeid`,`otherdescription`,`image`,`complainno`) values('$personname2','$emailphn2','$locationtxt2','$complaintlist2','$otherdescription2','$smallImage2','$complainno')");
}
if($con)
{
echo $jsonOutput;
}
else
{
echo "Insertion Failed";
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
mysqli_close($con);
?>
Now where I should use if-else construct? in serverside or client side?
Can i use like this:
in Client side?
, function(data) {
if($('#peresonname').val()=="" || $('#personname').val()==null)
{
$('#phpnameErr').html(data.nameErr);
}
if($('#emailphn').val()== "" || $('#emailphn').val()==null)
{
$('#phpemailErr').html(data.emailphnErr);
}
.
.
.
.
$('#confirmation').show();
$('#myForm')[0].reset();
},"json")
.fail(function() {
alert( "Posting failed." );
});
return false;
});
});
hmmm it's wrong because it's still client side validation right?
so
should i use like this?
if (empty($_POST["emailphn1"])) {
echo $arr['nameErr'];
}
at serverside
Petra V., Champion
- 7794 Posts
- 1391 Reply Likes
Here a couple of thoughts:
1.
echo "Failed to connect to MySQL: " . mysqli_connect_error();
Do you really want this to be echoed? What should the app do with this message, if it occurs?
Wouldn't YOU (the developer) rather receive such message, instead of the user?
2.
$jsonOutput = json_encode($arr);
You are echoing this $jsonOutput, which is the full set of error messages.
But you only want to output the error messages that apply. So, you should start with an empty output array:
$outArr = array();then add each error message to that array if applicable, for instance:
if (empty($_POST["personname1"])) {$error_report = true;
$outArr['emailphnErr'] = arr['emailphnErr'];
}Finally, use that array to create the JSON:
$jsonOutput = json_encode($outArr);
3.
if($('#peresonname').val()=="" || $('#personname').val()==null)
{
$('#phpnameErr').html(data.nameErr);
}
You should not check the field value client side, again. Once the server has done the validating, it supplies the appropriate error messages in the json object. All you need to do client side is:
- see if there is an applicable error message for that field
- display that error message.
4.
(about the screen shot)
Apparently, you are displaying the error messages inside the input field. That would be problematic if the field was merely incorrectly entered by the user.
For instance, if he typed a mail address like abc%mydomain.com, you would write "Please enter a valid Mail Address". But....how does the user now know what he did wrong? How can he correct his error? He can't even see what he entered anymore.
So, you should make sure that the already entered data are still visible when displaying an error message.
- 724 Posts
- 3 Reply Likes
Whatever you are, my brother, father, friend, Teacher, you are awesome. My eyes are wet now, and also dark circles to code aagain n agian. :D I don't kno hus is behind this bot, but really u r .....i havn't words, again thank you Petra, Salute to u.
- 724 Posts
- 3 Reply Likes
But I found that I am still stand there.
It's now headache.
You please just tell me that how i can loop json objects at my client side please, with an example.
Please......
see:
it's returning empty array like []
<?php$con = mysqli_connect("localhost","root","","amroclean");
// Check connection
if (mysqli_connect_errno())
{
echo "Error: OOPS!, there was an error in connection " . mysqli_connect_error();
}
$otherdescription2=$smallImage2=$personname2=$emailphn2=$locationtxt2=$complaintlist2=$complainno="";
$error_report=false;
$arr = array('nameErr'=>'Please Enter Your Name!',
'emailphnErr'=>'Please Enter Your Email or Phone Number!',
'emailformatErr'=>'Please Enter Valid Email or Phone Number!',
'locationErr'=>'Please Enter location!',
'complaintlistErr'=>'Please Select Complaint Type',
'complaintNo'=>$complainno
);
$outArr = array();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["personname1"])) {
$error_report = true;
$outArr['nameErr'] = $arr['nameErr'];
}
else {
$personname2 =test_input($_POST["personname1"]);
}
if (empty($_POST["emailphn1"])) {
$error_report = true;
$outArr['emailphnErr'] = $arr['emailphnErr'];
}else {
$emailphn2 =test_input($_POST["emailphn1"]);
}
// check if e-mail or phone is well-formed
if (!preg_match('/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})|([7-9]{1}[0-9]{9})+$/',$emailphn2)){
$error_report = true;
$outArr['emailphnformatErr'] = $arr['emailphnformatErr'];
}
else {
$emailphn2 =test_input($_POST["emailphn1"]);
}
if (empty($_POST["locationtxt1"])) {
$error_report = true;
$outArr['locationErr'] = $arr['locationErr'];
}
else {
$locationtxt2 =test_input($_POST["locationtxt1"]);
}
if (empty($_POST["complaintlist1"])) {
echo $arr['complaintlistErr'];
clear();
}else {
$complaintlist2 = test_input($_POST["complaintlist1"]);
}
if ($_POST["complaintlist1"]=="Other" && empty($_POST["otherdescription1"])) {
$error_report = true;
$outArr['otherdescriptionErr'] = $arr['otherdescriptionErr'];
}else {
$otherdescription2 = test_input($_POST["otherdescription1"]);
}
}
if($error_report!=true)
{
echo $jsonOutput = json_encode($outArr);
}
else
{
$complainno=mt_rand(10000000,99999999);
mysqli_query($con,"INSERT INTO `complaint` (`peresonname`,`emailphn`,`locationtxt`,`compltypeid`,`otherdescription`,`image`,`complainno`) values('$personname2','$emailphn2','$locationtxt2','$complaintlist2','$otherdescription2','$smallImage2','$complainno')");
}
if($con)
{
echo $complainno;
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
mysqli_close($con);
?>
and at form it's showing posting failed.
I tried by many type, but cudn't
Petra V., Champion
- 7794 Posts
- 1391 Reply Likes
So, the server isn't reached at all? Your PHP script isn't executed?
Well, then of course no JSON object is returned.
But on the other hand, you said that the full error message array was returned, so the server DOES return a JSON object. It's just the contents of that object that wasn't correct. Are you saying that the server is now returning nothing at all?
And also, this is not a JSON object:
echo $complainno;Steps to check in your app:
1. Display the data that are returned from the server
2. Check if the object structure is what you expect from that JSON object
3. If no object is returned, check at the end of your script (before anything is echo'ed) what it is about to send.
How to loop through a json object? See
http://stackoverflow.com/questions/20772417/how-to-loop-through-json-array-in-jquery
- 724 Posts
- 3 Reply Likes
I have change my script see:
<?php
$con = mysqli_connect("localhost","root","","amroclean");
// Check connection
if (mysqli_connect_errno())
{
echo "Error: OOPS!, there was an error in connection " . mysqli_connect_error();
}
$complainno=$complainno=mt_rand(10000000,99999999);
$dataArr=array('complaintNo'=>$complainno);
$empty_errors=array('nameErr'=>'Please Enter Your Name',
'emailphnErr'=>'Please Enter your Email or Phone Number',
'locationtxtErr'=>'Please enter location',
'complaintlistErr'=>'Select Complaint Type',
'otherdescriptionErr'=>'Please Specify other problem');
$exp_errors=array('emailphnformatErr'=>'You have entered an invalid Email or Phone Number');
$jsondataArr=json_encode($dataArr);
$jsonemptyErr=json_encode($empty_errors);
$jsonexpErr=json_encode($exp_errors);
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$personname2=mysqli_real_escape_string($con,$_POST['personname1']);
$emailphn2=mysqli_real_escape_string($con,$_POST['emailphn1']);
$locationtxt2=mysqli_real_escape_string($con,$_POST['locationtxt1']);
$complaintlist2=mysqli_real_escape_string($con,$_POST['complaintlist1']);
$otherdescription2=mysqli_real_escape_string($con,$_POST['otherdescription1']);
$smallImage2=$_POST['image1'];
$sql="INSERT INTO `complaint` (`peresonname`,`emailphn`,`locationtxt`,`compltypeid`,`otherdescription`,`image`,`complainno`) values('$personname2','$emailphn2','$locationtxt2','$complaintlist2','$otherdescription2','$smallImage2','$complainno')";
foreach($_POST as $val)
{
if(trim($val)=='' || empty($val))
{
echo $jsonemptyErr;
}
if (!preg_match('/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})|([7-9]{1}[0-9]{9})+$/',$emailphn2))
{
echo $jsonexpErr;
}
}
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
else
{
echo $jsondataArr;
}
}
else
{
echo "Insertion Failed";
}
mysqli_close($con);
?>
Can you tell me why it's showing Insertion failed when I am browsing it on browser?
And it's showing an alert of "Posting Failed".
, function(data) { $('#phpnameErr').html(data.nameErr);
$('#phpemailErr').html(data.emailphnErr);
$('#phpemailformatErr').html(data.emailphnformatErr);
$('#phplocationErr').html(data.locationErr);
$('#phpcomplaintlistErr').html(data.complaintlistErr);
$('#phpotherdescriptionErr').html(data.otherdescriptionErr);
$('#response').html(data.complaintNo);
$('#confirmation').show();
$('#myForm')[0].reset();
},"json")
.fail(function() {
alert( "Posting failed." );
});
return false;
});
});
Petra V., Champion
- 7794 Posts
- 1391 Reply Likes
And while validating: are you echo'ing every error message as a String immediately, without collecting them in the array $outArr first? After all, you want a json object with all applicable error messages, right?
Now, the part
else
{echo "Insertion Failed";
}is the else branch of the
if ($_SERVER["REQUEST_METHOD"] == "POST")so it seems your server script is called without anything being posted.
- 724 Posts
- 3 Reply Likes
How are you today?
Yes i was wrong i just saw a stckover flow code.
now see i again did old coding.
<?php$con = mysqli_connect("localhost","root","","amroclean");
// Check connection
if (mysqli_connect_errno())
{
echo "Connection Failed: " . mysqli_connect_error();
}
$validation=false;
$otherdescription2=$smallImage2=$personname2=$emailphn2=$locationtxt2=$complaintlist2="";
$complainno=mt_rand(10000000,99999999);
$arr=array('nameErr'=>'Name required!',
'emailphnErr'=>'Enter Email or phone Number',
'emailphnformatErr'=>'Enter valid Email or Phone Number',
'locationErr'=>'Enter location',
'complaintlistErr'=>'Select Complaint Type!',
'otherdescriptionErr'=>'Please Specify Other Problem!',
'complaintNo'=>$complainno
);
$OutArr=array();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["personname1"])) {
$validation=true;
$OutArr=$arr['nameErr'];
}else {
$personname2 =test_input($_POST["personname1"]);
}
if (empty($_POST["emailphn1"])) {
$validation=true;
$OutArr=$arr['emailphnErr'];
}else {
$emailphn2 =test_input($_POST["emailphn1"]);
// check if e-mail address is well-formed
if (!filter_var($emailphn2, FILTER_VALIDATE_EMAIL)) {
$validation=true;
$OutArr=$arr['emailphnformatErr'];
}
}
if (empty($_POST["locationtxt1"])) {
$validation=true;
$OutArr=$arr['locationErr']; }
else {
$locationtxt2 =test_input($_POST["locationtxt1"]);
}
if (empty($_POST["complaintlist1"])) {
$validation=true;
$OutArr=$arr['complaintlistErr'];
}else {
$complaintlist2 = test_input($_POST["complaintlist1"]);
}
if (empty($_POST["otherdescription1"]) && ($_POST["otherdescription1"]=="Other") ){
$validation=true;
$OutArr=$arr['otherdescriptionErr']; }else {
$otherdescription2 = test_input($_POST["otherdescription1"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if($validation!=true)
{
mysqli_query($con,"INSERT INTO `complaint` (`peresonname`,`emailphn`,`locationtxt`,`compltypeid`,`otherdescription`,`image`,`complainno`) values('$personname2','$emailphn2','$locationtxt2','$complaintlist2','$otherdescription2','$smallImage2','$complainno')");
}
else
{
json_encode($OutArr);
}
if($con)
echo json_encode($arr);
else
echo "Insertion Failed";
mysqli_close($con);
?>
in this coding $OutArr[] is not returning any data.
Just like:

petra please clear me client side cding for this, i hope server side is ok now.
after lots of trials, i coudn't printed json values properly.
Please Petra, i want o complete it soon now. It's not m not trying, Realy day and night foind this, but json data can't show according to conditions/
Please give some code hint, yes u did, u also linked coding, but all they are relation to any single array element returned i want to print many array elements retrurned from server but at dfferent conditions............
Petra V., Champion
- 7794 Posts
- 1391 Reply Likes
This will never become an array with a collection of strings, but rather just a single string.
$OutArr=$arr['nameErr'];
As a result, your json object will be empty.
But Saba, these are very simple things to test. All you need to do is add a display statement for $OutArr before it's being sent, run your script and see what the json object contains. If it is empty, you need to find the error server side, and not focus on the client.
- 724 Posts
- 3 Reply Likes
- 724 Posts
- 3 Reply Likes
Everything is working as I am expecting in both scripts, now what i want:
A Confirmation page, I have already created with confirmation message and complaintNo, certainly i can return complaintNo from json array.
But problem is that when i am hitting the submit button in the form whenever with it's empty fields or filled, it's showing confirmation page instantly.
How i prevent this(to showing confirmation page) untill all fields are not filled out?
json array is returning all values.
But quick page flip to another page e.g. confirmation page.
I tried with two following method:
$('#page2).hide();
$('#confirmation).show();
&
$( ":mobile-pagecontainer" ).pagecontainer( "change", "#confirmation", {transition:'flip' } );
Both are behaving same.
see:
function(data) { for(var i = 0; i < data.length; i++){
$("#" + data[i].id).show();
$("#" + data[i].id).html(data[i].message);
}
$('#page2).hide();
$('#confirmation).show();
},"json").fail(function() {
alert( "Posting failed." );
});
and also
7 days ago u wrote:
var image = $("#smallImage").val();
What does this variable contain after this statement?
so should i use
$('#smallImage').attr('src');
this one?
Petra V., Champion
- 7794 Posts
- 1391 Reply Likes
Secondly, look what you are doing:
function(data) {
for(var i = 0; i < data.length; i++){
$("#" + data[i].id).show();
$("#" + data[i].id).html(data[i].message);
}
In the above part, you get the datafields and appropriate error messages returned, and you place them in the html elements as identified by $("#" + data[i].id).
You are still in the form page, which is OK.
So, this will display the form again, with possible error messages.
You tell your DOM that the fields (as returned by your server) must be shown, but you don't say that anything must be hidden, so there will be no difference for the user.
Then you add this:
$('#page2).hide();
$('#confirmation).show();
It tells your script to hide the form (which you just filled with messages) and show a confirmation page (which might be a separate html document or a div within the form page).
This part will always be executed after receiving the post response, regardless of the result.
That's why you always see the confirmation page.
Instead, you should first check if error messages exist. If so, you should display the form with messages. If not, you should show the confirmation.
And then you have this:
.fail(function() {
alert( "Posting failed." );
Suppose you are a user and you receive this alert box, what would you do?
And is that what you, the developer, really want to happen?
Petra V., Champion
- 7794 Posts
- 1391 Reply Likes
var image = $("#smallImage").val();
What does this variable contain after this statement?
Have you checked what it contains?
(And: now that you have posted something, did the image data actually make it to your database? Can you retrieve and display such image?)
so should i use
$('#smallImage').attr('src');
No, I didn't say that. You should use what you need in yur database to be retrieved and displayed. If you are expecting base-64 data from your database, you should post them. If yu expect the base64-url from your database, yu should post that. And if yu want a file to be posted instead of base64 data, you need the file transfer plugin, not ajax.
So, you should take yur pick, then implement that throughout the whole client server process.
- 724 Posts
- 3 Reply Likes
- 724 Posts
- 3 Reply Likes
Can we make good friends :) although i am not able to be friend of adobe co. champion.
Petra V., Champion
- 7794 Posts
- 1391 Reply Likes
The picture indicates that an image element has been inserted into html, and the actual image could not be found online. So it must be either whitelisting or a wrong url.
Have you tried the whole app in a regular browser?
Have you tried retrieving and displaying the url from the img.src attribute after inserting it in your DOM?
- 724 Posts
- 3 Reply Likes
WHen I reset the APN settings as default. Image was showing :) :)
- 724 Posts
- 3 Reply Likes
Sorry, i am not ordering you, like always requesting you sir. with this busy schedule how i am creating this i really can't tell. With office full day duty. Mid night. sometime when i am so much tired, i sleep but when open my eyes, iquick open laptop and work..............
But Thank You after GOD. Because He make u interface for me to help.
And without irritating you are always here, i can't pay thanks Petra, :)
May GOD Bless YOu and your family
Is that Correct?
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown(e) {
e.preventDefault();
}Petra V., Champion
- 7794 Posts
- 1391 Reply Likes
The same way you are handling the back button at your main (index) page. Catch the 'backbutton' event. In this case, in the callback function, call the event.preventDefault() method.
- 724 Posts
- 3 Reply Likes
Petra V., Champion
- 7794 Posts
- 1391 Reply Likes
I hope you coded it in such a way that it works differently on index, because otherwise the back button would always dismiss your app.
- 724 Posts
- 3 Reply Likes
Petra V., Champion
- 7794 Posts
- 1391 Reply Likes
That function could contain a switch, evaluating the current page. In semi-code:
function myBackButton(e) {
if (currentpage is 'index') {exitApp();}
elseif (currentpage is 'offline') {e.preventDefault();}
else {}
}
- 724 Posts
- 3 Reply Likes
- 724 Posts
- 3 Reply Likes
<script>
$(document).ready(function() {
document.addEventListener("offline", onOffline, false);
document.addEventListener("online", onOnline, false);
document.addEventListener("backbutton", mybackButton, false);
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
if(navigator && navigator.network && navigator.network.connection && navigator.network.connection.type == Connection.NONE) {
$( ":mobile-pagecontainer" ).pagecontainer( "load", "offline.html", {transition:'flip' } );
}
var x = $('#maperror');
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError,{enableHighAccuracy: true, timeout: 20000});
}
else {
$("#maperror").text("Geolocation is not supported by this browser.");
}
function showPosition(position) {
$("#lat").val(position.coords.latitude);
$("#lon").val(position.coords.longitude);
var latlon =position.coords.latitude+","+position.coords.longitude;
var img_url = "http://maps.googleapis.com/maps/api/staticmap?mytimestamp="+new Date().getTime()+"¢er="+latlon+"&zoom=16&size=220x220&sensor=false&markers="+latlon+"&size:mid%7Ccolor:0xff0000%7Clabel:%7Camroha,+uttar+pradesh&visual_refresh=true";
$("#mapholder").html("<img src='"+img_url+"'>");
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
$("#maperror").text("User denied the request for Geolocation.");
break;
case error.POSITION_UNAVAILABLE:
$("#maperror").text("Location information is unavailable.");
break;
case error.TIMEOUT:
$("#maperror").text("Google Map couldn't loaded, Please reload the page to get user location");
break;
case error.UNKNOWN_ERROR:
$("#maperror").text("An unknown error occurred.");
break;
}
}
}
function mybackButton(e) {
if ($( ":mobile-pagecontainer" ).pagecontainer( "getActivePage" )[0].id=="page") {
exitApp();}
else if ($( ":mobile-pagecontainer" ).pagecontainer( "getActivePage" )[0].id=="offline") {e.preventDefault();}
}
function onOnline() {
// Only go back if user went to offline.html
if ($.mobile.activePage[0].baseURI.indexOf("offline.html") > -1) {
$.mobile.back();
}
}
function onOffline() {
$( ":mobile-pagecontainer" ).pagecontainer( "change", "offline.html", {transition:'flip' } );
}
});
</script>
Petra V., Champion
- 7794 Posts
- 1391 Reply Likes
Did you test it?
- 724 Posts
- 3 Reply Likes
Related Categories
-
PhoneGap Build
- 15111 Conversations
- 275 Followers
-
Plugins
- 1283 Conversations
- 38 Followers

