How to get javascript post variable in php? - php

I have a small problem to get jQuery post variables. So i have javascript file where is code bellow:
<script type="text/javascript">
$.post("view.php", { name: "John" } );
</script>
And i try to get that name "John" in view.php file like this:
<?php $variable = $_GET["name"]; ?>
And it wont get that name. Can someone please help me?

The variable values are not included in $_GET you might find them in $_POST
<?php $variable = $_POST["name"]; ?>

Since you've used '$.post' you must use the related action which will be $_POST. If you were using '$.get', then you would use your current method of $_GET.

Use $_POST instead of $_GET...

And you can use
<?php $variable = $_REQUEST['name']; ?>
if not shure how it should be get.

Related

Get Parameter from URL using PHP

I have a url:
domain.com/?city=newyork&postback=test
I am currently successfully passing the postback parameter using the PHP below. However, I can not figure out how to also pass the city parameter.
<?php session_start();
if(isset($_SESSION['postback'])) {
if($_GET['postback'] == "") {
header ("Location: qualify-step2.php?postback=".$_SESSION['postback']);
}
}
?>
Can someone please help me edit the code successfully? Any suggestions are greatly appreciated.
You should use the $_GET superglobal to get params from url... In your case $_GET['city'] holds the value newyork and $_GET['postback'] holds the value test.
You mean like this
<?php session_start();
if(isset($_SESSION['postback'])) {
if($_GET['postback'] == "") {
header ("Location: qualify-step2.php?postback=".$_SESSION['postback']."&city=".$_SESSION['city']);
}
}
?>
For geting parameter from URL, you should use $_GET['param'];.
Note: In here, You should use Get method for sending data.

Passing Variables Between Scripts in PHP

I have a simple question, with maybe not so simple of an answer. I want to be able to set a variable in one script and pass that value and variable to another script. Without passing it through the url, and having the ability to pass an array.
So I have index.php in there I have a variable
<?php
$myvariable = '';
<form action=editRecord.php>
do some form actions here and submit moving to editRecord.php
</form>
?>
Now in editRecord.php
<?php
header('Location: go back to index.php);
run functions and edit the Mysql DB
//Ok now here is where I want to assign a value to $myvariable and pass it back to index.php
?>
Is this possible? I am sorry for the amateurish question, but I am very green when it comes to php.
Thank You.
you can set it in the $_SESSION variable:
<?php
session_start();
$_SESSION["myVar"] = "blabla";
....
Of course you can store an array() in this variable too.
Just pass that information in the querystring. Then you can access it via $_GET. If it doesn't exist, just set the value to an empty string or whatever default value you want it to have.
// editRecord.php
<?php
// do db dtuff
header('Location: index.php?myvariable=somevalue');
?>
// index.php
<?php
$myvariable = (isset($_GET['myvariable'])) ? $_GET['myvariable'] : '';
<form action="editRecord.php" >
</form>
?>
You can also use session variables:
// editRecord.php
<?php
session_start();
// do db stuff
$_SESSION['myvariable'] = 'somevalue';
header('Location: index.php');
?>
// index.php
<?php
session_start();
$myvariable = (isset($_SESSION['myvariable'])) ? $_SESSION['myvariable'] : '';
<form action="editRecord.php" >
</form>
?>
You can use sessions, POST data, GET data, cookies and database data.

I want to click on an html link and echo it in php

I cant get it work. Need something really simple, not complicated. And of course not using form and button.
I have a link:
click here
my php script has something like:
<?php
echo $_POST['name'];
echo $_POST['email'];
?>
Ok. So how can I post variables using post in an href? Is it possible?
Variables added to the Query string are found in $_GET, not in $_POST.
You want to use $_GET['field_name']
<?php
echo $_GET['name'];
echo $_GET['email'];
?>
$_POST is populated from POST request body data. You don't have a POST request here.
$_GET is populated from URL query parameters. You have URL query parameters here.
echo $_GET['name'];
echo $_GET['email'];
W3Schools has an example like this (http://www.w3schools.com/PHP/php_forms.asp):
<?php echo $_POST["name"] ?>
<?php echo $_GET["name"] ?>

PHP - Manually Set And Then Echo $_GET Data

How would I for example, take a url with some $_GET data, for example http://www.website.com/something?food=steak
How would I then output steak? My current situation is that I'm trying to use the Header function to redirect to a page where I have it so that if $_GET["duplicate"] is equal to 1, then echo this, else, echo nothing. But its not taking the $_GET data I can tell I did a var_dump($_GET);
<?PHP if ($_GET["duplicate"] == 1 )
{
echo "<h1>Username Taken!</h1>";
}
else
{
echo "";
}
?>
The above is using the url http://something.com/register?duplicate=1
It's just a variable, treat it like one:
echo $_GET['food'];
Everything after question mark is available in form of global array $_GET.
$a=$_GET["food"];
echo $a;
also
if url has ?food=steak&color=red;
$a=$_GET["food"];
$b=$_GET["color"];
more than one is possible. Also search for $_POST.
Alright, so I figured my issue out. I have a $_GET variable that gets the end of the page and declares it as "p" for page. I need to do the following to get it to work.
?p=createuser&duplicate=1

send php variable value via javascript in url

I've read this post(how do i send a variable in the url in javascript), but can't get it to work. I must add that I don't know javascript at all. I'm trying to open an new "printer friendly" page in php, but in order for that I need to send the userid with the url, my code looks like this:
Page1
<?php
session_start();
$userid = $_SESSION['userid'];
?>
<head>
<script type="text/javascript">
function open_win()
{
window.open("http://www.mysite.com/exams/print.php?uid_print=$userid",'','width=200,height=100')
}
</script>
</head>
<?php
if ($_SESSION['auth']) {
include 'datalogin.php';
echo "<input type='button' value='Print this page' onclick='open_win()' />";
Page2(print.php)
<?php
session_start();
include 'datalogin.php';
$uid_print1 = $_GET['uid_print'];
echo $uid_print1;
But my ouput is: $userid
window.open("http://www.mysite.com/exams/print.php?uid_print=<?php echo $userid; ?>",'','width=200,height=100')
At the moment the variable in your javascript tag is not replaced because it is not enclosed by PHP tags. You can easily do it like this:
window.open("http://www.mysite.com/exams/print.php?uid_print=<?php echo $userid; ?>",'', 'width=200,height=100')
Modify your window.open line to looks like this:
window.open("http://www.mysite.com/exams/print.php?uid_print=<?php echo $userid; ?>",'','width=200,height=100');
You have to 'enter' PHP block, and print out $userid variable to HTML (JavaScript) output.
This line in your JS script:
window.open("http://www.mysite.com/exams/print.php?uid_print=$userid",'','width=200,height=100')
Outputs '$userid' directly. You need to replace $userid with something like:
<?php echo $userid; ?>
Use <?php....?> tags:
...print.php?uid_print=<?php echo $userid;?>
Replacing:
window.open("http://www.mysite.com/exams/print.php?uid_print=$userid",'','width=200,height=100')
With:
window.open("http://www.mysite.com/exams/print.php?uid_print=<?php echo $userid; ?>",'','width=200,height=100')
Should probably do the trick.
You can also use the shorthand <?= $userid ?> which, to my eyes is more readable.
please concatenate $userid in your javascript.
it can be concatenated like uid_print='.$userid.' and you will get the value in $_GET["uid_print"]
you can also echo the $userid in string like

Categories