I have the code below for Java Script on getwifi1.php
$(function(){
$('#wifi-avail').on('click', 'td', function(){
var txt;
var user;
var pass = prompt("Please enter your password:", "");
if (pass == null || pass == "") {
txt = "User cancelled the prompt.";
}
user = $(this).html();
$.post(
"newone.php",
{
"username":user,
"password":pass
}
)
alert(user);
alert(pass);
}); //end inner function
}); //end outer function
I want to transfer the value of user and pass to another php file named newone.php in which I have written this code
<?php
$name1 = $_POST['username'];
$name2 = $_POST['password'];
echo $name1;
echo $name2;
?>
but the $name1 and $name2 seems to be empty always. I don't know what I am doing wrong. Can anyone correct my method or write me another code to transfer these two variables from getwifi1.php to newone.php. I have search ajax method which I applied like this but it didn't seem to work either
$.ajax({
method: "post",
url: "newone.php",
data: {username:user, password:pass}
})
ThankYou
See http://api.jquery.com/jquery.ajax/
Type: An alias for method. You should use type if you're using versions of
jQuery prior to 1.9.0.
I'm not sure, but I think you have to change 'method' to 'type' in your Ajax call.
My bad, this is not true and necessary.
This script should be working (and alert any error messages from your newone.php):
$.ajax({
type: 'POST',
url: 'newone.php',
data: {
username: user,
password: password
},
success:function(data){
alert(data);
}
});
http://api.jquery.com/jquery.ajax/
Related
i'm really stuck in ajax,
i don't have problem with simply sending values using form tag to PHP file.
but i want to make my program a bit more fancy using jquery and ajax.
and i'm totally stuck for few days.
what i'm trying to do is when i click on some text (study progress in my code), it turns into an editing box
and when i finish editing, it sends to variable "name" and update in PHP.
so i have to somehow send the value in the editing box in ajax,
but i keep having
" Undefined index: name "
i searched on google a lot, but i couldn't find the answer for me.
can i get some help from here?
i want to put the values of what i edited to the variable name ;
and want to check on server side.
how can i put the values of what i edited to variable name and send
to php in POST and check on server?
here is code that i made so far.
======HTML======
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/.../jquery/3.2.0/jquery.min.js"></script>
<div id="fullname">study progress</div>
<script>
$('#fullname').click(function(){
var name = $(this).text();
$(this).html('');
$('<input></input>')
.attr({
'type': 'text',
'name': 'fname',
'id': 'txt_fullname',
'size': '50',
'value': name
})
.appendTo('#fullname');
$("#txt_fullname").focus().val("").val(name);
});
$(document).on('blur','#txt_fullname', function(){
var name = $(this).val();
$.ajax({
type: 'POST',
url: 'practice_check.php',
data : { "name" : name },
success: function(){
$('#fullname').text(name);
alert(name);
location.replace("practice_check.php");
}
});
});
</script>
======PHP ( practice_check.php ) ======
<?php
$name = $_POST["name"];
echo $name;
?>
You were wrong. You need to specify the url and if you want to get data in PHP you need to add data. I made a simple code. You can check full detail at this link : http://api.jquery.com/jquery.ajax/
data
Type: PlainObject or String or Array
Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting
var name = 'abc';
$.ajax({
url: 'practice_check.php',
type: 'POST',
data: {name: name},
})
.done(function() {
console.log("success");
})
.fail(function() {
console.log("error");
})
.always(function() {
console.log("complete");
});
first your ajax should be like this
$.ajax({
type: 'POST',
url: 'practice_check.php',
data : { "name" : name },
dataType: 'JSON',
success: function(response){
//response will be the output of practice_check.php
$('#fullname').text(name);
alert(response.data);
}
});
in you practice_check.php
$name = $_POST["name"];
// do your functions here .. then echo response like for example
if(strlen($name) < 3){ echo "oops invalid name"; }
else { echo "successfully checked with no errors"; }
Can someone please show me the correct way I can add these two lines of code
data: {name: info, me: '<?php echo $me; ?>'},
data: dataString ,
So that I can send them in a $_POST to my action.php , I have tried several ways to do this but cannot get both of them successfully to be passed on to my action_comments.php I understand I'm missing something possible when using data: below or have not correctly formatted my code . I'm a total beginner with very little experience so sorry if I lack good practice but hopefully can be better from my debugging . Thanks to anyone who helps me get passed this .
Here is complete code to give overview what Im doing
<script type="text/javascript">
$(function() {
// call the submit button ref: name
$(".submit_button").click(function() {
declare textcontent
var textcontent = $("#content").val();
//dataString = 'content' globel var
var dataString = 'content='+ textcontent;
declare info
var info = $('#name').val();
// option no text inputted
if(textcontent=='')
{
// show message if no text
alert("Enter some text..");
$("#content").focus();
}
else
{
//display text animation loading
$("#flash").show();
$("#flash").fadeIn(400).html('<span class="load">Loading..</span>');
//var info equals the string
var info = $('#content').val();
//start ajax call
$.ajax({
type: "POST",
//path to my action.php
url: "actions/action_comment.php",
//Need to undestand how to correctly format these lines so
//they are both succesful when submitted to my action_comment.php
$me is declared (not-shown here it holds integer)
data: {name: info, me: '<?php echo $me; ?>'},
// pass the string from textarea to $_POST
data: dataString ,
// I can get one or the other to work but not both
cache: true,
// feed the success my data
success: function(html){
$("#show").after(html);
document.getElementById('content').value='';
$("#flash").hide();
$("#content").focus();
}
});
}
return false;
});
});
</script>
I have my $_POST as follows in action_comment.php
echo $me = $_POST['me'];
//DATASTRING FROM TEXTAREA
echo $content= $_POST['content'];
var dataString = 'content='+ textcontent;
$.ajax({
type: "POST",
url: "actions/action_comment.php",
data: {name: info, me: '<?php echo $me; ?>',txt_data: dataString},
....
});
Cannot use data attribute multiple times in same ajax request. Within php file you can access like $_POST['txt_data'] to get textarea content and same way for other parameters;
define data attribute once and pass all the data like as shown above.
if you want to post whole form data you can use this way
var form = $('#my_form');
$.ajax( {
type: "POST",
url: form.attr( 'action' ),
data: form.serialize(),
..
..
});
I am very new to ajax.
What I am trying to do here is bringing back some variables from a PHP file that I've wrote mainly to process a HTML form data into MySql db table.
After some research I concluded that I need to use json (first time) and I must add the part dataType:'json' to my ajax.
My problem is that after adding this part, I am no more able to submitting the form!
Can anyone please let me know what am I doing wrong here?
I just need to process the PHP code and return the three mentioned variables into a jquery variable so I can do some stuff with them.
Thank you in advance.
AJAX:
var form = $('#contact-form');
var formMessages = $('#form-messages');
form.submit(function(event) {
event.preventDefault();
var formData = form.serialize();
$.ajax({
type: 'POST',
url: form.attr('action'),
data: formData,
dataType: 'json', //after adding this part, can't anymore submit the form
success: function(data){
var message_status = data.message_status;
var duplicate = data.duplicate;
var number = data.ref_number;
//Do other stuff here
alert(number+duplicate+number);
}
})
});
PHP:
//other code here
$arr = array(
'message_status'=>$message_status,
'duplicate'=>$duplicate,
'ref_number'=>$ref_number
);
echo json_encode($arr);
The way you have specified the form method is incorrect.
change
type: 'POST',
to
method: 'POST',
And give that a try. Can you log your response and post it here ? Also, check your console for any errors.
If your dataType is json, you have to send Json object. However, form.serialize() gives you Url encoded data. (ampersand separated).
You have to prepare data as json object :
Here is the extension function you can add:
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
Credit goes to : Difference between serialize and serializeObject jquery
I have done some reading and I think i need to use json for this. I have never used this before. I am trying to accomplish this, but in jQuery
$email_exist_check = mysqli_query($connect, "SELECT * FROM accounts WHERE email='$desired_email'") or die(mysql_error());
$email_exist = mysqli_num_rows($email_exist_check);
if ($email_exist == 0) {
//stop and make user write something else
} else {
//keep going
}
I am switching my website over from php to jQuery, which is also very new to me but seems so much better. Here is a piece of my jQuery. I am validating a form. The form works and submits, but now i want to see if the email exists in my database before submission. How would i do this?
if (email == "") {
$("#error5").css("display", "inline");
$("#email").focus();
return false;
}
// Im guessing the new code would go here
var dataString = $("#acc_form").serialize();
var action = $("#acc_form").attr('action');
$.ajax({
type: "POST",
url: action,
data: dataString,
success: window.location.assign("cashcheck_order.php")
});
This is a basic ajax call using jquery
var thing1; //thing 1 to use in js
var thing2; //thing 2 to use
var form = ("#acc_form"); //localize the form to a variable. you don't need to keep looking it up
var dataString = form.serialize();
var action = form.attr('action');
$.ajax({
url: action,
data: dataString,
type: "post",
success: function(data){
var responseData = $.parseJSON(data); //json native decoding if available, otherwise will do it with jquery
thing1 = responseData["thing1"];
thing2 = responseData["thing2"];
},
error: function(data){
console.log("error", data);
}
});
On the php side, to bring the vars in you use
$input1 = isset($_GET["name_of_input1"]) ? $_GET["name_of_input1"] : "";
if this is set, set this value, else set blank.
you can use $_POST, $_REQUEST if you prefer.
do not forget to sanitize your inputs.
Now to send it back to the js file
$dataToReturn = [
"thing1"=>"I'm thing 1",
"thing2"=>"I'm thing 2"
];
//sending back data
echo json_encode($dataToReturn);
I have an ajax function that should be able to log me in, the function is executing and if I firebug it, every thing seems ok.
the php event handler, is also working since i used form post method before, but would like to use ajax now, Any body see where the problem is ?
Ajax function.
$(document).ready(function() {
$('#login').click(function() {
var email = $('#email').val();
var password = $('#password').val();
$.ajax({
url: 'core/manage_articles.php',
type: 'POST',
data: {
login:email,
email:email,
password:password,
},
success: function() {
window.location = 'profile.php';
}
});
});
});
and the eventlistner.
if(isset($_POST['login'])) /*Login -two arguments.*/
{
$email = $_POST['login_email'];
$password = $_POST['login_password'];
... all login stuff.
}
$_POST['login_email'] doesn't match your data. Should be $_POST['email']. Same with password.