How do I send a variable from a PHP to AJAX to a form or view?

If coding is more your thing then the Website Development & Scripting forums are for you. Share server-side scripting and database integration tips, tricks and problems here.
Post Reply
barnes
Newbie
Posts: 4
Joined: Sun Oct 01, 2017 9:28 am

Sun Oct 01, 2017 9:31 am

Hello, 

I am trying to get some variables from a php using ajax but I do not know how they are done. 

I have SESSION type variables in PHP, and I want to send them by ajax to a form that is in EXTJS. 

the variable is of this type: $ default_name = $ _SESSION ['default_name']; 

Someone could give me an example of how to send a variable from PHP by AJAX to another?, Because I see pure examples passing data from a form by ajax to php but not backwards. 

Any ideas? 

Thank you very much!!


User avatar
edwin
Junior Member
Posts: 18
Joined: Thu Sep 28, 2017 6:17 pm

Sun Oct 01, 2017 9:38 am

If you pass the php variables to an input type hidden:

Code: Select all

<input type = "hidden" id = "empr_name" value = "<? Php echo $ _SESSION ['empr_name']; ?>" />
And then you retrieve that value with jquery or javascript or the other would be that those session variables can be obtained from a php file (that only delivers those variables), for example getVariables.php and query that file through ajax and once rescued send them to the file you need :)
Last edited by edwin on Sun Oct 01, 2017 9:39 am, edited 1 time in total.
User avatar
Daniel204
Newbie
Posts: 7
Joined: Tue Jun 19, 2018 4:42 pm
Location: London

Mon Jul 30, 2018 8:22 pm

With jQuery, assuming is a POST:

$.post(‘mypage.php’, $(‘#myForm’).serialize())
.then(function(res) { // Success
});
I used a promise-style callback (.then) instead of using the third arg of the $.post function because it's more clear to read.

Anyway, the $.serialize function creates a URL encoded string, and it's perfect for these cases.

HTH!

Regards,
TechTIQ Solutions
Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 0 guests