I am trying to passing javascript variable value to another page and retrieve here into a php variable
Using function i made a pop up to appear and store it to a java script variable so now how do i pass it another page and i do have some text box values but i get this ,i use action and give page name in it.I need both text box value and prompt value in another page.
Please understand JavaScript is ran on the client machine, and PHP on the server.
HTTP is stateless.
You have to use AJAX.
Are you familiar with AJAX?
Use JavaScript to dynamically add hidden form fields into your form and set the value (alternatively just create the hidden fields in the php output with no value and just use JS to set it).
If you use jQuery you can call something like:
$.post("test.php", { name: "John", time: "2pm" } );
See http://api.jquery.com/jQuery.post/
Using JS you can put a variable into cookie:
document.cookie = 'var1=VALUE1'
Then read it on the other page.
Or you can append #var1=value1 to URL, then read it on the other page using
top.location.hash
Related
I have the below code, and I'm not sure if it even works. Basically, I want to do a like system, whereby when I click on the link, it adds a +1 to the user's likes.
I've tried so hard to read up but I just don't get anything, and I found a code similar below.
When I clicked on the link, it does process my insert.php page, but I don't know how to get the variable values...
What should I do? I'm not sure if the code structure below is correct...
Thanks!
<script>
function insertSalary()
{
var salary = $("#salary").val();
$.post('insert.php', {salary: salary}, function(data)
{
$("#current-salary").html(data);
});
}
</script>
<div id="current-salary">
<a id="salary" onClick="insertSalary();">+1</a>
</div>
The variable will be in your php script as $_POST['salary']
The value of salary is passed as part of the post method in jquery.
So you can do:
$.post('script.php', {salary: 100}, function(data){...});
and this will pass the value 100 to your php script as the salary value.
In php the $_POST and $_GET hashes contain the data that you pass with a given request. In jquery $.post, $.get $.ajax create requests and take data hashes to build the data you want to pass with the request.
While this may work, I would recommend separating your logic from your website by putting the javascript in an external file and then link your HTML page to it.
I would also advise against declarative event binding which you have done by specifying onClick="insertSalary()".
jQuery provides a method to pro-grammatically assign functions to events using the on method. So for your code, you could use:
$('#current-salary').on('click', insertSalary());
I want to send some data to a php function with javascript. I have already post a question about this earlier today here, though I didnt get a satisfactory answer, maybe didnt explain myself right.
So I have some variables in javascript( I get it through some API). I would like to send this variables to a function in php. I am using codeigniter so I call the function like("somecontroller/somefunction").
I would like the send the data through post to the function. The function calls a view and reloads the whole html. I am looking for something like sending a form. Only I have the variables in javascript. If there isnt any other way I will dynamicly create a form with jquery and send the data with the .click method on the submit button. But is there a more elegant solution??
I know about ajax, but the problem is that I dont want a asynchronous request I want to just submit the data and then for the new html to load.
The easiest way I can think to solve this would be to send the data through the query string, although you'd have to change the target page to accept GET or REQUEST instead of just POST.
To do this just:
window.location.href = 'somecontroller/somefunction?var1='+var1+'&var2='+var2;
Take a look at this link how to pass arguments to your controller methods in CodeIgniter. Then just redirect user to this link.
If you want to redirect through the Form, set action of form to your controller and in php you can access your form elements through $_GET or $_POST
Since you said you're using jQuery:
$.post("somecontroller/somefunction", {
param1: var1,
param2: var2,
...
}, function(result) { ... });
Could anyone please tell me how to use php session variables inside a JavaScript function? I have a php page with some option buttons and when the user clicks the submit button i want the value of the checked option button to be stored in a php session variable which i want to use on another page. which radio button has been selected is being checked through JavaScript. In the same function i want to store that value in a php session varaible.
Could anyone please tell me how this can be done?
If i use the session variable inside the javascript function it simply ignores it.
you could get the php session variable name in javaScript like this
var varname = '<?php echo $_SESSION["variable_name"]; ?>';
there is something called "Javascript Cookies", very handy for this kind of stuff came in good use when i had your problem.
here's the link: Javascript Cookies
hope it helps you out!
So I have the following code:
<div id="currentmotto"><?php mottoGrab($name); ?></div>
And what this does is it uses curl to screen scrape a users motto and display it on the site. What I need it to do is for that function to refresh every few seconds to see if the user has updated the motto.
I know with jquery I can use the .load('phpfile.php') but the problem then is if I put that function in that file, it no longer gets the $name variable as that is from another page.
Any ideas?
Pass name to phpfile.php via the query string:
.load('phpfile.php?name=THENAME');
Then grab the name from within phpfile.php using $_GET['name'] and stick it in the function.
OR
Make an AJAX request passing the name and update the page using javascript.
Since PHP is executed server-side, it doesn't change once the client loads the page. The only way to refresh a div without JavaScript is to reload the page.
I would try using AJAX to get the name from the other file and update the HTML with JavaScript
I need to pass some data via AJAX POST or GET to a php file which then uses that data to
reference or access a database without actually refreshing the page. How am i to do it?
For ex: if i use:
function callFunc() {
$.post ( " phpFile.php " , { name: "James"}
...
...
...
}
Take a look at Easy Ajax with jQuery should explain things.
When you use Ajax, an http request is made to the server that calls a php script sending post or get variables to this. The script is then executed and the response returned to the main page, with jquery you can manage the response and make the page not to refresh. If you have a HTML form and a submit button the page will be refreshed, you have to avoid this. Use jquery Val() selector to get the input values and send them trought an Ajax call without the submit button.