javascript method onclick is executing even submit button is not yet clicked - php

Im new to php and javascript so please bear with me.
index.php:
<?php
$_SESSION['test'] = 1;
?>
<div>
<?php echo "Before: " . $_SESSION['test']; ?>
<input type="submit" value="CLICK" onclick="<?php $_SESSION['test'] = 0; ?>;" />
<?php echo "After: " . $_SESSION['test']; ?>
</div>
Why is it that $_SESSION['test'] is already 0 when I haven't clicked the button yet??? Please help me...

PHP is a preprocessor. Everything you write in PHP is executed BEFORE the page is presented, while javascript executes clientside as the page is running. Therefore, you cannot set a PHP value with a javascript event.

PHP is a server-side language; it is parsed and run before anything is even sent to the browser. It does not interact with JavaScript.

You will need to use AJAX to call the php set the session on click via javascript. I suggest having a look at XMLHTTPRequest: http://www.w3.org/TR/XMLHttpRequest/ , or if you don't want to read all of that and learn it, I suggest looking at a javascript library such as http://www.jquery.com, which should simplify what you need to do.

You need to call session_start() before anything can be stored in the session.

The way you are trying to do it is impossible.
Use AJAX

Related

PHP variable creating link for button

So I wanted to make an easy way for me to see my school projects. I tried to make in webpage HTML input and output as a clickable link that contains mysite.com/"Input".I tried using PHP variable probably in wrong way.
I tried using PHP variables to make output for the button.
<input type="button" value="View item" onClick="parent.location='/view/<?PHP echo $output ?>.html'"/>
Thanks!
although i do not know how you are going to fetch the php variable and from where, i am giving an option. you can write the function like this:
<?php
$input_vars = 'some-place';
?>
<button onclick="onclickRedirect()">redirect</button>
<script>
function onclickRedirect(){
window.location.href = "http://www.yoursite.com/<?php echo $input_vars ?>";
}
</script>
make sure you do all in a php file. hope this will help you.

PHP - Call a function from a link\button

I know that PHP is only server-side and it's impossible to call directly a PHP-FUNCTION from a link.
But I can't use JavaScript \ jQuery \ Ajax
This is my code in main.php
function refreshgt2(){
for($l=1; $l!=$max_cicle; ++$l ) {
$data->query("INSERT IGNORE INTO `table` (`name1`,`name1`,`anothername`,`dog`) VALUES ('ME','".$img[$l]."','".$l."','".$hello[$l]."')");
}
}
And the html,
<a href="#" >Do something</a>
I've just tried with this need a button that calls a php function but didn't work for me.
Someone can help me?
I need something that load the function only when I will press the link\button\image
this should work. it gives a button, support multiple function call and is pure php + html
<form action="<?php $_PHP_SELF ?>" method="post">
<input type="hidden" name="function" value="refreshgt2">
<input type="submit">
</form>
<?php
if($_POST!=null && array_key_exists('function', $_POST)){
if(strcasecmp($_POST['function'],'refreshgt2')==0){
for($l=1; $l!=$max_cicle; ++$l )
{
$data->query("INSERT IGNORE INTO `table` (`name1`,`name1`,`anothername`,`dog`) VALUES ('ME','".$img[$l]."','".$l."','".$hello[$l]."')");
}
}else if(strcasecmp($_POST['function'],'some_other_function')==0){
//do things
}
}
?>
But i cant's use a javascript\jquery\ajax
Then the answer is No, you can not. Only option is to link your button to a new page which will generate the PHP output. PHP itself does not know anything about what you do at client side and whether you have clicked a link, PHP functions cannot execute at that time since PHP has already done its job and gone.
If you dont mind a redirect
<a href="your_php_script.php" >Do something</a>
you_php_script.php
<?php
refreshgt2();
If you can't use javascript/ajax (why not? we are in 2013), you can't refresh part of the page on the fly. Your only chance is to call a page like
Do something
and my-script.php then renders the page again after doing some stuff.
Use querystrings, set the href in your html to point to your page, and add a querystring parameter so that your php can decide which function to execute:
HTML
<a href='main.php?fn=some_function'>Do something<a/>
PHP
switch($_GET['fn']) {
case 'some_function':
//call your function
break;
default:
//Handle this
}
Otherwise, redirect to another php script like the other answers mentioned.

Issues with calling a JavaScript function from PHP

I have created a form to upload a a newsletter into the database. I'm using the iFrame method to post the date without the page refresh and I'm displaying a nice jQuery dialog for the loading.
I am now having issues with closing that dialog when the upload is complete. All the tutorials I have read online say that I must just echo out the code like this:
<?php
echo "<script type='text/javascript>uploadComplete();</script>";
?>
Now the JavaScript does run when I do something stupid to test like just echo out an alert, but when I try and call any function or just go straight to the jQuery to close the dialog it just says that the function is not defined. I can post my code if it is necessary, but it's pretty standard and I didn't think I would need to in this kind of example.
Any help would be appreciated! Thanks in advance.
you forgot ' in the end of 'text/javascript'
echo "<script type='text/javascript'>uploadComplete();</script>";
NB : the type attribute is entirely unnecessary if the script is JavaScript
using just php
echo '<script type="text/javascript">'
, 'jsfunction();'
, '</script>';
escaping from php mode to direct output mode
<?php
// some php stuff
?>
<script type="text/javascript">
jsFunction();
</script>
I eventually solved this by using the following code:
<?php
echo "<script type='text/javascript>top.uploadComplete();</script>";
?>
The "top" keyword just solved all of my problems. I hope this helps whoever has the same issue I had!

PHP refresh window? equivalent to F5 page reload?

Is there anything in PHP that is the equivalent of manually pressing the F5 page reload button? My php script is in a frame and isn't the parent script but it needs to refresh the entire page and not just it's frame.
Actually it is possible:
Header('Location: '.$_SERVER['PHP_SELF']);
Exit(); //optional
And it will reload the same page.
With PHP you just can handle server-side stuff. What you can do is print this in your iframe:
parent.window.location.reload();
If you have any text before a
header('Location: http://www.example.com/youformhere.php');
you'll have issues, because that must be sent before any other text is sent to the page.
Try using this code instead
<?php
$page = $_SERVER['PHP_SELF'];
echo '<meta http-equiv="Refresh" content="0;' . $page . '">';
?>
Just remember, this code will create and infinite loop, so you'll probably need to make some conditional changes to it.
PHP cannot force the client to do anything. It cannot refresh the page, let alone refresh the parent of a frame.
EDIT: You can of course, make PHP write JavaScript, but this is not PHP doing, it's actually JavaScript, and it will fail if JavaScript is disabled.
<?php
echo '<script>parent.window.location.reload(true);</script>';
?>
<?php
echo "<script>window.opener.location.reload();</script>";
echo "<script>window.close();</script>";
?>
with php you can use two redirections.
It works same as refresh in some issues.
you can use a page redirect.php and post your last url to it by GET method (for example).
then in redirect.php you can change header to location you`ve sent to it by GET method.
like this:
your page:
<?php
header("location:redirec.php?ref=".$your_url);
?>
redirect.php:
<?php
$ref_url=$_GET["ref"];
header("location:redirec.php?ref=".$ref_url);
?>
that worked for me good.
Use JavaScript for this. You can do:
echo '
<script type="text/javascript">
parent.window.location.reload(true);
</script>
';
In PHP and it will refresh the parent's frame page.
guess you could echo the meta tag to do the refresh in regular intervals ... like
<meta http-equiv="refresh" content="600" url="your-url-here">
All you need to do to manually refresh a page is to provide a link pointing to the same page
Like this:
Refresh the selection
Adding following in the page header works for me:
<?php
if($i_wanna_reload_the_full_page_on_top == "yes")
{
$reloadneeded = "1";
}
else
{
$reloadneeded = "0";
}
if($reloadneeded > 0)
{
?>
<script type="text/javascript">
top.window.location='indexframes.php';
</script>
<?php
}else{}
?>

If anchor is clicked php

I'm trying to echo information on a page after and anchor has been clicked
<a id="anchor">Information</a>
<?php
if(?){
echo 'INFORMATION';
}
?>
<a id="anchor" onclick="document.getElementById('information').style.display='block';">Information</a>
<div id="information" style="display:none"><? echo 'INFORMATION' ?></div>
I think you must use javascript here. PHP would need a page refresh to process your echo statement.
That's where you might want to utilize AJAX. You can often approach it like this:
<a onClick=" $('#output').load('output.php') ">click here</a>
<div id="output"><!-- This is where the content goes --></div>
Then define the according PHP script output.php like this:
<?php
echo $whatever;
?>
jQuery will then issue another HTTP request, invoking a PHP script, and finally injects it where you told it to (#output div).
Since PHP (your httpd, exactly) doesn't know anything about anchors, you'll need to handle this with javascript. Try jQuery.
You cant do this in PHP I'm afraid. PHP lives on the server and the page is on your client (browser). In order to do something (other than go to another page) when some clicks, you'll need to use javascript. Look at jquery.
You can't do it like this.
PHP works in the server side, therefore once the anchor information has come to the client browser, you won't be able execute any PHP code there.
There are several workarounds if you really want to achieve that.
i) Use client side JavaScript.
ii) Use Ajax to maker requests to server side and update page accordingly.
If you really want to show the information after clicking into a link can use the following code:
<html>
<title>lasdfjkad</title>
<head>
<script type="text/javascript">
function showhide(id){
if (document.getElementById){
obj = document.getElementById(id);
if (obj.style.display == "none"){
obj.style.display = "";
} else {
obj.style.display = "none";
}
}
}
</script>
</head>
<body>
Show/Hide Details
<div id="abc" style="display:none;">
your codes......
</div>
</body>
</html>
The real beauty of Javascript.....Hope this help you

Categories