Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
I have a PHP script that generates html. In the bottom part of the script I do some computations using PHP. I want to echo the result in the top part. Is this possible.
//script begins
<?php
....
//I want to echo the result here
....
....
....
//PHP computations, result in $result
//I do now want to echo result here but in the top part of the output document.
?>
//script ends
More Information- The output is an html document. I do some computations in the body part for which there is no workaround. I want to echo the output in the head part.
You need to make the computation where you need it. like this
<?php
// computations here and store the values in variables
$value = 'xyz';
// or define a function here that you can use
function func ()
{ return false; }
//html here, some exmaples
?>
<div><?php echo $value;?></div>
<h1><?php echo func();?></h1>
?>
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
how can I specifically convert this php below code to html? I want to do this as I want to be able to run this code on my website.
From reading other forum posts I understand about people saying them link the PHP file externally using something like this php. However, from my understanding my PHP code is slightly different in terms of the output and I'm just not sure.
Below is my php code I want to run externally on my site from the html file.
<?php
echo $_SERVER['HTTP_USER_AGENT'];
$browser = get_browser();
print_r($browser);
?>
Can you use Ajax and show the result in HTML
<script>
$.ajax({
urL:"./tomy.php",
type:"POST",
data:{getuserAgent:1},
success:function(data){
$("#mydiv").html(data);
}
});
</script>
tomy.php
<?php
if(isset($_POST['getuserAgent']){
echo $_SERVER['HTTP_USER_AGENT'];
$browser = get_browser();
print_r($browser);
}
?>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I just messed up with is a problem from the last couple of hours and still didn't get the solution i m new in php.what I m trying to do is display and hide the dive according to php condition.
here is the scenario:
note: by default DIV(display:none)
if(condition is satisfied ) //condition in PHP only
//call the javascript function which displays the div
else
//call the javascript function which hides the div
if you have the solution with example please must share it's a humble request
thank you
You want to mix Javascript, HTML and CSS as little as possible. It might look easier to mix it right now, but that will change as you get more experience.
There are multiple ways, but one I prefer is running PHP and passing the variables to a JS variable and let JS decide:
const showSpecificElement = <?php echo funtionWhichDoesSomething(); ?>;
if( showSpecificElement ){
// Javascript magic here
}
// Or, alternatively:
if(<?php echo funtionWhichDoesSomething(); ?>){
// Javascript magic here
}
// Or, slightly better:
$classWhichHidesThisDivOrNot = rand(1,2)==1 ? 'hideMe' : 'showMe';
<div class="<?=$classWhichHidesThisDivOrNot?>"> ... </div>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
below is my link code
detail.php is used to show detail of row by using row id.
My question is ...Is It Possible to pass complete link in certain variable and and only access this variable except of attaching page on every link.
Or Any function where i put my page name detail.php and access this function on link.
plz suggest me....
for exampl -
$var = detail.php and used this var like below link.
Not like below link.
">
Not sure if I understand your question correctly but I think this is what you are looking for:
$rows; //variable with all database rows
$baseString = "details.php";
foreach ($rows as $row)
{
$pageString = $baseString . "?id=" . $row['id'];
?>
<h2><?php echo htmlspecialchars_decode($row['headline']); ?></h2>
<?php
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I would like to have all PHP errors printed so a specific spot on my page, namely between:
<div id="errors">
</div>
I've seen custom error handling functions, but these do not seem to cover all error types. I can't even think of a way to do this with JavaScript, because the only similarity I see between errors is that they are enclosed mostly within b tags, and I would like to avoid regex if possible.
If you create an $errors array, you can add each error to that array and then iterate over it when displaying to the user.
Ex:
$errors = []; // add conditional statements below this and if the error arises, add it to the array.
foreach($errors as $error) {
echo $error['value'] . '<br />';
}
I have in the past gathered errors into an array as the page is loading, then at the bottom of the page when the page is finished loading, if there are errors, the php script will write a jQuery script to the div container similar to this:
<div id="errors"></div>
<?php //if something goes wrong write error
$_error['type'][] = 'error1'; ?>
<html stuff>
<?php // something else goes wrong
$_error['type'][] = 'error2'; ?>
<more html>
<?php if(isset($_error)) { ?>
<script>
$('#errors').fadeIn('fast');
// This part you could loop if you had lots of types or come up with some
// elaborate javascript to populate your div
$('#errors').html('<?php echo implode("; ",$_error['type']); ?>');
</script>
<?php } ?>
A suggestion is to have an array which stores all the error information and a boolean variable (e.g. isDebugMode) that tells whether errors should be printed or not.
Then use a for-loop to print out all the errors inside of the div you want, formatting the output with interspersed html.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to switch a task of jQuery to PHP.
Earlier it was like :
myString += "Hello There ! ";
$("#latestRoutes").prepend(myString).slideDown(2000);
But now As I am switching to PHP, How can I do the $.prepend function in PHP to append a variable to a particular string ?
On page load if the variable isset then show it as a first child of your div like this:
<div>
<?php if(isset($mystring)) echo '<p class="test">'.$mystring.'</p>'; ?>
<p>...</p>
<p>...</p>
</div>
but since this is php is served only on page refresh and of course you cannot have fancy js animations at once, some extra work needs to be done. If you want this to communicate with js add a class there and with js check its existence and then start its animations. This can be done like:
if($('.test').length > 0) {
alert("This message is brought by php, great!!!");
$('.test').on('click', function(){
$(this).hide();
});
}