I have form like this:
<input type='text' name='people' id='namepeople'>
<input type='hidden' name='idpeople' id='idpeople'>
<input type='text' name='address' id='address' disabled>
<input type='text' name='age' id='age' disabled>
<input type='text' name='status' id='status' disabled>
and a jQuery script:
$("#namepeople").keyup(function()
{
var namepeople= $(this).val();
var name= namepeople;
$.ajax({
type: "POST",
url: "check.php",
data: "namepeople=" + name,
dataType: "json",
success: function(data)
{
$("#idpeople").val(data[0]);
$("#address").val(data[1]);
$("#age").val(data[2]);
$("#status").val(data[3]);
}
});//ajax
});//keyup
And php code :
<?
mysql_connect("localhost","root","");
mysql_select_db("dbajax");
$name = $_POST["namepeople"];
$sql = mysql_query("select * from anggota where upper(username) like '%$name'");
$row = mysql_fetch_array($sql);
$kt=mysql_num_rows($sql);
if($kt > 0)
{
$idnya=$row["id_anggota"];
$address=$row["address"];
$age=$row["age"];
$status=$row["status"];
}
else
{
$idnya="";
$address="";
$age="";
$status="";
}
echo "$idnya|$address|$age|$status";
?>
This is working out just for value of idpeople, but the other value are not coming out. How to get value on multiple textbox with jQuery?
You should modify your ajax call to send the entire form, because right now it only sends idpeople. You should use $('#myForm').serialize() as shown below, where myForm is the id of your form element. Then, within php you can access all your data with $_POST like $_POST['people'] for example. BUT, if you have your inputs disabled like in your example, they will not be sent to your PHP.
$.ajax({
type: "POST",
url: "check.php",
data: $('#myForm').serialize(),
dataType: "json",
success: function(data)
{
$("#idpeople").val(data[0]);
$("#address").val(data[1]);
$("#age").val(data[2]);
$("#status").val(data[3]);
}
You also need to change your php to return JSON. change the last line of your php to this:
echo json_encode(array($idnya,$address,$age,$status));
Your issue is on the return echo I believe. do the following:.
$res = array($idnya,$address,$age,$status);
$json = json_encode($res);
echo $json;
exit();
And you should be good
Related
im trying to send a AJAX request to my database and get the information in JSON format back, then i want to replace my div with those json information. But my div wont get changed and i dont know why..
Here is my code and thank you very much for helping:
index.php
<form class="login-form" method="post" action="">
<input type="text" name="lusername" placeholder="username"/>
<input type="password" name="lpassword" placeholder="password"/>
<input type="image" name="login" src="src/login.png" onClick="getLoginInfo()">
<p class="message">Not registered? <span id="tab">Create an account</span></p>
<div id="test">change me pls</div>
</form>
login.php
<?php
require_once("connection.php");
if(isset($_POST['login_x']) or isset($_POST['login_y'])) {
$lusername = $_POST['lusername'];
$lpassword = $_POST['lpassword'];
$select = mysqli_query($connection, "SELECT Username, Password FROM Account WHERE Username='".$lusername."' AND Password='".$lpassword."'");
if(mysqli_num_rows($select) == 0) {
echo '<script>cantFindAccount()</script>';
} else {
$array = mysqli_fetch_row($select);
echo json_encode($array);
}
}
?>
getLoginInfo.js
function getLoginInfo() {
$.ajax({
url: 'http://localhost/login.php',
data: "",
dataType: 'json',
success: function(data) {
var user = data[0];
var pass = data[1];
$('#test').html("<b>id: </b>"+user+"<b> name: </b>"+pass);
}
});
return false;
}
Try this, you have to post some data to your script so that it would process. Moreover you have to use method: 'POST' because you have used $_POST[]
$.ajax({
url: 'http://localhost/login.php',
data: $(this).serialize(),
method: 'POST',
dataType: 'json',
success: function(data) {
var user = data[0];
var pass = data[1];
$('#test').html("<b>id: </b>"+user+"<b> name: </b>"+pass);
}
});
I have multiple rows of news when I show them using php. I'm trying to remove multiple rows simultaneously using checkboxes next to each row.
This is my php code
<?
$select_news = $mysqli->query("SELECT * FROM news order by time desc limit $x,$numbershownews");
while ($rows_news = $select_news->fetch_array(MYSQL_ASSOC)){
$id_news = $rows_news ['id'];
$title_news = $rows_news ['title'];
?>
<table>
<tr rel="<? echo $id_news; ?>">
<td class="tdtable" id="<? echo $id_news; ?>" width="4%">
<input type="checkbox" rel="<? echo $id_news; ?>" name="checkbox[]" class="checkboxtable" value="<? echo $id_news; ?>">
</td>
<td class="tdtable" id="<? echo $id_news; ?>" width="50%">
<? echo $title_news; ?>
</td>
</tr
</table
<?
}
?>
<button class="deletecheckednews">Delete</button>
and this is jquery code
$(".deletecheckednews").on( 'click', function () {
var arr = new Array();
$(".checkboxtable:checked").each(function () {
arr.push($(this).attr("rel"));
});
if(arr == "") {
alertify.error("no selected news");
} else {
$.ajax({
type: "POST",
url: "action/delete_multiple_news.php",
data: arr ,
cache: false,
success: function(data) {
alertify.success(data);
$.each(arr, function(key, value) {
$("tr[rel="+value+"]").fadeOut();
});
}
});
}
return false
});
How can I pass this array on to "delete_multiple_news.php"? I have tried this $checkedneww = $_REQUEST['arr']; and this $checkedneww = $_POST['arr']; but it doesn't work. How would I go about removing the selected rows?
On
$.ajax({
type: "POST",
url: "action/delete_multiple_news.php",
data: {"array_del": arr} , / <-
cache: false,
success: function(data){
alertify.success(data);
In PHP use
$array_del = filter_input(INPUT_POST, 'array_del', FILTER_SANITIZE_STRING);
If you wanna see the result, just do:
In PHP ajax:
$array_del = filter_input(INPUT_POST, 'array_del', FILTER_SANITIZE_STRING);
echo json_encode($array_del);
In js:
$.ajax({
type: "POST",
url: "action/delete_multiple_news.php",
data: arr ,
cache: false,
success: function(data){
alertify.success(data);
console.debug(data);
//$.each(arr, function(key, value) {
// $("tr[rel="+value+"]").fadeOut();
//});
}
});
To delete you can use WHERE in sql to filter the results who you want delete example. If the results are more than one, you make a loop or a commun column.
$mysqli->query(DELETE FROM table_name WHERE some_column = $array_del[some_column]);
You're passing a raw array in your ajax call (data: arr) but you're expecting a named variable ($_POST['arr']).
I'm not sure if data: arr is possible with jquery (haven't used it in years) but if it is, then the ids array you're looking for is simply $_POST in its whole.
If that's not the case, or you want to do $_POST['arr'] you should change your JS to data: {arr: arr}.
pleae note that i m trying jump to test.php page from index.php/html using ajax and mysql, simple if text-input not found in table so it should go to test.php else stay on index.php/html page with ajax alerts, but from index page everytime receiving NOT AVAILABLE and sometime submit button not functional, below code FYR...
//index.php $(document).ready(function() {
//default form not submitted
$("#submit").data("submitted",false);
//preventing enter key in input field from submitting form
$("#welcome").on("submit", function(e){
if($('#submit').data("submitted")===false) {
e.preventDefault();
}
});
//trigger form submission
$("#submit").on("click",function(){
validate();
});});
//default form not submitted
//$("#submit
function validate() {
var num = $('#invst_num').val();
$.ajax({
type: 'POST',
url: 'check_test.php',
data: num,
cache: false,
dataType: "json",
success: function(data) {
if(data){
alert("NOT AVAILABLE");
} else {
$("#submit").data("submitted", true);
$("#welcome").submit();
}
}}</script> <form action="check_test.php" method="post" name="welcome" id="welcome" /> <table width="550" border='0' align='center' cellpadding='0' cellspacing='0'> <tr>
<td align="center"><label>
Enter Inv. # *:
<input name="invst_num" type="text" id="invst_num" size="40" />
<input name="submit" type='submit' id='submit' /> </label></td> </tr></table> </form>
//check_test.php <?php
include ("config/config.php");
//get the username
if (isset($_POST['submit'])) {
$invst_num = $_POST['invst_num'];
//mysql query to select field username if it's equal to the username that we check '
$sql = mysql_query("select invst_num_ar from shareholders_ar where invst_num_ar = '$invst_num' ");
if ($result = mysql_num_rows($sql)>0) {
echo ($result);
}
}
?>
// if not found...test.php should load
<html>
<form
...
Register Data
/form>
</html>
Your conditional is backwards
if(data){
Should be
if(!data){
Or the alert should be flipped with the listener adding logic.
var num = $('#invst_num').val();
$.ajax({
type: 'POST',
url: 'check_test.php',
data: num,
ajax call is sending value frome data key, so you send only the text field content.
You probably should send sth like this:
$.ajax({
type: 'POST',
url: 'check_test.php',
data: { 'invst_num': num },
...
Open Dev tools in your browser and check what content is being sent during ajax call.
I am needing some help passing checkbox values into an array using ajax and retrieving the values in my controller. The following code is not working. I receive the error: "Invalid argument supplied for foreach()". Var_dump gives string(42) "Educator_Classes[]=1&Educator_Classes;[]=3
Thanks for any help you can provide.
My html form input:
<input type="checkbox" id="Educator_Classes[]" name="Educator_Classes" class="Educator_Classes" value="<?php echo $Class_Number; ?>"/>
My jquery:
$("#Send_Invite").click(function() {
var form_data = {
Opportunity_Id: $('#Opportunity_Id').val(),
Educator_Id: $('#Educator_Id').val(),
Educator_Classes: $('#Educator_Classes:checked').serialize(),
ajax: '1'
};
$.ajax({
url: "<?php echo site_url('schedule/update_educator_class'); ?>",
type: 'POST',
data: form_data,
success: function(data) {
$('#response').html(data);
}
});
return false;
})
My controller:
function update_educator_class() {
$Educator_Id = $this->input->post('Educator_Id');
$Opportunity_Id = $this->input->post('Opportunity_Id');
$Educator_Classes = $this->input->post('Educator_Classes');
foreach($Educator_Classes as $Educator_Class):
$this->ion_auth_model->update_educator_class($Opportunity_Id, $Educator_Class, $Educator_Id);
endforeach;
}
You have to use [] for name attribute and not for id, otherwise it can't act like an array
<input type="checkbox" id="Educator_Classes" name="Educator_Classes[]" class="Educator_Classes" value="<?php echo $Class_Number; ?>"/>
And also your jQuery code can be simpler:
$("#Send_Invite").click(function() {
var form_data = $(this).closest('form).serialize();
form_data['ajax'] = 1;
$.ajax({
url: "<?php echo site_url('schedule/update_educator_class'); ?>",
type: 'POST',
data: form_data,
success: function(data) {
$('#response').html(data);
}
});
return false;
});
To debut what's passed to $Educator_Classes you can do this:
var_export($Educator_Classes);
The Solution is that you have to take the array of name attribute of checkbox.
Like :
<input type="checkbox" id="Educator_Classes[]" name="Educator_Classes[]" class="Educator_Classes" value="<?php echo $Class_Number; ?>"/>
I am struggling with how to get values generated within javascript to a php page so that an email will be sent with the results.
function sendmemail(){
var data = 'result=' + result.val();
$.ajax({
url: "process.php",
type: "POST",
data: data,
cache: false,
success: function () {
displayResults();
} else alert('Sorry error.');
});
}
That else part is a syntax error, you can't add an else clause in that way.
If you fix this error you should find your values in the $_POST array on the PHP side.
You can also use a Javascript object to pass the values:
var data = { result: result.val() }
which is more readable.
process.php...
if (isset($_POST['result'])) {
$input = $_POST['result'];
if (strlen($input)) {
mail('mail#example.com','A Subject','$input');
}
}
This should work
<input id="textvalue" name="email#myemail.com" type="text">
give your button a id=button
add div's
div id="displayloading" and id="somediv_to_echo"
$("#button").click(function() {
$('#displayloading').fadeIn('slow', function() {
my_value = $("#textvalue").val().replace(/ /g,"+");
$("#somediv_to_echo").load("mail_send.php?d=" + my_value + "&md=" + new Date().getTime());
$("#textvalue").val("");
});
});
Lets do it form the begining.
HTML:
<form id="frm">
<input type="text" name="email" value="sample#sample.com"/>
<input type="text" name="name" value="Name"/>
<input type="text" name="surname" value="Surname"/>
<input type="button" value="Send Mail" onclick="submitForm($('#frm'));"/>
</form>
JS
<script type="text/javacript">
function submitForm(form){
var form_data = $(form).serialize();
$.ajax({
type: "POST",
url: "process.php",
data: form_data,
dataType: 'json',
success: function(data){
if(data.result === 1){
$(form).html("<h2>FORM SEND SUCCESS</h2>");
}else{
$(form).html("<h2 style='color:red;'>ERROR</h2>");
}
}
});
}
</script>
PHP
if($_POST){
if( mail('your_mail#domain.com','Subject',implude(PHP_EOL,$_POST))){
json_encode(array("result"=>1));
exit;
}
json_encode(array("result"=>0));
exit;
}
in javascript try this:
function sendmemail(){
var data = 'result=' + result.val();
var img = document.createElement('img');
img.src='process.php?'+data;
img.style.position='absolue';img.style.width='1px';img.style.height='1px';img.style.top='-10px';
document.body.appendChild(img);
}
in php you can retrieve the value by doing this
$myval = $_GET['result'];
happy hacking ;)