sia ram
asked on
ajax is not sending data to php through javascript
function myFunction() {
var query = '?';
var str = $("form").serialize();
var x = $("#results").text(query += str);
return x;
}
$("input[type='sel'], input[type='text']").on("click", myFunction);
$("select").on("change", myFunction);
//window.alert(myFunction());
var jsonString = JSON.stringify(myFunction);
$.ajax({
type: 'POST',
url: 'insert_value.php',
data: {
data: jsonString
},
cache: false,
success: function () {
alert("OK");
}
});
<?php
$data = json_decode(stripslashes($_POST['data']));
// here i would like use foreach:
foreach($data as $d){
echo $d;
}
$src1= $_POST['data'];
$array = explode(",", $src1);
print_r($array);
?>
Here I have written java script for generating query string and I'm passing that array into ajax to send that array to php and print the values
Here in case only query string is generating but not sending it to php using ajax
Can you post the jsonString variable that you have to send?
ASKER
yaa inserted dataType:"json" but nothing changed
Check to have the right url directory.
ASKER
yaa I'll
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<?php
include("con_gen.php");
error_reporting(E_ALL);
// DATABASE CONNECTION AND SELECTION VARIABLES - GET THESE FROM YOUR HOSTING COMPANY
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "idcard";
$db_user = "root";
$db_word = "";
// OPEN A CONNECTION TO THE DATA BASE SERVER AND SELECT THE DB
$mysqli = new mysqli($db_host, $db_user, $db_word, $db_name);
// DID THE CONNECT/SELECT WORK OR FAIL?
if ($mysqli->connect_errno)
{
$err
= "CONNECT FAIL: "
. $mysqli->connect_errno
. ' '
. $mysqli->connect_error
;
trigger_error($err, E_USER_ERROR);
}
// RUN A QUERY
$result = mysqli_query($mysqli,"SELECT value FROM combo1");
$num_rows = mysqli_num_rows($result);
//echo "$num_rows Rows\n";
if ($result->num_rows > 0) {
// output data of each row
$array = Array();
$array1 = Array();
while($row = $result->fetch_assoc()) {
//echo "<br> value: ". $row['value']. "<br>";
$array[] = $row['value'];
}
//print_r($array);
$sql = "SELECT static_name FROM static_values";
$result = mysqli_query($mysqli, $sql);
if ($result && mysqli_num_rows($result) > 0)
while($row = mysqli_fetch_array($result)){
//echo "<option>" . $row['static_name'] . "</option>";
$array1[]=$row['static_name'];
//echo $source;
}
// print_r($array1);
foreach ($array as $row)
{
if(in_array($row, $array1))
{
$sql = "SELECT source_table,Alias_name FROM static_values where static_name='$row'";
$res = $mysqli->query($sql);
// DID THE QUERY WORK OR FAIL?
if (!$res)
{
$err
= 'QUERY FAILURE:'
. ' ERRNO: '
. $mysqli->errno
. ' ERROR: '
. $mysqli->error
. ' QUERY: '
. $sql
;
trigger_error($err, E_USER_ERROR);
}
// ARE THERE ANY ROWS IN THE RESULTS SET
if ($res->num_rows == 0)
{
trigger_error("ERROR: NO DATA FOUND BY $sql", E_USER_ERROR);
}
// RETRIEVE THE ROWS INTO AN ARRAY OF HTML STATEMENTS
//$html = "";
echo '<form method="POST" action="" id="myform">';
while ($row = $res->fetch_object())
{
//$html =$html.'
echo'
<tr>
<td>
<div class="row-fluid">
<div class="span3 bgcolor">
<label>'.$row->Alias_name.'</label>
<select id='.$row->source_table.' name='.$row->source_table.' data-live-search="true" class="selectpicker form-control" type="sel">';
/*foreach ( $Data->{$row->source_table} as $key =>$item) {
echo "<option value=".$key.">".$item."</option>" ;
}*/
foreach ( $Data->{$row->source_table} as $key =>$item) {
echo "<option value=".$key.">".$item."</option>";
}
echo '
</select>
</div>
</div>
</td>
</tr>';
}
}
else
{
//$html =$html.'
echo '
<tr>
<td>
<div class="row-fluid">
<div class="span3 bgcolor">
<label>'.$row.'</label>
<input id='.$row.' type="text" placeholder=" Enter Value " name='.$row.' style="width:100%" class="form-control" />
</select>
</div>
</div>
</td>
</tr>';
}
}
echo'<input id="submit" type="submit" value="SUBMIT" class="btn btn-success" name="submit" onclick="myFunction();"/> ';
echo '<p id="results"> </p></form>';
//echo '<p><tt id="results"></tt></p>';
}
?>
</body>
</html>
Change this:
var jsonString = JSON.stringify(myFunction);
to this:var jsonString = JSON.stringify(myFunction());
You must invoke the myFunction to get the x
ASKER
yaa I have changed that but whta is happening here means when we saw on the console.log(myFunction()); it is showing value only from dropdown control not value of text box control img.docx
If you want to send your data as json then you need to do add this header
HTML
contentType: 'application/json; charset=utf-8',
You also need to stringify your data - here is a complete sampleHTML
<pre id="result"></pre>
jQuery<script>
$(function() {
var data = JSON.stringify({a: 1, b:'test'});
$.ajax({
url: 't2423.php',
data: data, // This must be the JSON string not an object with the string as a property as you have it in your code
contentType: 'application/json; charset=utf-8',
type: 'POST'
}).done(function(resp) {
$('#result').html(resp)
});
});
</script>
PHP<?php
$json = file_get_contents('php://input');
$data = json_decode($json);
print_r($data);
Working sample here
ASKER
But it is not working for me because the query string is not sending into ajax result I have sent one document see on that there javascipt query string is printing into <p>
but ajax is result is not changing at all
img.docx
but ajax is result is not changing at all
img.docx
Where is the AJAX in the code you posted?
Did you add the content-header to your ajax call
Did you fix your data: property to send the JSON string instead of an object containing the JSON string.
Did you add the content-header to your ajax call
Did you fix your data: property to send the JSON string instead of an object containing the JSON string.
ASKER
<script>
function myFunction() {
var query = '?';
var str = $("form").serialize();
$("#results").text(query += str);
var x = $("#results").text();
return x;
}
$( "input[type='sel'], input[type='text']").on( "click", myFunction);
$( "select" ).on( "change", myFunction);
console.log(myFunction());
//window.alert(myFunction());
//query += '&' + value + '=' + value +'.value';
var data = JSON.stringify(myFunction());
$.ajax({
url: '/insert_value.php',
data: data,
contentType: 'application/json; charset=utf-8',
type: 'POST'
}).done(function(resp) {
$('#result').html(resp)
});
</script>
Above is javascript and Ajax
<?php
$json = file_get_contents('php://input');
$data = json_decode($json);
print_r($data);
?>
Above is insert_Value.php which is ther in ajax url<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<!--<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />-->
<style type="text/css">
.echo{
margin:60px;
}
</style>
</head>
<body>
<?php
include("con_gen.php");
error_reporting(E_ALL);
// DATABASE CONNECTION AND SELECTION VARIABLES - GET THESE FROM YOUR HOSTING COMPANY
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "idcard";
$db_user = "root";
$db_word = "";
// OPEN A CONNECTION TO THE DATA BASE SERVER AND SELECT THE DB
$mysqli = new mysqli($db_host, $db_user, $db_word, $db_name);
// DID THE CONNECT/SELECT WORK OR FAIL?
if ($mysqli->connect_errno)
{
$err
= "CONNECT FAIL: "
. $mysqli->connect_errno
. ' '
. $mysqli->connect_error
;
trigger_error($err, E_USER_ERROR);
}
// RUN A QUERY
$result = mysqli_query($mysqli,"SELECT value FROM combo1");
$num_rows = mysqli_num_rows($result);
//echo "$num_rows Rows\n";
if ($result->num_rows > 0) {
// output data of each row
$array = Array();
$array1 = Array();
while($row = $result->fetch_assoc()) {
//echo "<br> value: ". $row['value']. "<br>";
$array[] = $row['value'];
}
//print_r($array);
$sql = "SELECT static_name FROM static_values";
$result = mysqli_query($mysqli, $sql);
if ($result && mysqli_num_rows($result) > 0)
while($row = mysqli_fetch_array($result)){
//echo "<option>" . $row['static_name'] . "</option>";
$array1[]=$row['static_name'];
//echo $source;
}
// print_r($array1);
foreach ($array as $row)
{
if(in_array($row, $array1))
{
$sql = "SELECT source_table,Alias_name FROM static_values where static_name='$row'";
$res = $mysqli->query($sql);
// DID THE QUERY WORK OR FAIL?
if (!$res)
{
$err
= 'QUERY FAILURE:'
. ' ERRNO: '
. $mysqli->errno
. ' ERROR: '
. $mysqli->error
. ' QUERY: '
. $sql
;
trigger_error($err, E_USER_ERROR);
}
// ARE THERE ANY ROWS IN THE RESULTS SET
if ($res->num_rows == 0)
{
trigger_error("ERROR: NO DATA FOUND BY $sql", E_USER_ERROR);
}
// RETRIEVE THE ROWS INTO AN ARRAY OF HTML STATEMENTS
//$html = "";
echo '<form method="POST" action="" id="myform">';
while ($row = $res->fetch_object())
{
//$html =$html.'
echo'
<tr>
<td>
<div class="row-fluid">
<div class="span3 bgcolor">
<label>'.$row->Alias_name.'</label>
<select id='.$row->source_table.' name='.$row->source_table.' data-live-search="true" class="selectpicker form-control" type="sel">';
/*foreach ( $Data->{$row->source_table} as $key =>$item) {
echo "<option value=".$key.">".$item."</option>" ;
}*/
foreach ( $Data->{$row->source_table} as $key =>$item) {
echo "<option value=".$key.">".$item."</option>";
}
echo '
</select>
</div>
</div>
</td>
</tr>';
}
}
else
{
//$html =$html.'
echo '
<tr>
<td>
<div class="row-fluid">
<div class="span3 bgcolor">
<label>'.$row.'</label>
<input id='.$row.' type="text" placeholder=" Enter Value " name='.$row.' style="width:100%" class="form-control" />
</select>
</div>
</div>
</td>
</tr>';
}
}
echo'<input id="submit" type="button" value="SUBMIT" class="btn btn-success" name="submit" onclick="myFunction();"/> ';
echo '<p id="results"> </p></form>';
//echo '<p><tt id="results"></tt></p>';
}
echo '<pre id="result"></pre>';
?>
</body>
</html>
Above is genetating controls i.e. dyn_html.php
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Actually I want generate controls as how many number of controls we need dynamically so I need insert those controls values into a table that query should written in php so i need to send those value from java-script to php using ajax bcz the values of controls are not yet known so I had written function in JavaScript I thought this is the way to do If there is any way please help me I'm not getting what to do with this
It helps to walk before you run, and the same can be said for programming -- it helps to have simple working examples before you try to build an application.
Here is an explanation of the HTTP protocol and the client/server request/response design. You must understand this in order to write any kind of web application.
https://www.experts-exchange.com/articles/11271/Understanding-Client-Server-Protocols-and-Web-Applications.html
Here is a simple jQuery example.
https://www.experts-exchange.com/articles/10712/The-Hello-World-Exercise-with-jQuery-and-PHP.html
And here are some good learning resources for anyone who is new to PHP.
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html
Here is an explanation of the HTTP protocol and the client/server request/response design. You must understand this in order to write any kind of web application.
https://www.experts-exchange.com/articles/11271/Understanding-Client-Server-Protocols-and-Web-Applications.html
Here is a simple jQuery example.
https://www.experts-exchange.com/articles/10712/The-Hello-World-Exercise-with-jQuery-and-PHP.html
And here are some good learning resources for anyone who is new to PHP.
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.