Array value is replaced when second one stores in php - javascript

I am storing session values into array getting from URL and try to display it (getting size of product in a shopping cart).
But first value is replaced by second one.
My code:
if(isset($_POST['radio'])){
$_SESSION['sz']=$_POST['radio'];
$si=$_SESSION['sz'];
}
<a href="product_detail.php?pdt_id='.$pdid.'&add=' .$pdid .'&size='.$si.'"
class="cartBtn" onclick="return fun1()">Add to cart</a>';
?>
Display page:
$rt=$_GET['size'];
$_SESSION['wer']=$rt;
$array = $_SESSION['wer']; //Assigns session var to $array
//print_r($array);
echo $array[$x];
}

About First part mistakes :
<?php
if(isset($_POST['radio'])){
$_SESSION['sz']=$_POST['radio'];
$si=$_SESSION['sz'];
}
Add to cart';
?>
Your a tag is completely wrong as #Hearner said. It should be out of the php tag or inside with an "echo" like this :
echo "<a href='product_detail.php?pdt_id=".$pdid."&add=".$pdid."&size=".$si."' class='cartBtn' onclick='return fun1()'>Add to cart</a>";
You cannot access your $si variable out of your if statement. As written here over, if your $si isn't declared before (since you said that it was not your complete code...) then $si (in the link href) does not exist. You should therefore declare it before your if statement OR place your link (a tag) inside your if statement too!
What if your "$_POST['radio']" is NOT set ?? what happens? code missing... !! is $si declared anyway?
About Second part :
<?php
for( $x = 0, $max = count($array); $x < $max; ++$x ) {
$rt=$_GET['size'];
$_SESSION['wer']=$rt;
$array = $_SESSION['wer'];
echo $array[$x];
}
?>
I don't understand what you're trying to do here... need more code/information... cannot help more without your whole code...
EDIT :
Here is a very simple example to show you how to keep your get vars into a session array.
page one (pageOne.php):
<?php
session_start();
if(!(isset($_SESSION['myTest']))){
$_SESSION['myTest'] = "AWESOME";
$_SESSION['varToKeep'] = [];
}else{
echo "A session is already started. This is : ".$_SESSION['myTest']."<br/>";
if(count($_SESSION['varToKeep']>0)){
echo "There are ".count($_SESSION['varToKeep'])." vars in the array!<br/>";
for($i=0;$i<count($_SESSION['varToKeep']);$i++){
echo "Item ".$i." : ".$_SESSION['varToKeep'][$i]."<br/>";
}
}
}
echo "<br/>Click below to add a value in array<br/>";
$random = rand(1,100);
echo "<a href='pageTwo.php?mygetvar=STACKTEST".$random."'>Click here</a>";
?>
page two (pageTwo.php):
<?php
session_start();
echo "myTest value is : ".$_SESSION['myTest']."<br/><br/>";
$value = $_GET['mygetvar'];
$_SESSION['varToKeep'][] = $value;
echo "<a href='pageOne.php'>CLICK HERE TO RETURN ON PAGE ONE!</a>";
?>

Related

Get javascript variable value in php

I need to get javascript variable value in php file.
html example:
UPDATE:
$html = '
<script>
window.runParams.adminSeq="3423423423423";
window.runParams.companyId="2349093284234";
</script>';
Shout I use regex ? regex is very complex to me... any help ?
<?php
$html = '<script>
window.runParams.adminSeq="3423423423423";
window.runParams.companyId="2349093284234";
</script>';
$variables = ["adminSeq", "companyId"];
$counter = 0;
foreach($variables as $variable) {
preg_match_all('/"(.*?)"/', $html, $matches);
${"$variable"} = ($matches[1])[$counter];
$counter++;
}
echo $adminSeq; // Prints out: 3423423423423
echo $companyId; // Prints out: 2349093284234
?>
You can also use GET requests to do this. The link would look like http://localhost/?adminSeq=3423423423423&companyId=2349093284234 then get out these values in PHP with:
<?php
$adminSeq = $_GET["adminSeq"];
$companyId = $_GET["companyId"];
?>

Referencing a defined html variable in JS

My index.php page includes a config.php file, which returns an array that I have defined some variables in by using "define('var1' , 10)".
I am trying to validate my forms input, but I can't figure out how I can reference var1 from within the JS function. What is the easiest way to do this?
Just echo it to a javascript variable:
<script type="text/javascript">
var var1JS = "<?php echo $var1; ?>";
</script>
Not quite sure I am full understanding without seeing the code but, you could echo the variable from the PHP array in the JS function (as above answer).
Or Echo the entire JS query:
$y = count($PHPdata_array);
echo "function exampleFunction() {";
echo "var ArrayName = [";
for ($i = 1; $i <= $y; $i+=2) {
echo "{" . $PHPdata_array[$i] . "," . $PHPdata_array[$i-1] . "},";
}
echo "];";

How to pass dynamic PHP variable as parameter in window.open method

I am trying to create a window pop up using window.open method in javascript, it works fine simply without any dynamic variable passing, but i am not getting how to pass the php variable inside the function to make it functional in another window.
Here is what i have tried so far:
<?php
if(isset($d->retweeted_status->id_str)){
$tweetId = $d->retweeted_status->id_str;
} else{
$tweetId = $d->id_str;
}
$myUrl = "retweetDetails.php?id=".$tweetId;
<?
<?php echo $d->retweet_count; ?> Retweets
I want to pass this $myUrl variable as a url parameter which will reflect in popup window.
Thanks
<?php echo $d->retweet_count; ?> Retweets
<?php
if(isset($d->retweeted_status->id_str)){
$tweetId = $d->retweeted_status->id_str;
} else{
$tweetId = $d->id_str;
}
$myUrl = "retweetDetails.php?id=".$tweetId;
<?
<?php echo $d->retweet_count; ?> Retweets

How to retrieve $_SESSION value in a JavaScript/HTML

I have this working script yet when I change it to retrieve(supposedly) the value inside $_SESSION["username"], it doesn't retrieve anything. The whole page is saved as .php as I am using some codes that uses PHP.
Code:
echo "<script type=text/javascript>";
echo "var hidden = false;";
echo "function actiondb1() {";
echo "if(!hidden) {";
echo "document.getElementById(\'clickdb1\').style.visibility = \'hidden\';";
echo "document.getElementById(\'db1\').style.visibility = \'visible\';";
echo "document.getElementById(\'db1\').disabled = false;";
echo "document.getElementById(\'db1\').value =".$_SESSION["username"];.";";
echo "}";
echo "}";
echo "</script>";
How can I make the script to properly retrieve the data inside $_SESSION["username"];?
Observe that, for instance, if the value of $_SESSION["username"] is John, your echo will be generating this result:
document.getElementById('db1').value = John;
But John is supposed to be a string and should be wrapped in quotation marks, otherwise JavaScript will understand it as a variable name (which value will be probably undefined).
As Havenard mentioned, this line is missing Javascript single quotes to properly denote a string variable:
echo "document.getElementById(\'db1\').value ='".$_SESSION["username"];."';";
However, you really shouldn't print JS out with PHP if you can help it. Though iatboy's answer answer won't ultimately fix your bug, it is a much cleaner way of doing things.
?>
<script type=text/javascript>;
var hidden = false;
function actiondb1() {
if(!hidden) {
document.getElementById('clickdb1').style.visibility = 'hidden';
document.getElementById('db1').style.visibility = 'visible';
document.getElementById('db1').disabled = false;
document.getElementById('db1').value ='<?php echo $_SESSION["username"];?>';
}
}
</script>;
<?php
Did you start session in this page?If you didn't,use the follow code to start session.
session_start();
Then change your code to
echo "<script type=text/javascript>";
echo "var hidden = false;\n";
echo "function actiondb1() {\n";
echo "alert('".$_SESSION['username']."')\n"; //test code
echo "if(!hidden) {\n";
echo "document.getElementById('clickdb1').style.visibility = 'hidden';\n";
echo "document.getElementById('db1').style.visibility = 'visible';\n";
echo "document.getElementById('db1').disabled = false;\n";
echo "document.getElementById('db1').value ='".$_SESSION["username"]."';\n";
echo "}\n";
echo "}\n";
echo "</script>";
it will be ok.

Setting JS variable to PHP variable error

I want to use a php login name in my javascript game...
I have this at the top of my php page that runs the javascript:
<?php
$username = (isset($_GET['username']) ? ($_GET['username']) : "dude");
?>
In my javascript I have this... which doesn't alert the name at all and throws a Uncaught ReferenceError: daniel is not defined error:
var name = <?php echo $username; ?>;
alert(name);
And then I have this... which displays the name correctly at the top of the HTML:
<?php
echo "Welcome back " . $username . "...";
?>
If the name is being displayed correctly server side when the page has loaded, why can't it be alerted out from the Javascript?
Thanks
You aren't delimiting the value as a JavaScript string. It may be a string on the server, but not on the client. Do the following:
Change this:
var name = <?php echo $username; ?>;
To this:
var name = "<?php echo $username; ?>";
Enclose your php code within quotes.Try like this :
<script type="text/javascript">
var name = '<?php echo $username; ?>';
alert(name);
</script>

Categories

Resources