Jquery .live passes parameter twice in request - php

I am creating simple login screen which will accept username & password on click of submit button.I tried using .click but its not working at all so i am using .live but its passes same parameter twice in request.
$("#loginsubmit").live('click', function (e) {
$.ajax({
url: 'auth_check.php',
data: $(loginForm1).serialize(),
type: 'POST',
cache: false,
success: function (result) {
if (result == 'success') {}
},
error: function (result) {}
});
e.preventDefault();
$.prettyPhoto.close();
return false;
});
This is what i am getting in request
pusername=&ppassword=&pusername=abc&ppassword=abc
I am using jquery first time and there is something called pretty photo used in my template.
I guess this pretty photo might be causing issues.
LoginForm1 looks like as follows :
<div class="login_register_stuff hide"><!-- Login/Register Modal forms - hidded by default to be opened through modal -->
<div id="login_panel">
<div class="inner-container login-panel">
<h3 class="m_title">SIGN IN YOUR ACCOUNT TO HAVE ACCESS TO DIFFERENT FEATURES</h3>
<form name="loginForm1" method="post" enctype="multipart/form-data" />
CREATE ACCOUNT
<input type="text" name="pusername" class="inputbox" placeholder="Username" />
<input type="password" name="ppassword" class="inputbox" placeholder="Password" />
<input type="submit" id="loginsubmit" name="loginsubmit" value="LOG IN" />
</form>
<div class="links">FORGOT YOUR USERNAME? / FORGOT YOUR PASSWORD?</div>
</div>
prettyphoto section snippet
enter <!-- prettyphoto scripts & styles -->
function ppOpen(panel, width){
jQuery.prettyPhoto.close();
setTimeout(function() {
jQuery.fn.prettyPhoto({social_tools: false, deeplinking: false, show_title: false, default_width: width, theme:'pp_kalypso'});
jQuery.prettyPhoto.open(panel);
}, 300);
} // function to open different panel within the panel
jQuery(document).ready(function($) {
jQuery("a[data-rel^='prettyPhoto'], .prettyphoto_link").prettyPhoto({theme:'pp_kalypso',social_tools:false, deeplinking:false});
jQuery("a[rel^='prettyPhoto']").prettyPhoto({theme:'pp_kalypso'});
jQuery("a[data-rel^='prettyPhoto[login_panel]']").prettyPhoto({theme:'pp_kalypso', default_width:800, social_tools:false, deeplinking:false});
jQuery(".prettyPhoto_transparent").click(function(e){
e.preventDefault();
jQuery.fn.prettyPhoto({social_tools: false, deeplinking: false, show_title: false, default_width: 980, theme:'pp_kalypso transparent', opacity: 0.95});
jQuery.prettyPhoto.open($(this).attr('href'),'','');
});
});
here
could you please help me to find out the issue.

This is, because prettyphoto clones your inline content by opening. After opening your inline content, you have to clear the original content - otherwise you've got everything twice - and if you close prettyphoto, you have to put it back there, if its not dynamic generated.
Example:
$.fn.prettyPhoto({
ajaxcallback:function(){
$("#yourhiddenformid").html("");
},
callback:function(){
no need, if its dynamic generated, otherwise:
rewrite html of div;
}

You have to bind onsubmit event on form:
$("#loginForm1").on('submit', function (e) {
$.ajax({
url: 'auth_check.php',
data: $('#loginForm1').serialize(),
type: 'POST',
cache: false,
success: function (result) {
if (result == 'success') {}
},
error: function (result) {}
});
e.preventDefault();
$.prettyPhoto.close();
return false;
});

Related

Uploading File Images with AJAX and PHP

I am attempting to create an upload document which will upload a profile picture. I am having trouble capturing the data from the changePicture form which only has an input for the images and a submit. I have never used the FormData object to date, so I am still learning around this. Any guidance would be useful.
See my HTML
<form id="changePicture" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="name">Update Your Profile Picture</label>
<input type="file" id="profilePic" class="form-control" name="profilePic">
<input type="hidden" value="<?php echo $id; ?>" name="id">
</div>
<button type="submit" id="updateProfileBtn" class="btn btn-success float-right">Upload Image</button>
</form>
Here is my AJAX code:
function updateProfilePic(){
$("#changePicture").on("submit", function(e) {
e.preventDefault();
$("#spinner").show();
setTimeout(function(){
$.ajax({
url: "../ajax/admin/updateProfilePic.php",
type: "POST",
data: new FormData(this),
cache: false,
contentType: false,
processData: false,
success: function(result){
$("#spinner").hide();
$("#changePicture").append(result);
setTimeout(function(){
$("#changePicture").slideDown();
}, 1500);
}
});
}, 3000);
});
}
The PHP file at this moment only echos out "Working" to see it accesses the page alright, which it does. However, when I attempt to locate the file through a variable nothing has been sent and returns undefined index.
this will be undefined because it's inside ajax's scope
Try:
me = this;
$.ajax({
url: "../ajax/admin/updateProfilePic.php",
type: "POST",
data: new FormData(me),
...
As, for me, when using ajax I always prefer to base64encode the image and pass it as a JSON to PHP but i guess that's a personal preference...
Why are you wrapping your AJAX call in a
setTimeout(function() {..})
?
By doing this, you cannot simply write new FormData(this), because the this context does not refer to the form that you are looking for.
Try executing the code without the timeout, or try storing the form data in a global variable
Edit: Example added:
var myFormData;
function updateProfilePic(){
$("#changePicture").on("submit", function(e) {
e.preventDefault();
$("#spinner").show();
myFormData = new FormData(this);
setTimeout(function(){
$.ajax({
url: "../ajax/admin/updateProfilePic.php",
type: "POST",
data: myFormData,
....

Return false not working in my jquery ajax post, it sends to my php file

I'm sorry everyone.. :( my bad I actually didn't include the jquery library...
Sorry for that :3 i'm just very exhausted right now.
can someone tell me what's wrong and/or lack of my codes? returning false is not working.... is there any conflict in the codes below?...
html
<form class="login-form" method="POST" action="loginauthenticate.php">
<fieldset>
<h3>WELCOME!</h3>
<div id="get_content"></div>
<p>
<input class="username" type="text" name="username" placeholder="Username" autocomplete="off" required/>
<br>
<input class="password" type="password" name="password" placeholder="Password" required/>
<br>
</p>
<input id="register" class="login-button" type="submit" value="Log In" />
</fieldset>
</form>
<footer>
<P>© 2016 ACT Students,Thesis</P>
</footer>
js
<script type="text/javascript">
$(document).ready(function() {
$('form').submit(function() {
$.ajax({
data: $(this).serialize(),
type: $(this).attr('method'),
url: $(this).attr('action'),
success: function(data) {
$('#get_content').html(data);
}
});
return false;
});
});
</script>
This works for preventing default and Ajax also fires. Try it again.
<script type="text/javascript">
$(document).ready(function() {
$('form').submit(function() {
$.ajax({
data: $(this).serialize(),
type: $(this).attr('method'),
url: $(this).attr('action'),
success: function(data) {
$('#get_content').html(data);
}
});
return false;
});
});
</script>
You need to use event.preventDefault() and add event variable in submit(function(event). The event parameter comes from the event binding function. This is the standart method that stops the default action of an element from happening. In oter words if this method is called, the default action of the event will not be triggered.
return false; does 3 separate things when you call it :
1.event.preventDefault(); – It stops the browsers default behaviour.
2.event.stopPropagation(); – It prevents the event from propagating (or “bubbling up”) the DOM.
3.Stops callback execution and returns immediately when called.
preventDefault(); does one thing: It stops the browsers default behaviour.
So when to use them? Simply it depends on what you want to accomplish. Use preventDefault(); if you want to “just” prevent the default browser behaviour. Use return false; when you want to prevent the default browser behaviour and prevent the event from propagating the DOM. In most situations where you would use return false; what you really want is preventDefault().
<script type="text/javascript">
$(document).ready(function() {
$('form').submit(function(event) {
event.preventDefault();
$.ajax({
data: $(this).serialize(),
type: $(this).attr('method'),
url: $(this).attr('action'),
success: function(data) {
$('#get_content').html(data);
}
});
});
});
</script>
try to put return false before ajax work
$('form').submit(function() {
return false;
$.ajax({
data: $(this).serialize(),
type: $(this).attr('method'),
url: $(this).attr('action'),
success: function(data) {
$('#get_content').html(data);
}
});
});

Jquery Mobile redirect with anchor in IE

My code is running on jquery mobile 1.4.5. When i submit the form. The website will make a call to login.php and if it succeeds it will redirect to the #customer anchor. If not it will redirect to the #error popout.
The problem is that this works as expected on chrome and firefox however it does not work on desktop IE11.
$(document).ready(function() {
$('#loginform').submit(function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: 'login.php',
data: $(this).serialize(),
success: function(data) {
if (data === 'login') {
window.location = '#customer';
} else {
window.location = '#error';
}
}
});
});
});
<form id="loginform" method="post">
<div class="ui-field-contain">
<label for="storeID">Store ID:</label>
<input type="text" name="usn" id="usn">
<label for="password">Password:</label>
<input type="password" name="upw" id="upw">
</div>
<input type="submit" Value="Login" />
</form>
JQM has its own widget for navigating to Pages. It loads pages using Ajax into the Dom or once loaded into the Dom it directs to that Page. The Widget used is called Pagecontainer using Change Method.
Now i dont know about IE11, never used it, but per JQM user guide the following way is the one you need to use.
http://api.jquerymobile.com/pagecontainer/
(page transition in the example is set to none) you can choose one from here --http://demos.jquerymobile.com/1.4.5/transitions/
$( ":mobile-pagecontainer" ).pagecontainer( "change", "#customer", { transition: "none" })
if you want a reverse transition, ie going the other way round use reverse true
$( ":mobile-pagecontainer" ).pagecontainer( "change", "#customer", { transition: "none", reverse: true })
For the error dialog JQM has a widget for that too.
http://api.jquerymobile.com/dialog/
To Open the error dialog
$.mobile.changePage( "#error", { role: "dialog" } );

Ajax file upload inside modal

I have no idea how to implement this, let me try to explain
I have a form that open inside a bootstrap modal frame in my layout, in this form i have 2 fields
<input type="file" name="photoimg" id="photoimg" value="" />
<input type="hidden" name="consulta" value="5">
I need to submit this to my url with the value 5 for 'consulta' so the script can read and do the proper things with the file,
BUT
I need to do this without refreshing the opened modal, in other words, using ajax to submit the file (for further cropping)
i have this script that do the submit, what i'm doing wrong here?
<script type="text/javascript">
function enviaimagem(){
$.ajax({
type: "POST",
data: { consulta:5 },
dataType: 'html',
url: "<?=JURI::ROOT()?>ajax.html",
//dataType: "html",
success: function(result){
$("#corpo_modal").html('');
$("#corpo_modal").html(result);
},
beforeSend: function(){
$("#corpo_modal").html('');
$("#corpo_modal").css({display:"none"});
$('#ajaxloadergeneric').css({display:"block"});
},
complete: function(msg){
$('#ajaxloadergeneric').css({display:"none"});
$("#corpo_modal").css({display:"block"});
}
});
}
</script>
File upload through AJAX is supported through FormData object:
https://developer.mozilla.org/en-US/docs/Web/Guide/Using_FormData_Objects
, however it is not supported by all/old browsers( especially IE < 10)

jQuery Ajax post for newbie

First let me say I'm new to Ajax. I've been reading articles from jquery.com and some tutorials but I didn't figured it out yet how this works on what I'm trying to achieve.
I am trying to get the weather for a searched city using Google's Weather API XML, without page refresh.
I managed to retrieve the Weather XML and parse the data but everytime I search for a different place, the page reloads since my weather widget is under a tab.
This is what I have in my HTML:
<script type="text/javascript">
$(document).ready(function(){
// FOR THE TAB
$('.tab_btn').live('click', function (e) {
$('.tab_content').fadeIn();
});
$(".submit").click(function(){
$.ajax({
type : 'post',
url:"weather.php",
datatype: "text",
aysnc:false,
success:function(result){
$(".wedata").html(result);
}});
});
});
</script>
<style>.tab_content{display:none;}</style>
</head><body>
<input type="button" value="Show Content" class="tab_btn">
<div class="tab_content">
<h2>Weather</h2>
<form id="searchform" onKeyPress="return submitenter(this,event)" method="get"/>
<input type="search" placeholder="City" name="city">
<input type="hidden" placeholder="Language" name="lang">
<input type="submit" value="search" class="submit" style="width:100px">
</form>
<div id="weather" class="wedata">
</div>
</div>
And here is the actual demo I'm working on: http://downloadlive.org.
Now, if I add action="weather.php" on the search form I get the results, but I get redirected to weather.php which is logical. Without the action="weather.php", everytime I search my index which I'm on, adds up /?city=CITY+NAME which shouldn't. This should be added to weather.php, get the results and then retrieve them back into my index, if that makes sense?
This is my php code for weather.php: http://pastebin.com/aidXCeQg
which can be viewed here: http://downloadlive.org/weather.php
Can someone please help me out with this please?
Thanks alot
You just need to return false; from the click event handler. This will prevent the default action from occuring - in this case, submitting the form. Also, remove the async: false setting. You almost never want synchronous ajax requests.
$(".submit").click(function(){
$.ajax({
type : 'post',
url:"weather.php",
datatype: "text",
success: function(result){
$(".wedata").html(result);
}
});
return false;
});
Alternately you can pass a parameter name to the callback and then use event.preventDefault() to accomplish the same result as above:
$(".submit").click(function(e){
$.ajax({
type : 'post',
url:"weather.php",
datatype: "text",
success: function(result){
$(".wedata").html(result);
}
});
e.preventDefault();
});
You need to send the form data with the POST. It's super-easy to do this using .serialize().
$(".submit").click(function(){
$.ajax({
type : 'post',
url:"weather.php",
data: $(this.form).serialize(),
datatype: "text",
success: function(result){
$(".wedata").html(result);
}
});
return false;
});

Categories