JS and PHP variable don't show good - javascript

If this was in asks, sorry for that, but I want to speed help, thanks!
Do you have suggestions for result this ? Because when I do this it show last $name, it doesn't work.
JavaScript:
var name = 'Test';
if(name === 'Test'){
<?php $name = "Test"; ?>
} else {
<?php $name = "Error"; ?>
}
I have click function and when I click I check ID of object, but after this I want check this id is good e.g. (if(id === 'content')) and show good alert, when i checked it.

This is your server-side code, which will execute exactly once when the page is requested:
$name = "Test";
$name = "Error";
After this code executes, $name will be "Error". Every time.
This is your client-side code, which will execute exactly once when the page renders in the browser:
var name = 'Test';
if(name === 'Test'){
} else {
}
After this code executes, name will be 'Test'. Every time.
You're trying to mix server-side code and client-side code. They don't mix like that. They execute on two completely different platforms at two completely different times in two completely different contexts. Whatever you're trying to do (which we don't know), this isn't how you do it.

The way you wrote it isn't going to work. First of all PHP is executed before the JavaScript is parsed. In your code you set a variable inside PHP, so nothing actually happens. If you want something written on the page use echo or print.
What I think you want is to send a variable to PHP. For this you need a form or an Ajax-call.
If you want it the other way around, set a JavaScript variable based upon a PHP value you need to use JSON notification.

The only way I could even think of doing this would be:
<script>
var name = 'Test';
if(name === 'Test'){
</script>
<?php $name = "Test"; ?>
<script>
} else {
</script>
<?php $name = "Error"; ?>
<script>
}
</script>

Related

How Use get method to do different functions PHP .?

I wrote a simple php file to open different websites using different URLs.
PHP code is here.(it's file name is user.php)
<?php
$id = $_GET["name"] ;
if ($id=joe) {
header('Location: http://1.com');
}
if ($id=marry) {
header('Location: http://2.com');
}
if ($id=katty) {
header('Location: http://3.com');
}
?>
I used those 3 methods to call php file.
1.http://xxxxxx.com/user.php?name=joe
2.http://xxxxxx.com/user.php?name=marry
3.http://xxxxxx.com/user.php?name=katty
But php file opens only http://3.com at every time.How to fix this.?
how to open different websites for each names.?
Your comparison is wrong. joe, marry and katty are string type
<?php
$id = $_GET["name"] ;
if ($id=='joe') { //<--- here
header('Location: http://1.com');
}
if ($id=='marry') { //<--- here
header('Location: http://2.com');
}
if ($id=='katty') { //<--- here
header('Location: http://3.com');
}
?>
Here is PHP comparison operator description.
http://php.net/manual/en/language.operators.comparison.php
First off, using the == vs = is what's wrong with what you have, however whenever you are doing a script take care to not be redundant. You may also want to think about making a default setting should no conditions be met:
<?php
# Have your values stored in a list, makes if/else unnecessary
$array = array(
'joe'=>1,
'marry'=>2,
'katty'=>3,
'default'=>1
);
# Make sure to check that something is set first
$id = (!empty($_GET['name']))? trim($_GET['name']) : 'default';
# Set the domain
$redirect = (isset($array[$id]))? $array[$id] : $array['default'];
# Rediret
header("Location: http://{$redirect}.com");
# Stop the execution
exit;
So it looks like your question has been answered above but it's probably not that clear for you, if you're just beginning (using arrays, short php if statements etc).
I'm assuming that you're just learning PHP considering what you're trying to achieve, so here is a simplified answer that is easier to understand than what some other people have posted here:
<?php
// Check that you actually have a 'name' being submitted that you can assign
if (!empty($_GET['name'])) {
$id = $_GET['name'];
}
// If there isn't a 'name' being submitted, handle that
else {
// return an error or don't redirect at all
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
// Else your code will keep running if an $id is set
if ($id == 'joe') {
header('Location: http://1.com');
}
if ($id=marry) {
header('Location: http://2.com');
}
if ($id=katty) {
header('Location: http://3.com');
}
?>
Hope this helps you better understand what's happening.
You should use == for conditional statements not =
if you use = , you say :
$id='joe';
$id='marry';
$id='katty';
if($id='katty') return 1 boolean

Simple javascript PHP default variable issue

I don't know why I can't figure out a way to do this... This is basic programming logic. For some reason, my mind is just not working right today though. I have a page that loads and runs some javascript. When the page firsdt loads, the javascript needs to run using a default variable, however when a user clicks a link on the page, a PHP variable is set which gets sent to the javascript side of things changing the outcome of the javascript if statement. For some reason though, I just cant figure out how to get this to work. Please help.
<?php
$username = $_POST['username'];
?>
<script type="text/javascript">
// Change the username
var phpusername = "<?php echo $username; ?>";
if(phpusername == (undefined || null)) {
(This is where the default condition code will go)
} else {
(This is where code will run when user updates username)
}
</script>
There is a little more to this story as to why I am doing things this way (Using both PHP and Javascript), so please resist offering radically different solutions. There is a LOT more to this puzzle not shown here, however the rest of the puzzle does not necessarily effect the functionality of this part.
Thanks!
<?php
$username = isset($_POST['username'])?$_POST['username']:''; // set fallback value
?>
<script type="text/javascript">
// Change the username
var phpusername = '<?php echo $username; ?>';
if(phpusername == '') { // check if empty
(This is where the default condition code will go)
} else {
(This is where code will run when user updates username)
}
</script>

How to allow PHP file to be only accessible through call reference /view.php?url=123

I want to block direct access to my php file and make it work only through a call reference like: mysite.com/view.php?url=123
And when it is called direct: mysite.com/view.php
Returns: Invalid ID!
My script:
if(isset($_GET['url']) && isset($sources[$_GET['url']])){
$link = $sources[$_GET['url']]; }
After your update in question, you just need to add ELSE condition, if condition false than execute else part, something like:
if(isset($_GET['url']) && isset($sources[$_GET['url']])){
$link = $sources[$_GET['url']];
}
else{
echo "Invalid ID"; // error message
}
But, in your IF condition, you have a problem, suppose if any one try to access this URL:
mysite.com/view.php?url=asdfasdf
Maybe, you code break, in this case, you can also use intval() function, if value of url always integer.
<?php
$url = $_GET['url'];
if(!$url) {
die('Invalid ID!');
}

How to restrict $_SESSION[] using Javascript?

I wanna get just $_SESSION["yetki"] value when I call users function actually I am getting value but always getting "manager" value even if user equal student .
<script>
function users(tik) {
var user = tik.id;
if(user === "student")
{
<?php $_SESSION["yetki"]="student"; echo $_SESSION["yetki"]; ?>
}
else ()
{
<?php $_SESSION["yetki"]="manager"; echo $_SESSION["yetki"]; ?>
}
}
</script>
What you are doing is completly wrong, you are mixing both client side and server side code, javascript is client side code and php is server side language. In your if else condition you need to send request to server to set that session variable. For sending request to server you can use ajax.
Actually, you got all your fundamental understanding of Php and JavaScript wrong. By the time that this script is already running in the client's web browser, the Php scripts would have been processed/executed already and echoed into the document body.
Here's how it works. When you ask for a Php "page", the server would execute every Php script in that page and generate a response. That response would be the one that your web browser would execute.
for example, if you do this:
<script>
if (<?Php echo "true"; ?>) { alert ( 'The server said true' ); }
else { alert ( 'The server didn't say anything' ); }
</script>
The one you'll see in your web browser is:
<script>
if (true) { alert ( 'The server said true' ); }
else { alert ( 'The server didn't say anything' ); }
</script>
What Php does is to create dynamic contents for the webpage and send it back to the client. The client's web browser would then execute the result of that generated content. The Php codes would all be executed as soon as you requested for the web page - the process all happens in the server. JavaScript, on the other hand, would execute AFTER the client receives the web page.
In fact, "echo" is a pretty descriptive term of what Php does. When you type a web address in your browser's address bar and press enter, you are sending a request to the server. Once it "hits" the server, it will "echo" a response in the form of HTML. That HTML would then be read by your web browser and that would include everything from Javascript to CSS. And yes, you can even echo a whole mix of HTML elements and Javascript content. You can even echo the whole document body.
for example:
<?Php
echo
<<<YOURCONTENT
<HTML>
<HEAD></HEAD>
<BODY>You're gonna love my body.</BODY>
</HTML>
YOURCONTENT;
?>
WHAT YOU SHOULD DO FIRST is to validate what the contents of $_SESSION["yetki"] would be.
<?Php
if(your conditions here)
$_SESSION["yetki"]="student";
else
$_SESSION["yetki"]="manager";
?>
<script>
function users(tik) {
var user = tik.id;
if(user === "student")
{
alert('<?php echo $_SESSION["yetki"]; ?>');
}
else
{ // I DON'T KNOW WHAT YOU'RE TRYING HERE, BUT LET'S DO AN ALERT.
alert('<?php echo $_SESSION["yetki"]; ?>');
}
}
</script>
possible conditions for Php if statement:
$_POST['yourFormInputName'] == 'yourRogueValue'
or
$_GET['yourURLVariableName'] == 'yourRogueValue'
or
$_SESSION['perhapsAnotherStoredSession'] == 'yourRogueValue'
you can do with something like this.. but don't know is this a good answer
<script>
function users(tik) {
var user = tik.id;
if(user === "student")
{
var aa = "<?php $_SESSION["yetki"]="student"; echo $_SESSION["yetki"]; ?>"
}
else
{
var aa = "<?php $_SESSION["yetki"]="manager"; echo $_SESSION["yetki"]; ?>"
}
alert(aa);
}
</script>

PHP: Not changing the value of a variable

I have a problem in confirmation message when the USER click CANCEL the value of $IsCanceled = "yes"
then when I click OK the value of `$IsCanceled = "no"..
The problem is, when I click the OK the value of $IsCanceled is still yes...
<?php
else { ?>
<script>
var myVar = "<?php echo $bldg[$i]; ?> station is already full. Do you want to save the other networks?";
if (confirm(myVar)) {
<?php $IsCanceled = "no";?>
} else {
<?php
$IsCanceled = "yes";
?>
}
</script>
<?php
}
//and so on...
I already traced everthing but its still "yes" the value..
Thanks
PHP and Javascript.
Two completely different languages used in various different tasks.
What you have written above tries to mix two languages, which if you were writing in any other environment you wouldn't even consider doing. The above script you have written, will run its PHP when on the server, and produce an output that is sent to the browser.
In your case, that will look something like:
<script>
var myVar = "i station is already full. Do you want to save the other networks?";
if (confirm(myVar)) {
} else {
}
</script>
That is the exact output of your code at the moment. If you want to navigate the user to "save to other networks", you would have to create a hidden HTML form, with an <input type="hidden" .. that can hold the answer you need. Then, with Javascript, you can show your confirmation dialog and populate the HTML form, submit it, and handle it again with PHP.
Think of it like Tennis. You cannot change the way you hit the ball, after already sending it to the opponent. In this case, you can program both sides and make them handle the tennis ball accordingly.
php is server side scripting it can not assigned without page refresh.
Use javascript(client side scripting) variable to assign yes or no
else
{
?>
<script>
var myVar = "<?php echo $bldg[$i]; ?> station is already full. Do you want to save the other networks?";
if (confirm(myVar)) {
IsCanceled = "no";
} else {
IsCanceled = "yes";
}
alert(IsCanceled);
</script>
<?php
}
If you want to do some logic at client side you need to use JS without PHP in it.
And if you need to do logic server side so you need to POST or GET to the server.
Just open your page as source right after it loaded and you will see, that in place where you put PHP condition will be nothing.
It's because PHP already done all it's work. Check $IsCanceled and show page to you.
ALSO note, that if you need to check instead of assign you need to use double equals sign instead one e.g. if($IsCanceled == 'no') will check if variable IsCanceled equals string 'no'. BUT if($IsCanceled = 'no') tells php to assign string 'no' to variable IsCanceled it will be TRUE always because it's assignment and result of assignment is TRUE

Categories