How to Manipulate template content at runtime in php? - php

I want to add or manipulate the content of template through a PHP page on runtime.
For example, In my online shopping website, I have a division for shopping cart in template then how can I change that division's content through a PHP page when user add an item to cart..
I don't want to assign values to that division on each page so, I put that division in template.
Please Help me with this..
Thanking you in advance for any Interest shown..

You will need to use Javascript in order to do that - well at least if you don't want the page to reload.
There are plenty of examples all over the web (search for setting up cookie/session variable using Javascript).
Using Javascript libraries such as jQuery would help you a lot.
For example:
User clicks on "add to cart"
Javascript updates a cookie variable with the item's information
Javascript updates content of the cart on the page
Optionally, use AJAX to communicate with the server and do something on success/error.

Related

Reload PHP If/Else Statement in cart footer without reloading whole page? ( XT Floating Cart)

The code is as below:
I am getting a total number of items from woocommerce cart and I am showing a specific footer on a specific quantity.
** Everything is working fine but the problem is i want it to update the footer also with floating cart body without reloading the whole page.**
Every time I have to reload
Want to Achieve:
Update Footer Div/Page According to quantity means a user need to have a total of 10 items in the cart to checkout
But the whole page should not reload it just should be the floating cart.
By the way I am using XT floating cart
Will be really Appreciated
<?
$min = 10;
if(WC()->cart->cart_contents_count < $min)
{
xt_woo_floating_cart()->get_template( 'parts/cart/custom_footer' );
}
else
{
xt_woo_floating_cart()->get_template( 'parts/cart/footer' );
}
?>
Shams what you're attempting to achieve is unfortunately not what PHP was built for.
When you submit a request to PHP it runs through the code, interprets it and renders the page server side, before sending the HTML, CSS and JS to the user. Therefore, you cannot reload just part of page without reloading the whole page.
One way you can achieve your desired functionality is to create a controller. Creating a controller means you create a PHP function that sits at at specific web URL. You can parse an object to this URL and the URL can parse you an object back.
Once you have created the controller you need to create the caller. Usually these front end calls are made using Javascript. You can wait for a user to complete an action than then use AJAX to send a request to your controller. Your controller can return some data and you can use javascript to manipulate the DOM to alter the page without reloading the whole page.
Please note that this is just one way of achieving your desired functionality and requires some knowledge of programming. However, if you're determined and happy to learn you can use the information above research how to program an approach like this.

How to use php in wordpress template to modify on-page details?

There must be a standard answer to this question which must come up all the time, but I haven't been able to find the solution.
I am trying to set up the return page from a PayPal payment on a Wordpress site. I have a button on the sales page which sends the transaction to PayPal and once completed PayPal sends POST data back to a url for a page that I specify on my site.
The responding page on my site needs to perform a confirmation handshake with PayPal, read the POST data, email me with the details, and display a table to the customer with the details. All except the last action can easily be done with php, but displaying the table (which should be the easiest) is where I'm stuck.
I have a draft "Thank you for your order" page on standard page in Wordpress. I have created a custom template file which can be selected from the Templates dropdown selector on the Edit page. The custom template can host the php code to perform the handshake, get the POST data and email me with the details.
However, the template (which is a copy of the Atahualpa theme's index.php file) renders the page by various Wordpress function calls, and I don't want to interfere with those. However, this means that I can't directly control the page rendering using echo commands in php. With javascript or jquery I could allow the page to load and then modify the innerHtml, but this seems to be a clumsy way of doing it, and it would probably be visible to the user, as page elements are first rendered then modified.
What's the best way to do this?

Displaying PHP result in the same page

I want to add custom PHP to my wordpress site.
I managed to do it but the result is displayed in another page and it is just simple text.
I created simple calculator, and when user hits submit I want a div with the result to occur right below this calculator.
What is the simple way to do it or where can I seek some advice? Couldn't find it on google.
PS I am using wordpress themes so I added the action for the form as another file.
You could use javascipt to toggle visibility of your calendar div after response or add your custom div into response. You didn't wrote much. At first you should check out what is AJAX and find out where your response is creating.
If you didn't choose to add that for response, you can simple add PHP block to your templates files with <?php ... ?>.
Good luck! :)

How can I change the value of a $_GET variable without using a link?

The website I am working with is a property sales site where the user can freely advertise their property. Properties can be for sale or for rental: http://ev-villa.com
The search panel I designed has 2 tabs: For Sale and To Rent. To get a clearer understanding of the following problem I would advise you to load the site from the above URL. As the site is multi-lingual you may have to change the language to English by clicking on the flag icon in the top right of the page.
So the tabs in the search panel (left side of the page) can be clicked by the user and this tab will now change colour to show that it has been selected. This is handled by a variable in the URL called saleType. So if the value of this variable is equal to "torent" then the "To Rent" tab will change colour (The css on the page changes when the variable's value changes). Apart from changing colour this variable also affects the search results gained when the user clicks the search button.
The issue here is that the variable change is done by a link which requires reloading/redirecting. So if these tabs are clicked a few times, the result is that the user will have to click the browser's back button quite a few times to leave the website or to get to other parts of the website.
I am basically looking for an alternative solution to changing the value of this variable in php without using a link so that the value is set without redirecting the browser to a new page. I am trying to avoid the use of JavaScript and other languages, to ensure maximum cross-browser and cross platform compatibility, making sure this website works for browsers that don't support JavaScript or don;t have JavaScript enabled at the time of use.
I wasn't happy with my previous answer, so I gave it a little bit more thought.
Here's a workaround, completely without JavaScript and without requesting the server:
Instead of 1 form for both types of search (rent/sale), create two identical forms but with different action attributes.
<!-- Rental -->
<form action="search.php?saleType=torent" method="get">
...
<!-- Sale -->
<form action="search.ph?saleType=forsale" method="get">
...
Use CSS to position the forms on top of one another, and to hide one of the forms.
Use the Checkbox Hack to detect a click event on the form "For Sale" and "To Rent" links (you'll need to make them a <label>), and to hide/show the respective form.

How to integrate support for Autopaging Plugins

How do I make my website compatible with all those autopaging plugins out there that load the next page beneath the current, thereby saving a page reload?
It's depend on how you design your page.
Let's take a look at twitter page. The items there displayed in a li, top to down. Implementing a autopaging to page like this will be quite simple.
When page load, display a predefined number of item in list (10 or 20, or else)
When user reach the end of list, (maybe detect the position of certain element, or else), load the next page via AJAX. The back end page should detect AJAX request and then only return a portion of page that contain the list item only
In AJAX response handler block, do a DOM manipulation to add the newly received list item into the end of existing list.
In my project, I create function like this by myself. I'm using jQuery btw.
You can't make it compatible with all plugins. You can make it compatible with a specific plugin. How to do that is probably described in the documentation for said plugin.

Categories