cordova app with jquery, php - php

I'm trying to make a hybrid app using cordova and I want to ask two questions?
One of them is the login, I did it with jquery
<script type="text/javascript">
$(document).ready(function () {
$("#submit_btn").on("click", function(){
var username = $("#username").val();
var pass = $("#pass").val();
$.ajax({
type: "POST",
url: "link",
data: { username: username},
sucess: function () {
}
})
});
});
</script>
Is this a "right" way to do it?
and the best way to connect to a database?
<?php
header('Acess-Controll-Allow-Origin: *');
header('Acess-Controll-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
if(isset($_POST)) {
$username = $_POST['username'];
$pass = $_POST['pass'];
require("connect.php");
$sql = "INSERT INTO fields VALUES ('$username'. '$pass')";
mysqli_query($mysqli, $sql);
}
?>
I'm getting this error:
Notice: Undefined index: $username in /home/wm29tzgj/public_html/areacliente/insert.php on line 7
Notice: Undefined index: $pass in /home/wm29tzgj/public_html/areacliente/insert.php on line 8
<div class="signin-form">
<div class="container">
<form class="form-signin" method="post" id="login-form">
<h2 class="form-signin-heading">Log In to WebApp.</h2><hr />
<div id="error">
<!-- error will be shown here ! -->
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="Username" name="username" id="username" />
<span id="check-e"></span>
</div>
<div class="form-group">
<input type="password" class="form-control" placeholder="Password" name="password" id="password" />
</div>
<hr />
<div class="form-group">
<button type="submit" class="btn btn-default" name="btn-login" id="btn-login">
<span class="glyphicon glyphicon-log-in"></span> Sign In
</button>
</div>
</form>
</div>
</div>

Like this :
<script type="text/javascript">
$(document).ready(function () {
$("#submit_btn").on("click", function(){
var username = $("#username").val();
var pass = $("#password").val();
$.ajax({
type: "POST",
url: "http://website.php",
data: {'username': username,"pass":pass },
cache: false,
success: function (data) {
}
});
});
</script>

<script type="text/javascript">
var username="";
var passTxt="";
$(document).ready(function () {
$("#submit_btn").on("click", function(){
username = $("#username").val();
passTxt = $("#password").val();
$.ajax({
type: "POST",
url: "http://website.php",
data: {'username': username,"pass":passTxt },
cache: false,
success: function (data) {
}
});
});
</script>

Related

How to get CKEditor value in Codeigniter?

I Want to receive the values from the CKEditor. I Have written this HTML code
<div class="col-md-12">
<div class="form-group">
<label for="Image" class="control-label">Item Description </label>
<textarea id="item_description" name="item_description" rows="10" cols="80" style="resize:none"></textarea>
</div>
<!-- /.form-group -->
</div>
<div class="col-md-2">
<input type="submit" name="submit" id="Add" value="Add Items" class="btn btn-success">
</div>
This is the JQuery Code to get the value of the CKEditor
$("#Add").on("click", function(e) {
e.preventDefault();
var item_description = CKEDITOR.instances["item_description"].getData();
var formData = new FormData($("#form1")[0]); //It automatically collects all fields from form
$.ajax({
url: "'.base_url().'Home/add_items",
type: "post",
data: formData,
success: function(output) {
alert('Added'):
}
});
});
And this the Home controller but when I here access the item_description
value is empty.
<?php
class Home extends MY_Controller
{
public function add_items()
{
echo $title = $this->input->post('item_description');
//$this->show('admin/add_items.php');
}
}
?>
Modify your javascript codes, like this :
$("#Add").on("click", function(e) {
e.preventDefault();
var formData = new FormData($("#form1")[0]); //It automatically collects all fields from form
formData.append('item_description', CKEDITOR.instances['item_description'].getData());
$.ajax({
url: "<?=base_url()?>Home/add_items",
type: "post",
data: formData,
success: function(output) {
alert(output);
}
});
});

Cannot post data with JQuery and Ajax

I'm new with php. I'm trying to post some input with ajax but it doesn't work.
This is my HTML for input:
<div class="form-group">
<h3>Answer:</h3>
<div class="input-group">
<textarea name="q1" id="q1" class="form-control" rows="4" ></textarea>
</div>
</div>
<button type="button" id="button" class="btn btn-primary btn-lg">Submit</button>
And this is my jquery function to post data:
$(function(){
$('button').click(function(){
var q1= $('#q1').val();
$.ajax({
type: 'post',
url: 'test.php',
data: {
q1: q1
},
success: function (response) {
console.log( response);
}
});
});
});
My test.php is a simple code to display the input:
<?php
$q1= $_POST['q1'];
echo $q1;
?>
I don't know why in my test.php I get this error : Notice: Undefined index: q1 in C:\xampp\htdocs\series\file\test.php on line 2
Can anyone tell me where is the issue?
First of all you are trying to post a json data to your php file so jQuery may not create correct payload for your request since you getting undefined index:q1. Can you try the code below;
$(function () {
$('button').click(function () {
var q1 = $('#q1').val();
$.ajax({
type : 'post',
url : 'test.php',
data : "q1="+q1,
success : function (response) {
console.log(response);
}
});
});
});
Try this:
$('document').ready(function(){
$("form#data").submit(function(){
var formData = new FormData($(this)[0]);
$.ajax({
url: 'url',
type: 'POST',
data: formData,
async: false,
success: function (data)
{
alert(data);
},
cache: false,
contentType: false,
processData: false
});
return false;
});
});
}
<form id="data" method="post">
<div class="form-group">
<h3>Answer:</h3>
<div class="input-group">
<textarea name="q1" id="q1" class="form-control" rows="4" ></textarea>
</div>
</div>
<input type="submit" id="button" class="btn btn-primary btn-lg">
</form>

I'm trying to send username and password to a php file using AJAX with post method. How to retrieve fields

How to access these variables ?
I'm trying to retrieve username and password in php files, But, it says Undefined index username if I use $_POST['Username'}
<script>
var a = new XMLHttpRequest();
a.onreadystatechange = function(){
if (a.readyState==4 && a.status==200) {
ajaxFinished = true;
alert(a.responseText);
}
else {
}
}
a.open("post","php/psswd.php",true);
a.send('"Username="+document.getElementByNames("Username")+"&Password="+getElementByNames("Password")'); // posting username and password
</script>
How to retrieve these fields in php file ?
I found out the answer myself, the problem was that,
a.setRequestHeader("Content-type","application/x-www-form-urlencoded");
needs to be added. And document.getElementsByName('xyz') returns nodeList, but not perticular node, We need to traverse that nodeList.
instead of using the XMLHttpRequest method, take a look at this:
<script type="text/javascript">
$('#loginForm').submit(function(e) {
var username = $("#login-username").val(); //id of the form text input
password = $("#login-password").val(); //id of the form text input
e.preventDefault();
$.ajax({
type: "POST",
url: url,
data: { form: 'login', username: '' + username + '', password: '' + password + ''}
}).success(function( msg ) {
//msg is the text returned from the PHP function that will validate the login
$('.login_success_text').html(msg);
});
});
</script>
<body>
<form role="form" name="loginForm" id="loginForm" method="POST">
<label>Username</label>
<input id="login-username" class="form-control text placeholder" placeholder="Username" name="username" type="text">
<label>Password</label>
<input id="login-password" class="form-control password placeholder" placeholder="Password" name="password" autocomplete="off" type="password">
<input type="submit" value="Login" />
<div class="login_success">
<span class="login_success_text"></span>
</div>
</body>
The syntax is getElementsByName and not the way you presently have getElementByNames
The word Elements is pluralized, not Names.
<script>
var a = new XMLHttpRequest();
a.onreadystatechange = function(){
if (a.readyState==4 && a.status==200)
{
ajaxFinished = true;
alert(a.responseText);
}
else
{
}
}
a.open("post","php/psswd.php",true);
a.send('"Username="+document.getElementsByName("Username")+"&Password="+getElementsByName("Password")'); // posting username and password
</script>
For more information on this, visit:
https://developer.mozilla.org/en-US/docs/Web/API/document.getElementsByName
Edit
The following works using jQuery and tested with FF 28.0 and IE 7.
Sidenote: You may want to change this window.location = "success.php"
<!DOCTYPE html>
<head>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
function chk_ajax_login_with_php(){
var username=document.getElementById("username").value;
var password=document.getElementById("password").value;
var params = "username="+username+"&password="+password;
var url = "php/psswd.php";
$.ajax({
type: 'POST',
url: url,
dataType: 'html',
data: params,
beforeSend: function() {
document.getElementById("status").innerHTML= 'checking...' ;
},
complete: function() {
},
success: function(html) {
document.getElementById("status").innerHTML= html;
if(html=="success"){
window.location = "success.php"
}
}
});
}
</script>
</head>
<body>
<div id='logindiv'>
<label>Username:</label>
<input name="username" id="username" type="text">
<label>Password:</label>
<input name="password" id="password" type="password">
<input value="Submit" name="submit" class="submit" type="submit" onclick='chk_ajax_login_with_php();'>
<div id='status'></div>
</div>

Pass ajax form value to controller codeigniter

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('form.ajax-form').onclick('login_submit', function() {
var obj = $(this), // (*) references the current object/form each time
url = obj.attr('action'),
method = obj.attr('method'),
data = {};
obj.find('[user_email]').each(function(index, value) {
// console.log(value);
var obj = $(this),
email = obj.attr('user_email'),
value = obj.val();
data[email] = value;
});
obj.find('[user_pass]').each(function(index, value) {
// console.log(value);
var obj = $(this),
pwd = obj.attr('user_pass'),
value = obj.val();
data[pwd] = value;
});
$.ajax({
// see the (*)
url: url,
type: method,
data: data,
success: function(response) {
console.log(response);
// $("#feedback").html(data);
}
});
// console.log('Trigger');
return false; //disable refresh
});
});
</script>
<?php echo form_open('welcome/login', array('class' => 'ajax-form','style'=>'z-index: 202')); ?>
<input name="user_email" type="email" class="login_email" placeholder="Email address" required autofocus>
<br />
<input name="user_pass" type="password" class="login_pass" placeholder="Password" required>
<label class="checkbox">
Forgot login details?
<button class="btn btn-lg btn-info" id="login_submit" style="margin-left:20px;">Sign in</button>
</label>
</form>
<?php if(isset($login_error) && $login_error !=='') { ?><div class="alert alert-danger login_err"><?php echo $login_error; ?></div> <?php } ?>
<!-- </div>-->
Hi i am trying to pass the email and password to the controller in codeigniter.
Any idea how to do that please help?
You could simplify a big deal by doing:
$('#ajax-form').submit(function(){
var form = $(this);
$.ajax({
url: form.attr('action'),
type: form.attr('method'),
data: form.serialize(),
success: function(response) {
console.log(response);
// $("#feedback").html(data);
}
});
return false;
});

how to find a parent?

i have a problem with finding parent for form in this code:
<li class="comment<?php echo $comment[id]?>">
<div class="comment-content">
<div class="comment-top">
<div class="comment-nme">
<?php echo $comment[name]?>
</div>
<div class="comment-dt">
<?php echo $comment[dt]?>
</div>
</div>
<div class="comment">
<?php echo $comment[comment]?>
</div>
<a class="reply" href="#comment<?php echo $comment[id]?>">Ответить</a>
</div>
<div class="answer-form">
<form method="post" name="answer-form" class="ans">
<textarea class="comment-textarea" name="comment"></textarea>
<div class="a-comment-inputs">
<input type="hidden" name="parent_id" value="<?php echo $comment[id]?>">
<input type="hidden" name="status" value="new">
<div class="a-comment-name">
Имя</br>
<input type="name" name="name" class="a-comment-name">
</div>
<div class="a-comment-email" >
Eмейл</br>
<input type="email" class="a-comment-email" name="email">
</div>
</div>
<div class="comment-apply">
<button value="submit" onclick="return sendDataChild();" class="answer-but">Добавить</button>
</div>
</form>
</div>
<?php if($comment[childs]){ ?>
<ul class="commentsRoot<?php echo $comment[id]?>">
<?php echo commentsString($comment[childs]) ?>
</ul>
<?php } ?>
</li>
i use this jQuery function:
function sendDataChild() {
var form = $('FORM[name=answer-form]');
var data = form.serialize();
$.ajax({
type: "POST",
url: "req.php",
dataType: "json",
data: data,
cache: false,
success: function (data) {
form[0].reset();
},
error: function (xhr, str) {
alert('Возникла ошибка: ' + xhr.responseCode);
}
//$("#messageModalDialog").text(resultStat).show();
});
return false;
};
but it select every form that find on button click.
Can somebody advise how to solve it?
one possible solution
<button value="submit" onclick="return sendDataChild(this);" class="answer-but">Добавить</button>
then
//pass the clicked button reference then find the parent form of the button
function sendDataChild(btn) {
var form = $(btn).closest('FORM[name=answer-form]');
var data = form.serialize();
$.ajax({
type: "POST",
url: "req.php",
dataType: "json",
data: data,
cache: false,
success: function (data) {
form[0].reset();
},
error: function (xhr, str) {
alert('Возникла ошибка: ' + xhr.responseCode);
}
//$("#messageModalDialog").text(resultStat).show();
});
return false;
};
Bind submit event on the form and pass the object to the form object to your method
$(document).ready(function(){
$("form[name='answer-form']").on('submit', function(){
sendDataChild($(this));
});
});
function sendDataChild(form) {
var data = form.serialize();
$.ajax({
type: "POST",
url: "req.php",
dataType: "json",
data: data,
cache: false,
success: function (data) {
form.reset();
},
error: function (xhr, str) {
alert('Возникла ошибка: ' + xhr.responseCode);
}
//$("#messageModalDialog").text(resultStat).show();
});
return false;
};

Categories