using session variables inside javascript functions - php

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!

Related

Pass value to script without x=something [duplicate]

I am working on a project where i need to access clean URL's always i.e no parameters can be passed in the URL for getting values on another page.
Can anyone please suggest an alternative other than 'Form Submit' method.
Thanks In Advance.
Use $_SESSION for this purpose.
Using $_SESSION you can use variable in multiple pages wherever you want.
// assigning variable
$_SESSION['name']="variable";
//retrieving
echo $_SESSION['name'];
write session_start() at the top of page wherever you want $_SESSION variable
For Clean URL's i prefer you may use HTTP POST Method. Hope that helps.
<form name="frmApp" method="POST" action="/action.php">
</form>
Else, you can use AJAX with jQuery to submit the values to another page.
$.ajax({url:"action.php",success:function(result){
$("div").html(result);
}});
Check out w3schools to get started with AJAX : http://www.w3schools.com/jquery/jquery_ajax.asp
No support for SESSION since i don't like writing php code inside my web page.
Sessions can help, or ajax call.
for using clean url you can use post method in form
<form name='' method='POST' action=''>
You can try mapping resources to the url.
Suppose you want to get mailing address of a customer then you can write.
http://[baseurl]/[customerId]/mailingaddress.
Read more here

Stop refresh page

I have this kind of href:
DOWN</td>
When I click on the links I lost datas entered before in a form,what is the way to prevent this?
Thanks!!!
You can either set a cookie:
setcookie('savedData' , $whateverYouWantToHaveSaved, time()+60*60*24*7);
or use a session
session_id();
session_start();
$_SESSION['savedData'] = $whateverYouWantToHaveSaved;
Ref:
cookies: http://www.php.net/setcookie
or
sessions: http://www.php.net/manual/en/ref.session.php
From there you will recall your data via either:
$savedData = $_COOKIE['savedData'];
or
$savedData = $_SESSION['savedData'];
The rest you will need to learn on your own.
You could use a hidden textbox inside the form and when you click on the link you call javascript function to set the value of the hidden input-field and that function submits the actual form. After that you could access the hidden input value from PHP.
DOWN</td>

PHP onSubmit action on an <a>

So basically my question is very simple, I have two buttons, I for page forward, one for page backwards, If one of those is pushed, a javascript function is called inside an onClick Event. Javascript then gets the variables of the page and then redirects to the next page, the only problem is, that I need to pass those variables to PHP in order to put them into the Database. So for that I make a load of cookies to pass the variables.
However, I was wondering if something like this would work :
<form>
<a onClick="nexpage();" onSubmit="phpScript.php"> <img src = "previous button.jpg"/> </a>
</form>
The idea behind this is that I want to store the variables in a PHP script, which will put them in a display:none; <div> and then for javascript to get the variables out. This instead of using cookies.
So is it possible to run a PHP script to get the variables and when the script is finished to get them, Javascript kicks in to redirect to the next page...
The reason I don't test this at this moment, is that my code is 100% complete, I don't want any sudden changes that maybe won't work at all. Yes I know back-up this and that, but I thought just asking here, maybe someone will know the answer!
Sincerly,
Harmen Brinkman
You can also use onClick = "this.form.submit(); return false;".
There is no any event like onSubmit for link, instead form do have onSubmit event.
Normal Way as OP asked.
<form action = "phpScript.php" method = "POST">
you can use document.getElementById("my_form").submit();
#Dipesh Parmar – Good point. You could also do:
window.onload=function() {
document.getElementById('my-form').onsubmit=function() {
// do what you want with the form
// AJAX POST CALL TO PHP PAGE
// Should be triggered on form submit
alert('hi');
// You must return false to prevent the default form behavior
return false;
}
});
Inspiration by Capture a form submit in JavaScript

Using jquery to process actions which require passing PHP variables within MVC framework

I am using codeigniter. What i want to achieve is as follows.
I am on a page /profile/username (Controller profile, function username).
This function loads a view which has a submit button.
<input type=submit value=apple>
I then have a jquery file which has ajax processing such that when the submit button is clicked /profile/process_click (a private function) is called with $.post.
The function process_click in the controller profile calls a method function which inserts the value of the submit button into the database (in this case 'apple'). Alongside this function needs to insert the username of the person on whos profile the link was clicked (in this case 'username').
So.. my question is, how do I pass username from my profile controller to my jquery file such that it can be passed to process_click by $.post?
I need a secure and safe and ideally not resource intensive way to do this..
Cheers
First of all, the process_click function cannot be private or you won't be able to call it via Ajax.
Jquery post method has a callback function which is called when the PHP script returns an answer, so in your js file do:
var userData = $("#form1").serialize();
$.post('http://localhost/profile/process_click',userData, function(data) {
//some code that handles data returned from php
});
in the php script:
public function process_data(){
$userName = $_POST['userName'];//or whatever the input name is
...//do some processing
echo $username;
}
i assume all of us would like to help but your question is not understandable, i think this is the reason you have received one answer only. Please edit and explain your problem in a wilder context. Don't save words, and use reference properly. for example, what is to 'jquery file' ??? what is to pass value to jquery? i assume you refer to javascript variable, and jquery $.ajax{} call, but can't be sure. can you be more oriented in your description. i also assume your english is much better than mine according to your residence so it shouldn't be an issue for you.
i can try and post back if you fix,
thanks and goodluck

passing javascript value to another page

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

Categories