PHP echo out jQuery in IF statement - php

EDIT: What a TW*T. Sorry everyone for wasting your time. Just missed a Google jQuery link on one F'in page. Whoops.
Hi, i have a div containing 3 forms. These should be the only thing on the screen on page load. When any of them are submitted, a graph gets shown below. What i'm trying to do is within the PHP IF statement is make the div disappear that contains the forms. Sound simple?
This is my code:
if($_GET['submit1']){
echo "<script type='text/javascript'>$('#options').css('display','none');</script>";
However, when i do submit one of the forms (therefore a $_GET has occurred) the div is still there??
EDIT:
If i try people answer on one line i get this:
Parse error: syntax error, unexpected '(', expecting T_VARIABLE or '$'
But if i put in people's multiline answers, no error, but div still shows!

Why do you want to hide the forms with JavaScript?
Simply do it with PHP:
<?php
if(!isset($_GET['submit1'])) {
?>
//<form> your form HTML here
<?php
} else {
?>
<p>Your submitted data: <?php print_r($_GET); ?></p>
<?php
}
?>
So your forms are only shown if you have NOT submitted one of them. You might have to adjust the parameters if you have multiple forms, for example
if(!isset($_GET['submit1']) && !isset($_GET['submit2'])) {
Edit: If you want to keep your forms after submitting but only hide it, you could do it that way:
<?php
$formsVisible = !isset($_GET['submit1']));
$formsDisplay = $formsVisible ? 'block' : 'none';
?>
<form style="display:<?php echo $formsDisplay; ?>">
<!-- ... --->
</form>

You need to add a DOM ready event:
if($_GET['submit1']) {
echo '<script type="text/javascript">' . "\n";
echo ' $(function() {' . "\n";
echo ' $("#options").hide();' . "\n";
echo ' });' . "\n";
echo '</script>' . "\n";
}

edit
You are getting a parse error because you're using double quotes, so the php parser is reading the dollar sign as a php variable. I would switch to a single quote php syntax to make your life easier:
if($_GET['submit1']){
echo '<script type="text/javascript">
$(function(){
$("#options").css("display","none");
});
</script>';
}
Be sure to wrap your jquery code in the onLoad function $(function(){};

Related

display div inside php using echo

I want display my button if it have isset($_GET). I am trying to do like this.
<?php if(isset($_GET['project_id'])){
echo '<div class="add_btn_primary"> Project Users </div>';
}?>
its giving me error like
Parse error: syntax error, unexpected 'project_id' (T_STRING), expecting ',' or ';' in C:\xamppp\htdocs\mayank\add_project.php on line 101
I am not getting idea what I should do for echo project_id in div. Let me know if someone can help me for that.
Thanks
Thats incorrect to use echo inside another echo and how can you start a new php tag without closing the first.
The correct way is to concatenate the variable along the string passed in echo, here is how
<?php if(isset($_GET['project_id'])){
echo '<div class="add_btn_primary"> Project Users </div>';
}?>
instead of breaking the php tags break the ' quotes to concatenate the value in the string.
Why do you need again tag inside echo just use it as below:
<?php
if(isset($_GET['project_id']))
{
echo ('<div class="add_btn_primary"><a href="manage_project_users.php?project_id='.$_GET["project_id"].'>Project Users</a></div>');
}
?>

JQuery function not executing inside a PHP loop

I have a php array of result $bank_r. I want to perform some results in all of the results. For that I wrote :
$(function(){
//alert('<?php echo count($bank_r); ?> ');
<?php
for($i=0;$i<count($bank_r);$i++)
{
$bank_name = strtolower (str_replace(" ","",$bank_r[$i]['bank_name']));
?>
alert('<?php echo $bank_name; ?>');
<?php } ?>
-------------
----------
//Some other jquery functions
//
//
});
I was expecting to alert the $bank_name, but it is not. Even the top alert('<?php echo count($bank_r); ?> '); also not alerting anything. But If I remove the php for loop, the top alert alerts the number of results. Whats wrong ?
EDIT:
Generated javascript code:
<script>
$(function(){
//alert('5 ');
alert('ucobank
');
alert('pnb');
alert('bob');
alert('sbi');
alert('hdfc');
//Other javascripts
});
Try this,
<?php
for($i=0;$i<count($bank_r);$i++)
{
$bank_name = strtolower (str_replace(" ","",$bank_r[$i]['bank_name']));
echo '<script> alert("'.$bank_name.'");</script>';
// if you are outside the javascript then use script tag, otherwise remove the tags
}
?>
New line in first alert is problem. It causes JS parse error and because of that alerts will not appear. You have to replace newline to eg. spaces by str_replace("\n", ' ', $string);.

jQuery Show php GET variable value in a textarea dynamically

Im trying to show GET variable data in a textarea. I tried may solutions in StackOverflow but none of them worked. I have no idea why. Can you please tell me if I'm missing something.
My Story
I have a link on a page and href is /index.php?page=contact-us&performance=Lamborghini
and it takes me to the Contact Us page and the url is still /index.php?page=contact-us&performance=Lamborghini which is what I want.
So, Im tyring to grab performance variables's content which is Lamborghini and print it in a textarea which is
<textarea name="m62b34fbrp__32" cols="19" rows="7" class="cms_textarea" id="fbrp__32"></textarea>
So what I did is added <?php $performanceInfo= $_GET['performance']; ?> to the top of my page. Then added $("#fbrp__32").val(<?php echo $performanceInfo; ?>); in my script.js file. But it never prints Lamborghini in the textarea.
When I try $("#fbrp__32").val('<?php echo $performanceInfo; ?>'); (Notice the ' marks) it prints <?php echo $performanceInfo; ?> in the textarea.
When I <?php echo $_GET["performance"]; ?> it prints Lamborghini fine too.
Can you guys tell me why its not working for me?
NOTE : I cant edit the textarea manually as it is generated using a plugin Thanks a lot.
PHP Parse will not parse .js files, you will need to do this outside of the .js file. If not you will need to in your webserver config file set to parse .js files as PHP files.
EDIT
You mentioned that you added <?php $performanceInfo= $_GET['performance']; ?> to the top of the page and then $("#fbrp__32").val(<?php echo $performanceInfo; ?>); to your script.js file. This will not work as per default configuration script.js or any .js file will not be interpreted by the PHP parser. so your statement $("#fbrp__32").val(<?php echo $performanceInfo; ?>); will cause a syntax error.
The best way to handle it without changing server configuration would be to do this at the top of your page.
<script type='text/javascript'>
var performance = "<?php echo $_GET['performance']; ?>";
</script>
and in your script.js file do $("#fbrp__32").val(performance);
Try this
<textarea name="m62b34fbrp__32" cols="19" rows="7" class="cms_textarea" id="fbrp__32">
<?php echo $_REQUEST['performance']; ?></textarea>
OR in JS
$(document).ready(function(){
var performance='<?php echo $_REQUEST["performance"]; ?>';
$("#fbrp__32").val(performance);
});
Just add the following right before the textarea (in your PHP/HTML-file, not JS-File!):
var performance = <?php echo '"' . htmlspecialchars( $_GET['performance'] ) . '"'; ?>
$("#fbrp__32").val(performance);
Just set value directly using PHP
<?php
$performanceInfo= $_GET['performance'];
?>
JQuery is a bit tricky.Instead try simple javascript for setting value:
document.getElementById("Enter Here Textarea Id").value = <?PHP echo $_GET['performance']; ?>
Ok. Finally this is the solution which worked for me.
In the php file I put
<?php
$performanceInfo= $_GET['performance'];
echo "<script>";
echo "$('#fbrp__32').val('$performanceInfo');";
echo "</script>";
?>
Its weird other solutions didnt work for me for somereason.
Try this
var value = <?php $performanceInfo= $_GET['performance']; ?>
$("#fbrp__32").val(value);
NB: php tags will not not render in js files

Javascript inside php not working properly

<div class="interactionLinksDiv">
REPLY
</div>
I have call the javascript function toggleReplyBox with five parameters. This code is written inside the php tags. But this code is not executing properly and the parameters are not being passed properly. If I call the function toggleReplyBox here with no parameters it works fine but thats not what I want.
<div class="interactionLinksDiv">
REPLY
</div>
When I copied this code to the html part of my php file It works fine and the parameters are passed and the function executes properly.
But I want to know why the function is not able to work inside of the php tags when everything is the same.
function toggleReplyBox(sendername,senderid,recName,recID,replyWipit) {
$("#recipientShow").text(recName);
document.replyForm.pm_sender_name.value = sendername;
document.replyForm.pmWipit.value = replyWipit;
document.replyForm.pm_sender_id.value = senderid;
document.replyForm.pm_rec_name.value = recName;
document.replyForm.pm_rec_id.value = recID;
document.replyForm.replyBtn.value = "Send";
if ($('#replyBox').is(":hidden")) {
$('#replyBox').fadeIn(1000);
} else {
$('#replyBox').hide();
}
}
Inside the php tags I changed the code :
print <<<HTML
<div class="interactionLinksDiv">
REPLY
</div>
HTML;
And it is still showing the error
Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\htdocs\Fluid Solution\fluid-solution-website-template\interact\profile1.php on line 130
Line 130 is the <a href... line.
The first version of your code is neither PHP (javascript/HTML tags are "naked") nor Javascript: the "." string concatenation operator won't work in Javascript, nor will the $variable expansion.
You can get it to work in PHP like this:
<?php
$fullname = "Test";
$current_id = 15;
$id = 9;
$thisRandNum = 42;
// All lines beyond this point, and...
print <<<HTML
<div class="interactionLinksDiv">
<a href="javascript:toggleReplyBox('$fullname','$current_id',
'$current_id','$id','$thisRandNum')">REPLY</a>
</div>
HTML;
// ...up to here, start at the first column (i.e. they are not indented).
?>
Note that within the here-document (area between <<<HTML and HTML), you can't use the string concatenation operator "." (or any other).
Or you can do as you did in the second version of your code, replacing only the variables with <?php echo $variablename; ?> and leaving all the rest as HTML.
As a simpler example let's consider an alert() box with message sent from PHP. This means that:
1) the script is executed server side; anything between <?php ?> tags is executed, and its output replaces the tags themselves.
After this phase, we no longer have PHP but a mix of HTML and Javascript, which can be executed by the client it's sent to. So we want to have a HTML like
<script type="text/javascript">
alert('Hello, world');
</script>
To do this we can generate all the HTML in PHP:
echo '<script type="text/javascript">';
echo "alert('$message');"; // or also: echo 'alert("' . $message . '");';
echo '</script>';
Or we can do it with a here-document, where operators do not work, but $variables do:
echo <<<HEREDOCUMENT
<script type="text/javascript">
alert('$message');
</script>
HEREDOCUMENT;
Or we can run it all in HTML, and only rely on PHP to generate the lone variable:
<script type="text/javascript">
alert('<?php echo $message; ?>');
</script>
But always you need to keep separated what it's being done in PHP, what in Javascript, and what is in the HTML markup.

Jquery to parse HTML in a string

I tried searching the related posts, and having a hard time figuring out how to fix my query - I'm pretty close, any help is much appreciated (new to Jquery).
I program in PHP, and trying to pull either the HREF value from a tag, or the text. Either will work.
I basically have my HTML code in a string, might contain multiple tags, and would like to load the text of the tags into either a PHP array or variable (right now just trying to ALERT the results, I can dump it later).
My PHP Code:
<?php
$info = '<li><strong>I want this text</strong>';
echo '<script type="text/javascript">';
echo '$("document").ready( function () {';
echo 'alert($("a", $("' . $info . '")).html());';
echo '});';
echo '</script>';
?>
The above doesn't alert anything. Putting in
echo 'alert("yes")';
does work, so I'm guessing there's something basic wrong with my syntax, but 4 hours later still unable to find it! :)
Thanks in advance.
You aren't Javascript-escaping the quotes in your string.
Your code creates Javascript that looks like
$("<li>...<a href="http..."...")
The quotes in the attribute end the Javascript string, creating a syntax error.
You need to call json_encode.
SLaks has the rest of your problem. But also, it's not:
$("document").ready();
It's:
$(document).ready();
The former is a selector for a tag named <document>.
This should work the way you want it to:
<?php
$info = '<li><strong>I want this text</strong></li>';
?>
<script type="text/javascript">
$(document).ready( function () {
alert($("a", $("<?php echo $info; ?>")).html());
});
</script>
You are not closing your li Tag
$info = '<li><strong>I want this text</strong>';
should be
$info = '<li><strong>I want this text</strong></li>';
You should escape info. It's breaking because you've got double quotes inside of double quotes:
$info = addslashes($info);
or
$info = json_encode($info);
or just
$info = str_replace('"', '\\"');
Try to save the HTML is a JS variable first, then use it. Also, heredocs are your friend.
<?php
$info = '<li><strong>I want this text</strong></li>';
echo <<<END
<script type="text/javascript">
$(function(){
var HTML = '$info';
alert($('a', $(HTML)).html());
});
</script>
END;
?>
echo '<script type="text/javascript">
$(document).ready( function () {
var info = \''.$info.'\';
$("a").html(info);
alert(info);
});
</script>';

Categories