Jump to content

Call function w/ onClick w/ PHP_SELF


OldWest

Recommended Posts

I have been messing with this for a few hours, and I cannot get this to work with my click. On the first example, the php runs regardless of click or not - basically when the page loads it runs... And the second, I cannot figure out how to store the while loop results in a variable to pass to the page.. I would prefer to use my first method as it's much cleaner and less code. IS this even possible? Basically I just want to run my function ONLY when the button is clicked.

 

<a href="" onCLick="<?php myFunction(); ?>"/>Click to run function</a>

 

OR

 

<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="myFunctionRun" value=""/>
<input type="submit" value="Run Function">
</form>

 

Link to comment
Share on other sites

Php is a server side scripting language. Any php code on a page is executed when the page is requested.

 

onclick events occur in the browser.

 

The only way that a browser can communicate anything to a server is by making a HTTP request to the server.

 

If you want a HTTP request to cause a specific php function to be executed on a page, you must pass some identifying piece of information to that page as part of the HTTP request and the code on that page determines that is needs to call the function in question.

Link to comment
Share on other sites

OldWest, an AJAX solution could be used here... which is a whole other animal.

 

Try sending HTTP requests to the script itself via a form or a bit of javascript attached to a button and pass a $_GET var like such:

 

<button name="clickMeButton" type="button" onclick="window.location='index.php?var=true'">Click Me!</button>

 

 

Link to comment
Share on other sites

PFMaBiSmAd,

 

I'm just to sure the simplest and most effective way to handle my problem. Any insight to that would be helpful.

 

Here are the applicable snippets of my code. As you can see in my input name... I would like to store the array results in there, but I was not able to store the data in the results output array

 

mysql_output

<form method="post" action="download_to_excel.php">
<input type="hidden" name="arrayResults-HowDoIStoreThemHere?" value=""/>
<input type="submit" value="Download to Excel">
</form>

 

I need to pass the below results generated in the while loop to a variable to pass to the download_to_excel.php page... Is this the best way to do this? And if so, can you recommend or point me to the right way to do this?

 

echo "<p><table border='1' cellpadding='3' width='100%'>";
echo "
<tr>
<th>Updated Code 1</th>
<th>Updated Code 2</th>
</tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array($result)) {

//START :: If to find matching criteria class it w/ bg color
if ($row['orig_code_1'] < '10000') {
		$changedClass = "changedClassCSS";
} else {
		$changedClass = "";
};
//END :: If to find matching criteria class it w/ bg color

//START :: Print out the contents of each row into a table
echo "<tr><td class=\"$changedClass\">"; 
echo $row['orig_code_1'];
echo "</td><td class=\"$changedClass\">"; 
echo $row['orig_code_2'];
echo "</td>"; 
}
echo "</table></p>";
//END :: Print out the contents of each row into a table

// START //
//echo "$tableOutput";
// END //

 

 

download_to_excel.php

<?php
$tableOutput = $POST_['tableOutput'];
/* 
Here we create a simple text format like CSV with a delimiter \t tab and new line delimiter for rows
thats all
*/
$data_headers=array('orig_code_1','orig_code_2');
$data_array[]=array('orig_code_1','orig_code_2');
// here we prepare a simple  \t delimetered text format 
$delim_string='';
$delim_string.=implode("\t",$data_headers)."\n";
foreach($data_array as $k=>$v){
$delim_string.=implode("\t",$v)."\n";
}

//make time dependent xls name that is always unique
$xlsfile = "excel_example".date("m-d-Y-hiA").".xls";
//stop the browser displaying the data as text
//force the browser to download as xcel document
header('Content-type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=$xlsfile");

print $delim_string;
?>

 

Link to comment
Share on other sites

OldWest, an AJAX solution could be used here... which is a whole other animal.

 

Try sending HTTP requests to the script itself via a form or a bit of javascript attached to a button and pass a $_GET var like such:

 

<button name="clickMeButton" type="button" onclick="window.location='index.php?var=true'">Click Me!</button>

 

Tried this. The problem is no data in my spreadsheet when I use this method... The problem is I need to pass the data from my while array to the download page... And that's what Im trying to solve.

Link to comment
Share on other sites

For any of you who were wondering a solution (might not be the best!, but it works!)... I realized I could test the post status with isset, and then with an if include the file I am trying to process. Here is my code. Enjoy!

 

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<!--<input type="hidden" name="post_var">-->
  <input type="submit" name="submit" value="Download to Excel">
</form>

// Testing "submit" for isset... And if true, include the download function file.. works!
<?php
if (isset($_POST['submit'])) {
		include('download_to_excel_1.php');
	};
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.