"Converting" javascript variable (String) value into PHP variable - javascript

I am using window.open() method to open a new window and passing a Javascript variable (String) using localStorage.setItem() method.
In the new window that's opened, I retrive the passed variable using localStorage.getItem() method.
Now I need this variable for an SQL query using PHP,
something like "SELECT rowname from tablename WHERE id = TheJSVariable"
But I couldn't find any where a solution for it.
Any suggestions?
Thanks

When opening with window.open you can add your parameter as common get parameter. i.e.:
window.open ("mysite.com/page.php?param=123");
And then at page.php you can read param value with:
$param = $_GET['param'];
update:
If you want to make post request you could add (hidden) form with form fields holding the values you want to pass and submit that form from code. Something like:
<form id="TheForm" method="post" action="test.asp" target="TheWindow">
<input type="hidden" name="something" value="something" />
<input type="hidden" name="more" value="something" />
<input type="hidden" name="other" value="something" />
</form>
<script type="text/javascript">
window.open('', 'TheWindow');
document.getElementById('TheForm').submit();
</script>
Window.open and pass parameters by post method

Related

set value in a form or pass a value in a form using jquery or javascript or ajax

i created a Google Form and list down all the ID in each input type, and then created a localhost with the same ID,
The first thing i want to do is to save the values into a localhost and pass the values in the Google Form or set the values into the corresponding ID or Name in each input type in a new tab, is there a way to do this using ajax or jquery?
my form looks like this
<form method="post" action"">
<input type="text" name="FName">
<input type="text" name="LName">
<input type="Submit">
</form>
$post('https://docs.google.com/a/grabtaxi.com/forms/d/e/1FAIpQLSfjiuhFbURVavdNW82ofHG3gPpBUKkK2VATQQKmV-YKKrJ75Q/viewform'{FName:Fname,LName}function(){
window.open('https://docs.google.com/a/grabtaxi.com/forms/d/e/1FAIpQLSfjiuhFbURVavdNW82ofHG3gPpBUKkK2VATQQKmV-YKKrJ75Q/viewform');
});
i am trying to use the $.post() in jquery but it looks like i cannot set the values into the Google Form.
Any tips please... Thank You very much
You should wrap your javascript code inside a scipt tag. Then, you should bind the code to submit event, tha happened when you press the submit button.
<script type="JavaScript">
$('input[type=submit]').submit(
// here you should put your code
});
</script>
And check your code, you wrote $post instead of $.post mi sing the dot.

Pass javascript variable value to .php using a html form

I have a html form, a variable and a .php script. I would like to pass the value of this variable to the .php script using a html form, it has to be the variables value, so lets say it is 1 then it passes the value 1, but then if the value of the variable changes to 2 then the value the form passes should be 2. Currently when passing the variable, it just passes the name of it.
html:
<form id="get-form" action="getAmount.php" method="POST" name="get-form">
<input type="text" name="amount" id="amount" />
<input type="hidden" id="testVar" name="testVar" value="testVar"/>
<input type="image" src="butImg.png" id="Button" value="pay" alt="but"/>
</form>
<script type="text/javascript">
var testVar= 1;
document.getElementById("testVar").value = testVar;
</script>
php:
$testVar= $_POST['testVar'];
I think the problem is that javascript in your browser is turned off,
so the javascript code will not be executed and the value of testVar will stay the same.
You can easily check that by opening the web inspector of your browser and check if the value of the hidden field is your given value or testVar.
Instead of trying passing the Javascript variable do pass the PHP one
<input type="hidden" name="testVar" id="testVar" value="<?php echo $_POST["testVar"];?>" />
If you need the JavaScript variable for something else you can still have the JS assignment part:
<script type="text/javascript">
var testVar= <?php echo $_POST["testVar"];?>;
</script>

How do I insert a cookie data value into a hidden form field?

I would like to be able to take data stored in a cookie and insert it into a hidden form value.
I am storing the data in a cookie using the following:
<script type="text/javascript">
$(function () {
$("#btnCookie").bind("click", function () {
$.cookie("name", $("#txtName").val());
I am able to retrieve the value as plain text on the next page using:
$.cookie("name")
I have a form on the 2nd page that I want this value to be a part of. How can I insert it into the following:
<input type="hidden" name="cookie" />
I have tried this but it doesn't work:
<input type="hidden" name="cookie" value="$.cookie("name")" />
You can do it like this,
var cookie_val = $.cookie("name");
$('input[name=cookie]').val(cookie_val);
<input type="hidden" name="cookie" value="$.cookie('name')" />
The double quotes are causing an error in the HTML.
Try this jquery cookie u can use in php.
value="<?php echo $_COOKIE['name']; ?>"
or use jquery
$('input[name=cookie]').val($.cookie("name"));

javascript set location sending JSON data

What I'm trying to do is the functional equivalent of setting the "location" property in javascript, but I want to send along JSON encoded data to the server. I don't want to use AJAX, I want to completely replace my page contents with whatever the server sends back.
I think I might be able to do what I want by using form.submit by setting the form enctype attribute to application/json, but I don't know how to get my JSON into the form data set. Is it possible to do this?
You could send the JSON as the query component of the URL:
document.location.href = server_url + "?" + encodeURIComponent(json_string);
One way of doing it is with a form like this (not as clean as you would like it to be, but if you do not want to use AJAX your choices are quite limited):
<form action="json.php" method="post">
<input type="hidden" name="json" value="{'x':1}" />
</form>
you could set an input value to stringified JSON when submitting the form:
<form method="post" action="myscript.php" onsubmit="DoSubmit();">
<input type="hidden" id="myjsoninput" name="json" value="{'x':1}" />
<input type="submit" name="submit" />
</form>
function DoSubmit(){
document.getElementById("myjsoninput").value = JSON.stringify({a:'b'});
return true;
}

javascript xslt will not correctly post data

I am trying to submit a form using javascript (generated from an xslt stylesheet).
<form name='myform' action='search.php' method='post'>
<input type='hidden' name='query' />
<script type='text/javascript'>
function submit(id)
{
document.myform.elements[0] = id; *Note* I have tried document.getElementById('query').value = id;
document.myform.submit();
}
</script>
</form>
After this code I have xsl translations formatting xml data. I call the submit function like this:
NOTE This href='' does nothing
submit('somequery')
When I click on the href the javascript function executes and seems to work fine, except NO data gets POST'ed. I set the value to 'somequery' but when the form gets POST'ed, the value is '' (blank).
Why does it do this? I have tried createElement('input') and such from within javascript but I cannot ever get the form to POST the input value.
Are you using Firefox here? The behaviour may differ between browser. Firstly, you should go back to using getElementById
document.getElementById('query').value = id;
The reason this may not have worked is that your hidden element does not actually specify an id attribute.
<input type='hidden' name='query' id='query' />
In IE, I believe it allows a lack of id and assumes it is equal to the name. In Firefox, a lack of id results in a javascript error when you try to do getElementById
<form name='myform' action='search.php' method='post'>
<input type='hidden' name='query' id='query' />
<script type='text/javascript'>
function submit(id)
{
document.getElementById('query').value = id;
document.myform.submit();
}
</script>
</form>

Categories

Resources