i hope someone can help me with this problem
i have a logic problem in code to solve
i have a value in jquery in like this
$(".test").click(function(){
var category=$(this).attr("data-id");
}
data-id value is 1
php codes (test.php)
function test()
{
$newdata=data-id + data-id;
return newdata;
}
echo test();
i need the code to call the data-id value from first jquery to put in the test() function
and the last , i need to put the echo -ed newdata value after the php function calculate the value to put it inside .test div with this second jquery
$("li").click(function(){
$('.test').text(data-id);
}
i need the complete step to step code for this problem , thanks for notice and helping! , sorry for bad english
You have several things here, the server processes the page (php) and sends it to the client. In order to get the client altered content back to the server (php) for processing, you would use ajax.
First you need to get the value from your field provided by the client which you are already doing with:
$(".test").click(function(){
var category=$(this).attr("data-id");
}
Now, once you have that value, you need to pass it to your PHP page. To do this, we use ajax, you will need to alter your click function:
$(".test").click(function(){
var testDiv = $(this);
//get the category value
var category = testDiv.attr("data-id");
//pass the category value to your php page
$.ajax({
url: 'test.php',
method: 'POST',
data: 'category='+category,
success: function(returnedData)
{
}
});
});
You can now access the $_POST variable in your PHP function:
//within test.php
function test()
{
$newdata=$_POST['category'] + $_POST['category'];
return $newdata;
}
echo test();
//if category was 1, this would echo 2 (as 1 + 1 = 2)
The last step is to put the returned value into your "test" div that was clicked:
//to place the returned data from test.php
//alter your .test click function
$(".test").click(function(){
var testDiv = $(this);
//get the category value
var category = testDiv.attr("data-id");
//pass the category value to your php page
$.ajax({
url: 'test.php',
method: 'POST',
data: 'category='+category,
success: function(returnedData)
{
//repopulate the clicked div with the returned data
testDiv.html(returnedData);
}
});
});
Related
What I want to do:
I want to get a value from a select element
And then I want the specific record from the database on the basis of that value.
Finally, gets the record and displays it in an input field
This is my code:
You can't pass jQuery variable to PHP as such, however you can do same thing through ajax, where just pass selected value through ajax to PHP file and get returned result display in input field
<script>
$("#chooseRoles").change(function(e){
e.preventDefault();
$.ajax({
url: 'getValue.php',
data: 'selectedRole='+$(this).val(),
type: 'POST',
success: function(result){
$("#userRoles").val(result);
}
});
});
PHP CODE (getValue.php);
$roles = Roles::find($_POST['selectedRole']);
echo $capabilities = $roles->capabilities;
I am planning to insert a link to my another php page using jquery function, but the code doesn't work... Alternative methods such as attr() has been tried as well, but it was still the same result... Can someone pls give me advices regarding this matter? Following are my codes:
$(document).ready(
function(){
$('#date').change(
function(){
$.ajax (
{
url: 'process.php',
type: 'post',
data: {
item_no:2,
movie_id: $('#movie').val(),
movie_date: $('#date').val()
},
dataType: 'json',
success: function(response){
$('#showtime').empty();
for(var i = 0; i<response.length; i++){
var showtime = response[i]['showtime'];
$('#showtime').append("<a href='movie_seat.php?id="+showtime+"' id='seatlink'>");
$('#showtime').append("<font style='margin-right:50px;'>");
$('#showtime').append(showtime);
$('#showtime').append("</font></a>");
} // for loop
} // function taking response
} // block in ajax
); //ajax
} // function inside change
); // select->date change
} // function inside ready
); // document.ready
*Note that #showtime is the id of a attribute
My expectations:
The value displayed should be able to work as a link that will direct the user to another php page. But apparently, it doesn't....
You need append all together, because if you append something like this:
$('#showtime').append("<a href='movie_seat.php?id="+showtime+"' id='seatlink'>");
$('#showtime').append("<font style='margin-right:50px;'>");
$('#showtime').append(showtime);
$('#showtime').append("</font></a>");
That way the tags will be closed, and you html will seems like this:
<font style="margin-right:50px;"></font>
showtime
So what you need to do is append all together, i mean tags inside tags, try this:
var link = "<a href='movie_seat.php?id="+showtime+"' id='seatlink'>"
+ "<font style='margin-right:50px;'>"
+ showtime
+ "</font></a>";
$('#showtime').append(link);
Thanks for reading. I have tried the answers in other similar questions but none have worked so far. I'm trying to UPDATE values inside a Table that is inside a form, so in the first part of the file is the isset "saveImport" which is the name of the a tag that I'm using to send the id thru the URL:
if (isset($_POST['saveImport'])) {
$id = $_POST['id'];
}
a tag:
<a name="saveImport" href="./?id=<?= $row['id']; ?>" class="saveImport btn btn-success col-xs">Save</a>
I do get the ID value in the URL but since is been updated in the same file I'm assuming it refreshes the page before the variable gets post thru AJAX:
<script type="text/javascript">
$(document).ready(function() {
$('.saveImport').click(function() {
var imp_id = id.id;
var imp_href = $(id).attr('href');
alert(imp_href);
$.ajax({
url: "./",
data: {id : imp_id},
type: "POST",
success: function(data){
alert(id);//send this ID to the PHP variable without
//refreshing the file
}
});
});
});
I'm getting the class .saveImport because it's inside a Table which is displaying values from a Database so it's inside a while loop and if I use an ID it will cause conflict as the ID will repeat itself.
I already created the PHP function to UPDATE the Values which executes if the ISSET is true, so my real problem will be to execute the ISSET and inside grab the ID that was posted with AJAX so I can use the ID in the UPDATE function. Right now, if I click on the a tag, it sends the ID thru URL, the page refreshes but the value of the ID is not getting in the $id = $_POST['id];
I appreciate your time.
This should work.
Change the ajax url to your php file name (mine was t.php).
Replace 99 with your data from php. (id is used. href is not used)
<?php
if (isset($_POST['saveImport'])) {
print_r( $_POST );
exit;
}
?>
<a name="saveImport" id='99' href="./?id=99" class="saveImport btn btn-success col-xs"'>Save</a>
<script type="text/javascript">
$('.saveImport').click(function(event) {
var imp_id = this.id;
$.ajax({
url: "t.php",
data: {id : imp_id, saveImport: 1},
type: "POST",
success: function(data){
alert( data );//send this ID to the PHP variable without
//refreshing the file
}
});
event.preventDefault();
});
</script>
.preventDefault() will prevent the link from loading the target page... And then, ajax will proceed.
$('.saveImport').click(function(event) {
event.preventDefault();
What I have
An url like this: http://localhost:8888/website/?key=ABC
A MySQL table with many rows, one with a key called ABC. I have a button that I want users to click (upvote) the MySQL row corresponding to key=ABC.
In my scripts.js I have this (incomplete?) Ajax code:
function increment() {
$.ajax({
type: 'POST',
url: '../js/ajax.php',
success: function(data) {
alert("function saved: " + data);
}
});
}
And my ajax.php looks like this:
<?php
if (isset($_GET['key']) {
$rowKEYtest = $_GET['key'];
$sql2 = "UPDATE database SET Score = Score + 1 WHERE UniqueKey = '$rowKEYtest'";
$conn->query($sql2);
} else {
echo "lol";
}
?>
It's not updating. I have no idea what to do.
Please have a look in your code, There is 2 mistakes.
1. In your ajax call you are using type: 'POST', you should use GET.
2. You are nor parsing your 'key' param in ajax call.
Actually you are using POST method in ajax but in ajax.php trying to get the value with GET method. second thing you are not passing any param in ajax call.
Hope this will help.
I am having a problem with seeing one of my variables on a webpage. Here is what I have so far.
$(document).ready(function() {
$(function() {
$("#CheckID").click(function() {
// submit ajax job and display the result
var id = '$("#ID").val()'
$.ajax({
type: "POST",
url: "test_wID.php",
data: "id",
success: function(data) {
$('#rightselection').html(data)
}
});
});
});
});
This is the jquery function I am using to take an ID entered into a form and use that ID with a bash script.
Here is the php.
<?php
//Get the ID from the HTML form and use it with the check chunk script.
$id = $_POST['ID'];
if (is_null($id)){
echo "$id is null";
}
echo "Selected test Game ID: ".$id;
//print shell_exec('/opt/bin/tester $id');
?>
I commented out the script because the variable is returning null, at this point I am just trying to make sure that I get the ID.
For completeness here is the form I'm using.
print "<p><h3>ID: <input type=\"text\" id=\"ID\" /></h3></p>";
#print "<br>";
print "<p><button id=\"CheckID\">Check ID</button></p>";
When i click the button I get the message in my div that the variable is null. So my question is am I missing something in the declaration? How is it that the var id is null?
Thanks for any help provided.
You should consider changing your jQuery code to:
$.ajax({
type: "POST",
url: "test_wID.php",
data: {id: $("#ID").val()},
success: function(data) {
$('#rightselection').html(data)
}
});
You mixed up strings and variable references at two points.
First, the statement var id = '$("#ID").val()' assigns just a string to your if variable and not the return value of the jQuery call. So just remove the ' here.
Second, the data parameter you're giving to the ajax() call again consists just of a string "id" and not the respective value. Here you need to change to {'id': id}.
So after correcting everything, your code should look like this:
$(document).ready(function() {
$("#CheckID").click(function() {
// submit ajax job and display the result
var id = $("#ID").val();
$.ajax({
type: "POST",
url: "test_wID.php",
data: {'id': id},
success: function(data) {
$('#rightselection').html(data);
}
});
});
});
Sidenote: Try to put all ;, where they belong. This prevents some errors, which can be hard to track!
EDIT
As pointed out in the comment by #FlorianMargaine you only need one wrapper not two around your code.
Firstly, the two following snippets are equivalent:
$(document).ready(function() {
});
// Is equivalent to:
$(function() {
});
So your code does the same as:
$(document).ready(function() {
$(document).ready(function() {
});
});
Plain useless, right?
Secondly, this line is plain wrong:
var id = '$("#ID").val()';
You're passing a string to the id variable. $('#ID').val() is not evaluated. This is the equivalent of doing this in PHP:
$id = '$_POST["id"]';
Which is just wrong, right?
You want this:
var id = $('#ID').val();
By the way, this variable naming could be improved, the same goes for the HTML ID.
Thirdly, you're doing the same mistake in the data option of $.ajax:
data: 'id'
You're just passing a string to the data option. You want the value of the id variable.
Then, if you absolutely want a string, I don't recommend it. jQuery expects a special kind of string. You better pass an object. Like this:
data: {
id: id
}
Do you see why the variable naming is wrong? You can't differentiate the property from the value. If you had done the following:
var idValue = $('#ID').val();
You could use this:
data: {
id: idValue
}
Which is way more readable.
In your $.ajax call you need to do:
data : { id: id }
If you want to pass parameters in an AJAX call you need to pass a string similar to the GET string you see in urls. So something like: d=123&name=test
Change the line
var id = '$("#ID").val()'
To
var id = 'id=' + $("#ID").val();