Fetching JSON from form data - php

I am writing a code to fetch JSON parameter sent using form.
I have this html
<form action="jsonfile.php" method="POST" name="myForm" enctype="application/json">
<p><label for="first_name">First Name:</label>
<input type="text" name="first_name" id="fname"></p>
<p><label for="last_name">Last Name:</label>
<input type="text" name="last_name" id="lname"></p>
<input value="Submit" type="submit" onclick="submitform()">
</form>
And json here
<script>
var formData = JSON.stringify($("#myForm").serializeArray());
$.ajax({
type: "POST",
url: "jsonfile.php",
data: formData,
dataType: "json",
contentType : "application/json",
success: function(result){
}
});
</script>
NOW, i want get the values of the above form sent as json in the other file called JSONFILE.PHP. I don't really know what should i use to decord the data into JSON DATA.
Thank you.

// HTML Code
<form method="POST" name="myForm" id="myForm" enctype="application/json" onsubmit="return false">
<p><label for="first_name">First Name:</label>
<input type="text" name="first_name" id="fname"></p>
<p><label for="last_name">Last Name:</label>
<input type="text" name="last_name" id="lname"></p>
<input value="Submit" type="submit" >
</form>
// JavaScript Code
<script>
$("#myForm").submit(function () {
var formData = JSON.stringify($("#myForm").serializeArray());
$.ajax({
type: "POST",
url: "jsonfile.php",
data: formData,
dataType: "json",
contentType: "application/json",
success: function (result) {
}
});
});
</script>
// PHP Code jsonfile.php
<?php
$data = json_decode(file_get_contents('php://input'), true);
if ($data) {
print_r($data);
}
?>

Related

Unable to serialize both the forms at once using jquery ajax

I have two form, first form input text, and file upload, the second form contains only text fields,
Here first is shown and the second form is hidden on the first and second form is shown and the first form is hidden, on submitting the second form, I want to combine two forms and submit the data by using ajax,
<form id="data" method="post" enctype="multipart/form-data">
<input type="text" name="first" value="Bob" />
<input type="text" name="middle" value="James" />
<input type="text" name="last" value="Smith" />
<input name="image" type="file" />
<button>Submit</button>
</form>
<form action="conn.php" method="POST" id="request-form2" enctype="multipart/form-data">
<input type="text" name="full_name2">
<br/>
<input type="text" name="last_name2">
<br/>
<input type="submit" value="submit" name="submit">
</form>
Here is script, i have tried,
$('form#request-form2').click(function(event) {
event.preventDefault();
var formData2 = $('#data');
var formData = new FormData(formData2);
console.log(formData);
$.ajax({
url: 'conn.php',
type: 'POST',
data: formData,
success: function (data) {
// console.log(data)
},
cache: false,
contentType: false,
processData: false
});
});
Haven't tested this, but why couldn't you just:
var formData=$('#data').serializeArray();
var formData2=$('#request-form2').serializeArray();
formData.push(formData2);
and include formData in your data option of your ajax call?

How to fill a textbox based on its previous textbox value using ajax

i have a from like this
<div class="row1">
<input type="text" class="pinCode" name="pin[]" value="">
<input type="text" class="areaName" name="location[]" value="">
</div>
<div class="row1">
<input type="text" class="pinCode" name="pin[]" value="">
<input type="text" class="areaName" name="location[]" value="">
</div>
<div class="row1">
<input type="text" class="pinCode" name="pin[]" value="">
<input type="text" class="areaName" name="location[]" value="">
</div>
I have a table in database named "city" with two column "pincode" and "cityname".
As I am filling the pinCode textbox ,I want to populate CityName textbox with city's name respective to input pincode.
please suggest some Jquery or ajax codes and php success page as i am a beginer.
on mouse out of pincode, you can call an ajax function like this,
$(document).on("mouseOut or onBlur",".pincode",function() {
var pincode = $(this).val();
$.ajax({
url: "ajax.php",
type: "POST",
dataType: "HTML",
async: false,
data: {"pincode": pincode},
success: function(data) {
// data will have the area name echo'ed in ajax.php file
$(this).closest('areaName').val(data);
}
});
});
ajax.php
your code to get the area name based on pincode
echo $areaname;

PHP not getting $_POST from ajax post

I'm trying to post data to the same page.
I can see that the post from the ajax is working correctly but the $_POST in my index.php isn't finding the post after form submission.
<form method="post" id="classic_login" action="">
<input type="text" name="user" placeholder="Username" class="classic_field" id="user_field" />
<input type="text" name="pass" placeholder="Password" class="classic_field" id="pass_field" />
<input type="submit" name="login" value="Login" class="classic_button" id="login_button" />
<input type="submit" name="register" value="Register" class="classic_button" id="register_button" />
</form>
and I've tried both print_r($_POST) and isset both don't change after submission
print_r($_POST);
if(isset($_POST['user']) && isset($_POST['pass']))
echo "works";
printing formdata after a test submission i get: user=testuser&pass=testpass
$("#classic_login").submit(function(event) {
var formdata = $(this).serialize();
event.preventDefault();
$.ajax
({
url: "",
type: 'POST',
data: formdata,
//success: function(response) { alert(response); },
error: function() { alert("fail"); }
});
});
Alternatively, you could use document.URL to make a request on the same page. Then in PHP, include an exit; after the request:
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
echo '<pre>';
print_r($_POST);
exit; // this is important!
}
?>
<form method="post" id="classic_login" action="">
<input type="text" name="user" placeholder="Username" class="classic_field" id="user_field" />
<input type="text" name="pass" placeholder="Password" class="classic_field" id="pass_field" />
<input type="submit" name="login" value="Login" class="classic_button" id="login_button" />
<input type="submit" name="register" value="Register" class="classic_button" id="register_button" />
</form>
<!-- this is no brainer, of course you need to load the library -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$("#classic_login").submit(function(event) {
var formdata = $(this).serialize();
event.preventDefault();
$.ajax({
url: document.URL, // you can use this
type: 'POST',
data: formdata,
success: function(response) {
alert(response);
}
});
});
</script>
Sample Output

Form Cross domain POST request using PHP

I am trying to send data from a form to a php file so I can store it in a database, but its not working...
The code for the form is not on the same server as the php file, because the form will be on a mobile app.
html
<div data-role="page" id="createclub">
<div data-role="content">
<form id="cname" align="left" action="post">
<label for="name">Enter Name:</label>
<input type="text" id="name" value="" />
<input type="submit" value="Submit" data-inline="true">
</form>
<div id="result"></div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$("#cname").submit( function () {
$.post(
'http://www.clubbedin.isadcharity.org/createclub.php',
$("#cname").serialize(),
function(data){
$("#result").html(data);
alert("Data " + data);
}
);
return false;
});
});
</script>
php file
$name = $_POST['name'];
THANK YOU!!!
Add this at the beginning of your PHP file:
header("access-control-allow-origin: *");
More info on cross domain policy here.
I think you need to prevent the default function of the submit button using .preventDefault() because as I look on your code you want to submit your form using ajax
$("#cname").submit(function (e) {
e.preventDefault();
$.ajax({
url: 'http://www.clubbedin.isadcharity.org/createclub.php',
crossDomain: true, //set as a cross domain requests
type: 'post',
data: $("#cname").serialize(),
success: function (data) {
$("#result").html(data);
alert("Data " + data);
},
});
});
and please use .ajax() so that you can set your ajax request to a cross-domain request
http://api.jquery.com/jQuery.ajax/
Your input element <input type="text" id="name" value="" /> must set up the name attribute as name="name".
form #cname after edited
<form id="cname" align="left" action="post">
<label for="name">Enter Name:</label>
<input type="text" id="name" name="name" value="" />
<input type="submit" value="Submit" data-inline="true">
</form>
You can gather more informations from the jQuery API Documention:
http://api.jquery.com/
http://api.jquery.com/serialize/

jquery submit only works once

I'm quite new to jquery but can't get the following to work properly. I can submit my form only once, after that i can't submit it a second time.
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$('#register').submit(function() {
$.ajax({
data: $(this).serialize(),
type: $(this).attr('method'),
url: $(this).attr('action'),
success: function(response) {
$('#regrespons').html(response);
$("#regrespons").css("visibility", "visible");
$("#regrespons").fadeOut(1000);
}
});
return false;
});
});
</script>
<form id="register" action="includes/register.php" method="post" autocomplete="off">
<input class="inp" name="gebr`enter code here`" type="text" />
<input class="inp" name="ww1" type="password" />
<input class="inp" name="ww2" type="password" />
<input class="inp" name="mail" type="text" />
<input class="but" type="submit" value="Registreren" />
</form>
Any help is apriciated!
Change $("#register").submit(function () { to $(document).on('submit', '#register', function () {

Categories