I'm trying to learn PHP, and to do so im trying to create a code to register new products, i've made an loggin access where the person to do so needs to be connected, so inside of the cpanel I did differents sections, like add client, add product...
So i did an index in the _cpanel folde where i have my main html and to not repeat the same html i put an include inside this index so i can call others php files inside.
I dont know if its correctly to do like this BUT, as i said Im new at programming..
But everything works pretty good, until i need to call my post in my url..
like.. to add a new product i need to go to :
localhost -> connect... to go to localhost/_cpanel
if i click in add a new produt, i go to localhost/_cpanel/?page=folder/addnewproduct.php
and inside this php file i put a form with an action="?go=addnewproduct"
so, i did everything to put all the info in my db, but when i submit it goes to
localhost/_cpanel/?go=addnewproduct and nothing happens..
i tried to change the action like action="folder/?page=addnewproduct.php&?go=addnewproduct" but it doesnt work either.
BUT i made it works changing to action"folder/addnewproduct.php?go=newproduct
but to do so i need to go to "addnewproduct.php", and this page only is an included page inside my index..
i could call back to the index page after adding a new product, so that it doesnt show this page only, but when posting it loads fast to the page and after than it go back to my _cpanel.
i tried to find solutions but, didnt find... i think its just a problem with the way i put it in my action, maybe theres a way to do it and i dont know it yet.
I don't think I get what you're wanting exactly, but here you go:
If you're trying to redirect the request, you may just use the header function as follows:
header('Location: target/url.php');
If you want to make a external post request with php, you may use curl, as suggested here:
PHP + curl, HTTP POST sample code?
Related
I'm trying to use header() in PHP so it will send a GET request, and also redirect the user to a certain div ID, and originally the code I came up with was this:
header("Location: ?load=10#divName");
However, that gave a 500 error code, due to "redirecting me too many times".
I then tried doing something like this:
header("Location: #divName?load=10");
However, this just completely ignored the GET request, which makes sense, since it thinks it's part of the div ID.
Should I just give up and try using a POST request instead, or is there a way around this? Thanks!
Note: I only am using header() for tests, otherwise, it is connected to an <a> tag.
P.S. If you are wondering what I'm trying to do over here, when you click a button, it sends a request to load more blog posts, and then it directs the user to the last blog post in the list that was just loaded. I changed the div ID for less confusion. Please let me know if there is a better way around this, I'm sure there is!
First using a header tag, you need to use the full URI if you want to redirect to somewhere. I recommend using the global $_SERVER for it. documentation
Second, the thing that you want to do is achievable using javascript and ajax, instead of php. I recommend you research the ajax documentation to see some examples.
But mainly to fix the main problem of "redirecting me too many times", check if you are looping something that can callback the header function more than once.
Currently I'm building a plug-in for wordpress, where I have a couple of webpages with tables, filled with data from a database. Right now I'm working on the part where I click on a number (MemberID) which should redirect to a page, having all the information from another table regarding this number. I've tried transfering the data through the URL, with both
?MemberID=".$item->MemberID."
and
/MemberID="$item->MemberID."
, but everytime I try to alter the URL, the webpages suddenly can't seem to find itself anymore. I'm sure it has to do whith this part of the code:
add_submenu_page( null , 'Manage memberDetails' , 'memberDetails' , 'manage_options', 'memberDetails' , 'memberDetails_list' );
where the fifth value sets the URL for this page to be
http://localhost/wordpress/wp-admin/admin.php?page=memberDetails
So whenever I try to add something behind that URL, it doesn't acknowledge the existance of said page anymore, resulting in a redirection error.
Is there a way to transfer the variable without using the URL, or is there a way to give more of a dynamic to the URL?
You are using add_submenu_page wrong. The first param is required and can not be null.
https://codex.wordpress.org/Function_Reference/add_submenu_page
You need to get that fixed first so you page properly works.
As someone pointed my fairly basic mistake out this is what went wrong:
The URL is
http://localhost/wordpress/wp-admin/admin.php?page=memberDetails
where I tried to turn it into
http://localhost/wordpress/wp-admin/admin.php?page=memberDetails/memberID=
which creates a whole new webpage and is therefor impossible in this case. I need to transfer the variable behind the memberID=, not refer to a whole new webpage. The other thing I tried to do was
http://localhost/wordpress/wp-admin/admin.php?page=memberDetails?memberID=
which is impossible, because the second ? in the URL needed to be replaced with a &
From there on, it was pretty easy to use $_GET to retrieve the variable from the URL and use it.
So for extra clarity, this is how the URL is supposed to look
http://localhost/wordpress/wp-admin/admin.php?page=memberDetails&memberID=
I've look around, and not found anything yet. So I'm going to ask a question, sorry if this is 'nooby'
What I want to do is this:
Have a page with a form on it, IE: Form.php
and I want to allow myself to edit another page, IE: index.php
Kind of like a really BASIC two page CMS, edit it on the form.php page, and then it saves on index.php doesn't overwrite, but saves it under the current post that's already there.
Sorry for the 'vague' question, however want to do this fast :P
You can do it the way Patashu said with a database but if your only going to be editing one page you can do it with http://php.net/manual/en/function.fopen.php.
On the Form.php have it open index.php using PHP then have a textarea field that echos out the index.php and then using the Fopen function save over index.php. Make sure you secure Form.php with a password or even using a database. It also depends what programming language you will have inside index.php
Instead of editing HTML or PHP using a PHP file, you should make a database, store content in it that you want to be dynamic, and retrieve from the database when the PHP for the dynamic page executes. Go read about SQL :)
(reposting so it can be chosen as selected answer, if you want!)
OK - Thanks for taking the time!
I am using WP on a CentOS 6 server. I have a plug in with the functions, I have a function that makes a DB call and populates relevant products based on $_GET variables, I took from one of the other project and modify it so it works! But here where I run in to the problem, I go to the main page and i have a function that gets called first, goes through the URL name and determines the categories id and then from that I need to pass that to the URL so that when the next function then calls $_GET["category_id"] and that ID is there and ready to be use and it it does its magic. (all staying on the same page no refreshing or anything)
So I need to put that on the URL as the page is being loaded and so that I can use it (Again i get the variables from a function that is doing all the work with the address for relevant info,) So how do i do it? HTML or PHP, and a straight forward way no extra installs would be nice :)
Edit 1:
So is there something then I could integrate in that would be simple and straight forward that would allow me to do a mini refresh and get the right variables in place, never used JavaScript but seams to be getting or something in php ... Ideas are welcome :)
You can with javascript and the history API
The only way you can change the url without actually redirecting the user is by using the pushState method.
e.g. open a console and copy and paste this:
var stateObj = { foo: "bar" };
history.pushState(stateObj, "changes url to stackoverflow.com/yes-you-can", "/yes-you-can");
You won't be redirected, and the url of your browser will change unless, basically, you're using IE 9 or less. You can see a demo of this on html5demos.com
OK here is how I am going to get around this problem I am having
I made a new table in the DB and then I already have a list of the Domain We are using, so then I am going to give to the customer there are three columns and they will manually enter the fields and it will be on them to manage and change what they want displayed on the webpage.
CVS import and then BAM! done! just pull $_SERVER["SERVER_NAME"] and then compare that to the domain column and done! (I will have what ever cat's and sizes they want and it will not be on us to create any complicated functions and if statements for exceptions and it is in there hands!)
Not the exact answer I wanted to get but much easier and not so much complicated :)
I didn't know how to ask professional so if topic is wrong, please correct it.
My problem is quite complicated. I was training on symfony webpage how to generate form and then send this data to another page.
But now i would like do something different. I would like create page xyzSuccess.php.
Then generate 29 links on this page. Each links would have its own number.
Each link would redirect to the same page. For example numberSuccess.php. After that, this site would give me data of the number which i clicked.
I will show the example :)
Page xyzSuccess.php have links, the third one is http://localhost:8080/web/number?nr=3
I click on it and i go to numberSuccess.php, the page give me number 3
After the page got number, also take from any datafile information under that number 3 ( i don't know, maybe any file wchich can store symfony / array somewhere?.)
I dont know how to do this, i don't want use any database eq. *sql
So far i created only page xyz.php which use php for loop. I see that action.class.php would work with it if i would use submit button, but i really dont have any idea how make this working with links. And... that problem with storing data :( maybe create file in lib/form ?
corrected the link to page (deleted success which used only in templates), guy under is right :)
First of all, you're talking about URL's like /web/numberSuccess... That probably means you've misconfigured the root. Also see this question, asked today.
Then it's common to give your actions names like number, and then in you're actions file you have the executeNumber action, which then renders the numberSuccess template. So you shouldn't include the Success in the action name/URL.
Then, your question:
What I would do, if you don't want to use a database:
Create a file links.txt in your /data directory, and on every line create an url, followed by a space and then the title of your link.
In your list action open the this file using file_get_contents(), explode it on the newline-character, and assign this array to the view.
In your view loop through the array, use the array index as your number, and explode each item on the first space, so you can display the title.
In the view action your open and explode the links file again, and now you can pointer directly to the index, explode again and you have the URL.