Unable to show table after POST on same form PHP jQuery - php

I have this code below that gets the data from the POST and return the data as a table. When the code runs I put Console Log to see what is happening once the SUBMIT button is pressed. When the console log shows as "done" I want to show the table instead, but can't see why it's not happening.
<script type="text/javascript">
$(function(){
$('#searchform').on('submit', function(e){
e.preventDefault();
//alert($('#searchpostcode').val())
$.post('includes/jobdetailssearch.php',
$('#searchform').serialize(),
function(data, status){
$('.table-responsive #displayadd').html(data.Display);
//$("#table-responsive td").last().append(data);
console.log("done");
}).fail(function () {
console.log("fail");
});
});
});
</script>
Why would this be?

Make sure your php script returns a serialized json structure, which can be parsed by javascript.
Try to console.log(data) in your callback function to see what is returned from PHP.

Try This:
<script type="text/javascript">
$(function(){
// Submit Button Pressed
$(document).on('submit','#searchform', function(e){
// Prevent Default Click
e.preventDefault();
//The first parameter of $.post() is the URL we wish to request
$.post("includes/jobdetailssearch.php",
{
//Then we pass in some data to send along with the request
searchval:$('#searchform').val()
},
//The third parameter is a callback function. The first callback parameter holds the content of the page requested, and the second callback parameter holds the status of the request.
function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});
</script>
Note:
$('#searchform').on('submit' work same as $('#searchform').submit( but when you use $(document).on('submit','#searchform' then it will also work for the DOM added later.

Related

jquery $.post data to popup window from a popup window

I'm trying to post data to a new window (using window.open) from a popup that was opened with the window.open method. What I'm trying to do is pass the selected option value to the newly opened window and load data using $_POST.
Here's what I tried:
$(document).on('click', '.openForm', function()
{
var select = $(this).data('select'),
val = $('#'+ select).val();
$.post('/path/to/page.php', {id: val}, function(res)
{
window.open('/path/to/page.php');
});
});
and currently page.php has a var_dump of $_POST which returns empty. It also opens the page in a new tab instead of a new window - I think this is to do with giving the open windows unique names?
I'm not sure how to get it so $.post works with posting to the same page and opening it with the sent data - any links or solutions you know of that could work?
Thanks :)
Modify your script like this:
<script type="text/javascript">
$(document).on('click', '.openForm', function()
{
var select = $(this).data('select'),
val = $('#'+ select).val();
$.post('/path/to/page.php', {id: val}, function(res)
{
console.log(res);
var myWindow = window.open("", "MyWindow", "width=600,height=600");
myWindow.document.write(res);
//window.open('test_json.php',1,'width=600, height=600');
});
});
</script>
1) var_dump of $_POST returns empty. Because your both request $.post('/path/to/page.php') and window.open('/path/to/page.php'); will be different. 1st treated as post and after completion of it window.open('/path/to/page.php'); will be arise which will be get request.
2) To open window on same not in new tab you have to pass its width and height as 3rd parameter in window.open() method.

Jquery ajax post doesn't send data to server end(Using PHP)

HTML
<a id="1">Add to notebook</a>
<a id="2">Add to notebook</a>
<a id="3">Add to notebook</a>
<a id="4">Add to notebook</a>
...
<a id="40">Add to notebook</a>
JavaScript
<script>
$(document).ready(function(){
//callback handler for post submit
$("a").click(function(e)
{
var vocab_id = $(this).attr("id");
var formURL = 'http://localhost/test/test.php';
$.ajax(
{
url : formURL,
type: "POST",
data : vocab_id,
dataType : "text",
});
e.preventDefault(); //STOP default action
//e.unbind(); //unbind. to stop multiple form submit.
});
});
</script>
I have a list of hyperlinks, it will send the currently clicked value of id to the server end by clicking the respective link.
But the problem is
Back-end doesn't receive any post data at all.
People said that I should uncomment the e.unbind() to stop multiple submit, but I got error after that line was uncommented. Error message is like this.
Uncaught TypeError: undefined is not a function 2:479(anonymous function) 2:479n.event.dispatch jquery-1.11.0.js:3r.handle
The data part in jQuery.ajax() should be like either
data :{ name : vocab_id }
or
data : "name="+vocab_id
Syntax for unbind() is not correct use the following
$(this).unbind(e);
it will unbind the object from current event handler

jquery ajax form sended, but var_dump($_POST) empty

everyone, i got a very simple problem, but i am really confused... for un hour
in a php page, i have the code:
<script>
$( document ).ready(function() {
$("#date_avant").click(function(){
$.post( "_corps_medecin_changedate.php", { name: 'test', id: '1' } );
$("#consultations_jour").load("_corps_medecin_changedate.php");
});
});
</script>
And in the page _corps_medecin_changedate.php, just at the beginning, i have the code:
<?php
var_dump($_POST);
$date_debut = $_POST['name'];
?>
And i got msg:
array (size=0)
empty
Notice: Undefined index: name
I checked the firebug, the parameters are correct in the post list, so i thinked it is sended correctly, but just canot receive in the action page.
Server info : Apache/2.2.11 (Win32) PHP/5.4.4
And in local envirment.
Can anyone help me?
Your returned data from your $.post call is not automatically made available to your .load() call. They are two separate functions which means your call to load essentially send no POST data to the PHP script.
You can pass the post parameters as part of your call to load
<script>
$( document ).ready(function() {
$("#date_avant").click(function(){
$("#consultations_jour").load("_corps_medecin_changedate.php", { name: 'test', id: '1' });
});
});
</script>
You have to handle result of post request in callback function, like this:
<script>
$( document ).ready(function() {
$("#date_avant").click(function(){
$.post( "_corps_medecin_changedate.php", { name: 'test', id: '1' },
function(result) { $("#consultations_jour").html(result);} );
});
});
</script>
In your code you make 2 different requests (first is in .post function, and second is in .load).
that is because your are calling the _corps_medecin_changedate.php again right (.load()), after the post is made with no posted datas.. and you are not using callbacks..
$("#consultations_jour").load("_corps_medecin_changedate.php"); //here
Since post is async... the .load() function (which isn't inside a post's callback) is called right after you call post and does not waits for it
i have no idea why are you loading the same php page again after post it made which i don't think is needed...but as an example ...you can use callback... to check the datas that you have posted
$.post( "_corps_medecin_changedate.php", { name: 'test', id: '1' },function(data){
alert('done');
});
//$("#consultations_jour").load("_corps_medecin_changedate.php");
try this and you should get the posted values in your PHP page.

Run PHP code when user clicks link and pass variables

I need to run a PHP code from external server when user clicks a link. Link can't lead directly to PHP file so I guess I need to use AJAX/jQuery to run the PHP? But how can I do it and how can I pass a variable to the link?
Something like this?
<a href="runcode.html?id=' + ID + '"> and then runcode.html will have an AJAX/jQuery code that will send that variable to PHP?
use something like this in you page with link
Some text
in the same page put this somewhere on top
<script language='javascript'>
$(function(){
$('.myClass').click(function(){
var data1 = 'someString';
var data2 = 5;//some integer
var data3 = "<?php echo $somephpVariable?>";
$.ajax({
url : "phpfile.php (where you want to pass datas or run some php code)",
data: "d1="+data1+"&d2="+data2+"&d3="+data3,
type : "post",//can be get or post
success: function(){
alert('success');//do something
}
});
return false;
});
});
</script>
on the url mentioned in url: in ajax submission
you can fetch those datas passed
for examlple
<?php
$data1 =$_POST['d1'];
$data2 =$_POST['d2'];
$data3 =$_POST['d3'];
//now you can perform actions as you wish
?>
hope that helps
You can do this with an ajax request too. The basic idea is:
Send ajax request to runcode.html
Configure another AJAX to trigger from that page
Considering, this as the markup
<a id="link" href="runcode.html'">Test</a>
JS
$("#link").on("click", function() {
$.get("runcode.html", { "id" : ID }, function(data) {
//on success
});
return false; //stop the navigation
});

Print the value in loop in Jquery ready function

somehow still not able to do what I’m inted to do. It gives me the last value in loop on click not sure why. Here I want the value which is been clicked.
Here is my code:
$(document).ready(function() {
var link = $('a[id]').size();
//alert(link);
var i=1;
while (i<=link)
{
$('#payment_'+i).click(function(){
//alert($("#pro_path_"+i).val());
$.post("<?php echo $base; ?>form/setpropath/", {pro_path: $("#pro_path_"+i).val()}, function(data){
//alert(data);
$("#container").html(data);
});
});
i++;
}
});
Here the placement_1, placement_2 .... are the hrefs and the pro_path is the value I want to post, the value is defined in the hidden input type with id as pro_path_1, pro_path_2, etc. and here the hrefs varies for different users so in the code I have $('a[id]').size(). Somehow when execute and alert I get last value in the loop and I don’t want that, it should be that value which is clicked.
I think onready event it should have parsed the document and the values inside the loop
I’m not sure where I went wrong. Please help me to get my intended result.
Thanks, all
I would suggest using the startsWith attribute filter and getting rid of the while loop:
$(document).ready(function() {
$('a[id^=payment_]').each(function() {
//extract the number from the current id
var num = $(this).attr('id').split('_')[1];
$(this).click(function(){
$.post("<?php echo $base; ?>form/setpropath/", {pro_path: $("#pro_path_" + num).val()},function(data){
$("#container").html(data);
});
});
});
});
You have to use a local copy of i:
$('#payment_'+i).click(function(){
var i = i; // copies global i to local i
$.post("<?php echo $base; ?>form/setpropath/", {pro_path: $("#pro_path_"+i).val()}, function(data){
$("#container").html(data);
});
});
Otherwise the callback function will use the global i.
Here is a note on multiple/concurrent Asynchronous Requests:
Since you are sending multiple requests via AJAX you should keep in mind that only 2 concurrent requests are supported by browsers.
So it is only natural that you get only the response from the last request.
What if you added a class to each of the links and do something like this
$(function() {
$('.paymentbutton').click(function(e) {
$.post("<?php echo $base; ?>form/setpropath/",
{pro_path: $(this).val()},
function(data) {
$("#container").html(data);
});
});
});
});
Note the use of $(this) to get the link that was clicked.

Categories