I have searched all over the internet to run a function from URL and found out it is not possible.
I am asking if there is another way to do this. I will explain below what I am trying to achieve.
On my website I have several functions that when a hyperlink is clicked on that cracks.php page, the main content changes accordingly. Now I want to direct people from a single URL to that cracks.php page with the function already called.
Example - www.example.com/cracks.php?AC ( which will call a function named AC and the content changes before the page loads)
Ive found this method below, but couldnt get it to work.
if(document.location.search == '?AC')
{
AC();
}
Sorry for the messy code on the website. Thanks for reading, any help would be appreciated.
You can call www.example.com/cracks.php?do=AC and then get do with $doMethod = $_GET['do'];. What you then do is, use a switch function or a few ifs to check and execute when e.g. $doMethod equals AC.
Like this:
$doMethod = $_GET['do'];
switch($doMethod)
{
case "AC":
//some random stuff to do
break;
case "BD":
//some random stuff to do
break;
case "XY":
//some random stuff to do
break;
default:
break;
}
That depends if you need to do that dynamically or you can do it hard coded. Because that hard coded is too simple (with if's and switches), what you have to do is:
$functionsList = Array('func1', 'func2');
function func1(){
echo '1';
}
function func2(){
echo '2';
}
if (function_exists($_GET['f']) and in_array($_GET['f'], $functionsList)){
call_user_func($_GET['f']);
}
Then call your_file_name.php?f=func1 and your_file_name.php?f=func2 and you'll see different outputs.
With the help of Mark Koopman I managed to use his Javascript method and it worked like I wanted.
So heres the method in Javascript:
<html>
<body>
<script>
function handleOnload()
{
if(location.search == "?AC")
alert("the query string is " + location.search);
}
window.onload=handleOnload;
</script>
</body>
</html>
Related
Here is my question
I have a class say ABC and a function XYZ defined inside it and that function is returning something based on the above logic in it.
class ABC {
function XYZ{
..........
.......
return "--- ";
}
}
Now there is an object on another page of this class which calls this method.
$z= new ABC;
if( $z->XYZ() )
{
some output
}
else{
another output
}
My problem is i dont have access to PAGE 2. but i need to change the output of else statement. I have only access to class ABC because i am overriding it in a module. so, in short i can only alter function XYZ. Whats the solution??
If its of any significance, i am working on MAGENTO ECOMMERCE Platform and class is a block class and page 2 is a template
You can alter the output html with an observer with the http_response_send_before event.
Capture the html and do your stuff.
You have a good explanation here
I hope this can help you
you can also do as follow :
<?php
ob_start();
// do your stuff
// ...
// here you capture all the output that it is generated by your scripts
$v = ob_get_contents();
// alter the value of $v by detecting if it should be changed. you can user regex for example to
// detect and update
// ...
// here you clean all data stored in buffer
ob_clean();
// and put the updated data in buffer
print $v;
// end of buffer operations
ob_end_flush();
?>
hope it helps,
You need a way to differentiate PAGE1 from PAGE2, lets say by the URL if it's possible. So you would have to change your method that will check on which page is currently and according to it, it will change the output, but you must write the output in the method itself instead of writing it in the template.
class ABC {
function XYZ {
$result = $this->getResult(); //Helper method to get the result.
if ($result) {
//Do something when result is true.
} else {
$url = $this->checkUrl(); //Check URL of the page.
if ($url == PAGE2) { //If you are on PAGE2
//Do something specific for the PAGE2
} else {
//Do something for all other pages
}
}
}
}
I know it's not the perfect solution, but I hope it will help you somehow.
The page number has to be specified somewhere in the $_GET or the $_POST array (or in another query). Check that variable and implement the alternative logic.
I'm working with a page that, once a link is called this script checks and if the POST contains the keyword it and then finds that page. However no matter how I organize this if it doesn't work.
<?PHP
if($_POST['page']) {
$page = (int)$_POST['page'];
$exists = file_exists('pages/page_'.$page.'html');
if($exists) {
echo file_get_contexnts('pages/page_'.$page.'html');
} else {
echo 'There is no such page!';
}
} else if ($_POST['course']) die("0"); {
$course = (int)$_POST['course'];
$exists = file_exists('courses/course_'.$course.'html');
if($exists) {
echo file_get_contexnts('courses/course_'.$course.'html');
die("1");
} else {
echo 'There is no such page!';
}
}
?>
The error I'm currently receiving with this setup is:
Notice: Undefined index: course in C:\wamp\www\Home Page\load_page.php on line 12
Call Stack
# Time Memory Function Location
1 0.0003 253944 {main}( ) ..\load_page.php:0
Is it because there is no 'course' in the page? I might be confused of the code I'm modifying a tutorial of a simple ajax website. It is possible what I am trying to do does not work.
In that case how could I possible go about doing what I want to do.
Right now I have a home page and it loads in another page without switching pages. I like the floridness of it. I would like to have a sort of sub call. So if you are on the home page and you go to courses page then you can click on a specific course and that will load from a different directory within the courses directory.
Homepage (when you click on courses you go to...)
pages/courses_home.html (when you click on a course you go to...)
courses/course_1.html (you can view course and then click back to directory above or go to home)
That is the structure I'm looking to try to achieve.
If more information is needed please let me know what and I'll do my best to include it. Thank you.
The syntax should be:
if(isset($_POST["page"])) {
} elseif(isset($_POST["course"])) {
}
I am not sure why you have a die statement there, but I don't think it belongs. Also, keep in mind the logic for what happens if neither of these conditions is met.
Edit: also keep in mind that isset doesn't prevent empty strings, so you may want to check for that as well. A function you could use is
function checkPost($value) {
return isset($_POST[$value]) && $_POST[$value] !== "";
}
To use:
if(checkPost('page')) {
//some logic
}
Wrong syntax.
elseif ($_POST['course']) {
without die statement.If 'course' undefined else statement works and does not get error. Sorry for bad English.
Try this:
if isset(($_POST['page'])) {
...
} else if isset(($_POST['course'])) die("0"); {
instead of this:
if($_POST['page']) {
...
} else if ($_POST['course']) die("0"); {
I have several links on a page, all of which need to execute a different method in PHP.
A link for "Create File", one for "Rename File" and one for "Delete file".
The only way I know of to execute PHP code with each hyperlink is by giving the URL of a different PHP file to each hyperlink.
Is there a way to link the URL (HREF) to a SPECIFIC method in a PHP file?
Here are a few lines of code that do not work, but might help you understand what I want to achieve:
Create File
Rename File
Rename File
I'm also pretty sure I'm taking the wrong approach, but I'm too much of a PHP novice to know better :)
Common approach in your situation would be setting a GET parameter called something like fileAction and then switching it's value, so your links would look like this:
Create File
Rename File
Delete File
And your filemanagement.php logiŃ would look somewhat like this:
<?php
$fileAction = $_GET['fileAction'];
switch ($fileAction) {
case 'create':
createFile();
break;
case 'rename':
renameFile();
break;
case 'delete':
deleteFile();
break;
default:
//your default logic here
break;
}
?>
you can send the method name as a $_GET value and execute the corresponding function, like so:
HTML:
Run some method
PHP:
<?php
if (isset($_GET['method'])) {
$method = $_GET['method'];
$method();
}
<?php
#$Method = $_GET['Method'];
if (!(isset($Method)))
{
$Method = "0"; // If URL does not contain ?Method=1 Then Assign It To 0
}
if ($Method > "3")
{
$Method = "0";
// If ?Method= value is higher than 3 Then Set it to 0
}
if ($Method == "0")
{
echo "Create File";
echo "Rename File";
echo "Delete File";
}
elseif ($Method == "1")
{
// Method = 1 Is Create File So:
// CreateFile();
}
elseif ($Method == "2")
{
// Method 2 is Rename File
// renameFile();
}
elseif ($Method == "3")
{
// Method 3 Is Delete File
// deleteFile();
}
?>
What you are describing is the perfect scenario for using an MVC framework. There are tons of these frameworks out there and they help you in organising your code better. If you are just starting out in PHP, I highly suggest you to look at a framework and some code samples on the web in order to get your head around the MVC principle.
When using an MVC framework, you simply define routes which map to a particular function (action) in a particular class (controller). I won't get too far in this answer, but I suggest you go through these ressources to get you started :
MVC definition on Wikipedia
CodeIgniter, an MVC framework for PHP
I've been trying to get user input from a Javascript prompt into a PHP function and have run into a lot a walls doing so. I am at a loss trying jQuery's $.post method- as the PHP simply does not want to execute, not sure why.
Anyways, here is a gist of what I am doing at the moment:
1 A project and its data are loaded from a database- this info is displayed in a table.
2 All data in the table is editable via Javascript prompt(), the code I am using for this is below:
<div id="lvl3"><?php echo $fetchdata['name']; ?></div>
The above work as such: lvl3 is a tag for font styling; blank href to make it act as a link; popupprompt is the prompt function I made, it takes one argument, the 'type' or what is being edited (1 for project name, 2 for project description, ect); return false so the page doesn't reload; php echo to display project data in the table.
3 Once the user clicks the object above- it executes a javascript function called popupprompt taking an argument of 'type', or what project info is being changed. the code for this function is below:
function popupprompt(type) {
switch(type)
{
case 1:
var name = prompt("Project Name:", "");
if (name != null && name != "")
{
//Change Project Name
var getname = name;
var gettype = type;
$.post("edit.php", { type: gettype, name: getname });
} else if (name == "") {
senderror("Please enter a valid Project Name");
} else {
//Prompt canceled
sendnotification('Canceled my ass!');
}
break;
case 2:
//Description?
case 3:
//Version?
case 4:
//Release?
default:
alert("There was an error processing your request.");
break;
} }
Th issue I am having in this function is that nothing in edit.php is executed- and i haven't the slightest clue why. Also, i've had to change the brackets around so it shows properly in the code box- so don't mind those.
4 Anyways, now user input is posted to edit.php- which doesn't work, but i'll post it anyways:
<?php
$type = $_POST['type'];
$name = $_POST['name'];
switch($type) {
case 1:
dbconnect();
$urlext = geturlext();
$authenticated = isauthenticated();
if ($authenticated == false)
{
echo("<script>senderror('Access denied');</script>");
} else {
//Escape and trim input
$input = trim($input);
$input = mysql_real_escape_string($input);
$update = "UPDATE 'projects' SET 'name' = '$input' WHERE 'name' = '$urlext'";
$updatequery = mysql_query($update) or die(mysql_error());
echo("<script>sendnotification('Project Name updated');</script>");
}
break;
default:
break;
}
?>
Again had to move some brackets around. But anyways- this function is supposed to update the data in the database- however, it instead does nothing. I've placed alerts in the beginning and they are never called.
Anyways, long story short- if you know what i'm doing wrong please enlighten me, also, if there is a better way to do this- please let me know!
I appreciate all help,
Thanks
Figured it out after a good week of setting it aside. Took a long look at it in firebug, rooted out a typo that was causing most of my issues and also a MySQL syntax error.
I don't know why, but my custom error reporting system does not work when called from a file in this manner. It might be another thing i'm overlooking but firebug sure helped.
Thanks to those who tried to help <3
I have a function in Wordpress that I BELIEVE should be getting the URL of the page a visitor is sitting on. It's as follows:
function get_request_url() {
$formurl = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
Then, for fun, I have a PHP enabled widget with:
<?php echo get_request_url() ; ?>
However, it always shows no value. I don't get errors, just... nothing.
I'm a pretty novice coder, and I hacked this together from a few different sources (I know, I know... search engine code snippet alchemy can be bad for your health), so I think I've either skipped a step, or missed the point entirely.
Can anyone point me in the right direction?
Your function needs to add a return
function get_request_url() {
$formurl = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
return $formurl;
}
Try:
function get_request_url() {
$formurl = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
return $formurl;
}