php checking div style block or none - php

I am trying to check my div style also with my code here
<?php
$Vidvid="Ab9kbmIAyLE";
if(isset($_GET["Vidvid"]))
{
$Vidvid=$_GET["Vidvid"];
}
?>
I don't know much in php , I think if(isset($_GET["Vidvid"] means id or name that gets in url like page.php?id=Vidvid am i right in this ?
If it's right, then I want to make it to check a div style also.
like if link is page.php?id=Vidvid and if div style is block
What i mean is to add something on if like checking div style also
2 Code
<?php
$feed = "hello";
if(isset($_GET["feed"])) {
$feed = $_GET["feed"];
echo 'yes';
} else {
echo 'no';
}
?>

You’ve got it the wrong way round.
// page.php?Vidvid=…
$Vidvid="Ab9kbmIAyLE";
if(isset($_GET["Vidvid"])) {
$Vidvid=$_GET["Vidvid"];
}
Data sent on the query string is stored in the $_GET array. The name=value pairs are translated to $_GET[name]=value in the array. In your code example, you’re looking for ?Vidvid=….
If you’re really sending ?id=, you should be checking for $_GET['id'].
// page.php?id=…
if(isset($_GET['id'])) { // does id=?
$value=$_GET[id]; // get value
}

Related

Replace Content on Page Based on URL Parameter with PHP

I'd like to replace content within my page based on the URL parameter.
Ideally I'd like to use PHP to get:
if {{parameter is X}} display {{content X}}
if {{parameter is Y}} display {{content Y}}
..for a few pages.
Current set up:
<?php if ($CURRENT_PAGE == "Index") { ?>
<div id="firstDiv">this is the standard page</div>
<?php } ?>
<?php if ($CURRENT_PAGE == "p1") { ?>
<div id-"secondDiv">this is a variation of the page</div>
<?php } ?>
And using include("includes/content.php"); to call the html blocks to the page
The firstDiv displays in index.php as expected, but adding the URL parameter changes nothing - the same div still shows (I'd like it to be replaced with the secondDiv)
It seems $CURRENT_PAGE doesn't like URL parameters - what is the alternative?
Hopefully this makes sense, I'm pretty new to PHP. Happy to provide more details if required.
Thanks in advance for any help.
-- UPDATE --
Thank you for the answers so far!
It seems I missed part of my own code (Thanks to vivek_23 for making me realise this - I'm using a template, excuse me!!)
I have a config file that defines which page is which, as so:
<?php
switch ($_SERVER["SCRIPT_NAME"]) {
case "index.php/?p=1":
$CURRENT_PAGE = "p1";
break;
default:
$CURRENT_PAGE = "Index";
}
?>
Before I learn $_GET, is there a way I can use my current set up?
Thanks again.
-- UPDATE 2 --
I have switched to using the $_GET method, which seems to be working well so far. My issue now is when the parameter is not set it is giving an undefined error. I'll try to remember to update with the fix.
$p = ($_GET['i']);
if($p == "1"){
echo '<div id="firstDiv"><p>this is the first div</p></div>';
}
Thanks to the two answerers below who suggested using $_GET
You can used $_GET like
if($_GET['p']==1){
echo '<div id="firstDiv">this is the standard page</div>';
}else if($_GET['p']==2){
echo '<div id="secondDiv">this is a variation of the page</div>';
}
The other way! you can used basename() with $_SERVER['PHP_SELF']
//echo basename($_SERVER['PHP_SELF']); first execute this and check the result
if(basename($_SERVER['PHP_SELF']) == 'index'){
echo '<div id="firstDiv">this is the standard page</div>';
}else{
echo '<div id="secondDiv">this is a variation of the page</div>';
}
You need to send the parameters on the URL query string, like:
yourdomain.com?p=1
So, with this URL, the query string is "?p=1", where you have a GET parameter named 'p' with a value of '1'.
In PHP to read a GET parameter you can use the associative array $_GET, like this:
$current_page = $_GET['p'];
echo $current_page; // returns '1'
The rest of your logic is OK, you can display one div or the other based on the value of the p parameter.
You can read more about how to read query string parameters here: http://php.net/manual/en/reserved.variables.get.php

Execute jquery in if statement

I got a login system, and what I want to do is to hide a div and show another div when the user types the incorrect login details. However it doesn't seem to work.
if(...) { // here I check if the user enters the correct information
... //much code inside
} else { // if incorrect info, do this
echo "<script>$('#jqueryhide2').hide();
$('#jqueryhide').show();</script>";
}
Tried to google a bit but can't find anything that could solve my problem.
put this code and also include your Jquery file -(dont forget)
echo "<script>
$('document').ready(function(){
$('#jqueryhide2').hide();
$('#jqueryhide').show();
});</script>";
I think you should missing the Onclick function like this:
<script>
$('loginId').Onclick(function(){
$('#jqueryhide2').hide();
$('#jqueryhide').show();
});</script>";
You don't need the script tags inside your statement. You can simply do the following. ( Make sure jqueryhide and jqueryhide2 are a part of the DOM. )
if (blah == blah )
{
$('#jqueryhide2').show();
$('#jqueryhide').hide();
}
else
{
$('#jqueryhide2').hide();
$('#jqueryhide').show();
}

Passing unknown link variables to a new page

I have a link, an offer page and a destination page. I need to carry the variables from the original link and input them into the links on the offer page.
original link
www.example.com/offerpage.php?offer=1&aff_id=var1&aff_sub=var2
Where you see var1 and var2, those could be any number.
I'm assuming I could do something like this (this is a total guess, just want to make sure I do it correctly).
<?php
if(array_key_exists('aff_id', $_GET)){
$aff_id = $_GET;
}
else {
$aff_id = '1';
}
?>
Then the links on the offer page would be
www.offer.com/index.php?offer=1&aff_id=<?php echo $aff_id; ?>&aff_sub=<?php echo $aff_sub; ?>
and whats the correct format for doing multiples?
This should probably do what you want:
if (!array_key_exists('aff_id', $_GET)) {
$_GET['aff_id'] = 1;
}
echo http_build_query($_GET);
If the query string is offerpage.php?offer=1&aff_id=var1&aff_sub=var2then the output will be:
offer=1&aff_id=var1&aff_sub=var2
And, if the query string doesn't contain aff_id, i.e. offerpage.php?offer=1&aff_sub=var2 then the output will be:
offer=1&aff_sub=var2&aff_id=1

Jquery - change font color based on value of sql result

I'm looking to change the font color of a div using jquery where the div is populated by the output of a SQL query.
I have:
$(document).ready(function(){
$('#foo').each(function(){
if ($(this).text() == 'bar') {
$(this).css('color','orange');
}
});
});
From a SO search which works fine when the div contains text.
But as this is SQL i'm populating the div with: ".$row['result']."
And this now does not work. I'm guessing this is because the sql, although being a varchar field is a $variable and isn't 'text' as such?.
I'm sure this is something simple, but i'm struggling to phrase this in google to return anything useful.
Many thanks.
edit
The whole thing is rather long and before i've tried to add the jquery was all working fine, so i'll just post the additions.
This is within the head:
echo "<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js'></script>";
echo "<script type='text/javascript'>";
echo "$(document).ready(function(){ $('#foo').each(function(){ if ($(this).text() == 'bar') { $(this).css('color','orange');}});});";
Then i echo each row in a while loop:
$sql = "SELECT...";
$result = mysql_query($sql)or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "<div id='foo'>".$row['result']."</div>";
}
The whole document is wrapped in PHP but its not the source of the issue as if i change the div to contain text rather than ".$row['result']." then the jquery executes on it just fine.
You are giving every div the same id ("foo"). An id has to be unique in HTML, you would be better off using a class for this. The way you have it now the .each() function would only be called on one element, possibly the first.
Change the HTML output like this:
echo "<div class='foo'>".$row['result']."</div>";
Then, adapt your selector in jQuery accordingly:
$('.foo').each(function(){
// ...
}
Do you use a $.ajax call or just emmbed the value via PHP on page load?
If you use PHP to print the value, I guess you forgott to echo the value:
".<?php echo $row['result']?>."
Not just:
".$row['result']."

show hide div based on mysql table row value

hi guys im trying to show and hide div according to mysql value but i couldnt do it can you help me what im doing wrong
here is my code thanks a lot for your ideas
var Value = <?php echo json_encode($valuek) ?>;
if (Value==1){
$('#show_hide').show();
}
else{
$('#show_hide').hide();
}
<?php
$valuek = $session->userinfo['vcc'];
?>
<div id="show_hide">
some code
</div>
<?php echo json_encode($valuek) ?>
will return a json string, instead try just using "echo"
<?php echo $valuek ?>
If all you are going for is a boolean value then there is simply no need for JSON.
Echo the value directly into the JavaScript. Remember to ensure you are passing a valid boolean value.
PHP code -
<?php
$showDiv = ($dbValue == 1? 'true' : 'false');
?>
JavaScript + PHP injection -
<script>
var value = '<?php echo $showDiv; ?>';
<script>
Don't forget to wrap the PHP injected value with quotes.
$valuek = $session->userinfo['vcc'];
I'm not sure if you have the code in this order in your php file, or just showed pieces of code in this order, but Should go BEFORE your js code. It has no value when js code is run.
To see what $valuek is, just echo it on top of the screen
<?php echo "<h1>$valuek</h1>" ?>.
Or just look at the source - at your js function, to see what is printed after 'var Value ='
That's the main thing really - to make sure that you getting what you expect from session.
And as been said, you don't need jason_encode, but you do need a semi-colon after echo command.
Also, I hope your jquery code is within $(document).ready function, not as is.

Categories