How can I display extra information when a selection is made? - php

For practice, on an html page, I have 2 buttons.
1 button was made using the tag, and it redirects to another page. (this works)
The second button, when pressed needs to display more features and options that populate the blank area of the page.

<?php
$action = (isset($_GET['sent'])) ? $_GET['sent'] : null;
If($action!=null){
?>
Other buttons go here, <b>Hello</b>
<?php
}
?>
<form action="<?php echo $_SERVER['PHP_SELF'].'?sent=yes'; ?>" method="get">
<input type="submit" value="Show more buttons" />
</form>
Give or take that should do what you want to do. Although I recommend learning as this method is a little annoying as the entire contents of the page have to be refreshed. When dealing with text, it's not so bad, but most websites have images.

Try using CSS with javascript. Have the additional content in a separate div that is position:absolute and visibility:hidden. Attach javascript to the 2nd button, so that click changes visibility to visible.

Related

How can a submit button made to be act as a link too?

I'm doing a Quiz project: The idea is to implement almost 25 questions in which each question occupies each HTML page with 4 radio buttons and a submit button and a reset button as well.On clicking the submit button it should take the user to the next page as well as submit the data to the server. How to achieve this dual behaviour?
I tried this:
<form action="cba.php" method="post">
<a href="abc.html">
<input type="submit" value="submit">
</a>
</form>
But this does only one purpose: Acting as a link without submitting the data.
If you just want to redirect the user after submitting the form, you can use :
header("Location: yourlink");
in the php script you called cba.php.
Otherwise, i'm not sure it is possible to redirect the user before sending him the php script page.
As mentioned, it would be a smoother experiance to handle this via ajax, but it can be acheived in just php by creating a redirect in the form processing code (as mentioned in comments and a current answer).
I believe your issue is with the fact that the same proccessing code (cba.php) will be called every step of the way, so you need a way for each quiz section to define the next section.
This can be done with a hidden field instead of the link code you tried:
<form action="cba.php" method="post">
<input type="hidden" name="next-page" value="abc.html">
<input type="submit" value="submit">
</form>
Then i cba.php, you redirect to the value contained in this hidden field:
//save the data from the form, then
header("Location: " . $_POST['next-page']);

Display button in HTML at top of page when button requires php output from lower in the code

Apologies for the horrific title - I had no idea how to summarise my needs (I have basic understanding of coding only through self-teaching).
I'm working on a HTML page that is built inside a php file, the page is part of a Wordpress site. The reason it's based on php is because it pulls information from a users profile and displays it.
I have a button that downloads a generated pdf of the content using Dompdf. The code for button is below:
<div class="download_pdf">
<form action="<?php echo get_bloginfo(template_directory) . '/pdf.php'; ?>" method="post">
<input type="hidden" name="html" value="<?php echo base64_encode($output); ?>" />
<input class="download-pdf" type="submit" value= "Download PDF" />
</form>
</div>
It takes the output from the code above it to generate the pdf, however currently this means I need to display the button at the end of the page, so that the output exists above the button code.
Ideally I want the button to be displayed at the top of the page where it is more easily viewable by the user however I'm not sure how as the page crashes due to no output existing at the point of creation (at least I believe that's the problem).

html checkbox onChange methode in php

Here is my problem, I know only html and php and I have no clue about how to use javascript... And all the solutions about my problems seems to be resolved in javascript and I wondered if there was a way to do it with php so that I could understand what I do.
I want to put a checkbox on the corner of my page (for instance "hide information") that would refresh the page automatically when checked and that would hide information on the page.
What I currently do is :
<?php
if(isset($_GET['condition']))
$_SESSION['condition'] = true;
else
$_SESSION['condition'] = false;
?>
...
...
<form>
<input type="checkbox" name="hide" value="1" onChange="this.form.submit()" <?php if($_SESSION['hide']) echo "checked";?> > hide information
</form>
I am facing two problems :
the first one is that I want the checkbox to stay checked/unchecked when the page is refreshed.. I solve that poorly with my php code, but there surely exists something better to do that.
When the box is checked, the page is refresh with only "hide=1" as an url argument, but I would love to keep all the other arguments that were there before the page was refreshed. Is there a way to refresh the page and keep all the arguments while knowing that the box is checked/unchecked ?
thanks for your help, and sorry for my poor knowledge.
Regarding the second point of your question you can move the POST (or GET) array to the SESSION one and back with the following code:
if(isset($_POST) & count($_POST)) { $_SESSION['post'] = $_POST; }
if(isset($_SESSION['post']) && count($_SESSION['post'])) { $_POST = $_SESSION['post']; }
I use this to do exactly the same. When I reload the page I keep the posted values.
Regarding the first point you are already on the right path.
I don't see anything wrong with how you've tried to solve problem 1.
Regarding the URL problem 2, either put session_start(); at the top of the page to get the session to work correctly.
Alternatively have hidden inputs in this pages' form and echo out the previous pages' POST values.
<form action="" method="post">
<input type="hidden" name="condition" value="<?php echo $_POST['condition']; ?>" />
<!-- have hidden inputs from previous page here, plus your checkbox to retain post values from the previous page -->
</form>
Although I'd recommend POST for this, you can do GET although it gets a bit messy like so:
<form action="thispage.php?condition=<?php echo $_GET['condition'];?>" />

Echoing a var from a sendmail.php form in a div inside the form page or in a popup window

The idea is to preview (this part works fine) the form somewhere so, if corrections are needed we can go back to the unrefreshed form to make corrections.
I have this form:
<form action="../../../../sendmail.php" id="Formulaire" method="post" name="Formulaire" onsubmit="return checkform()" target="_self">
I have a submit button to preview:
<input id="captcha" name="_Preview" type="submit" value="Preview" />
And here is my php code (from sendmail.php)
if(isset($_POST['_Preview'])) { echo $text; ?>
As stated above the preview (content of array $text) works well although it is presented on a blank page.
I like it to be presented either in a div on the form page or in a popup Window so when I close the popup I am back on the non-refreshed form.
I tryed different ways, my problem is everytime, after submitting the preview button I endup having the sendmail.php form in the background (blank of course or with the preview data). I don't know what approach to take. Thank you for your help.

In PHP, How would I use a variable that was created in another php page?

I have an included page to the main index page, in the included page it has buttons to click on..i want to give these buttons a variable and when someone clicks on it, I can keep track of what button they selected in another page, which will show information for the selected button/variable...
Any ideas?
Well there is several ways to do this, but the main question is are you using a form button or a image button or a link or what?
Form:
HTML:
<form name="phpForm" action="myFile.php" method="get">
<input type="submit" name="button" value="1">
<input type="submit" name="button" value="2">
</form>
PHP:
<?PHP
echo $_GET["button"]; //either 1 or 2
?>
Image:
HTML:
<img src="whoo.png" />
<img src="hilarious.png" />
And the PHP above will also work with this.
You should really start reading a basic PHP tutorial.
Depending on what form is the method, you'll receive the variables in either $_POST or $_GET:
Use this code to find out
print_r($_GET);
print_r($_POST);
Welcome to web programming, this should get you started: http://www.w3schools.com/php/php_intro.asp
There are several ways of doing this you can either use GET, POST, or store the variable in a SESSION
I am assuming when user clicks the button it is directed to another page, if its true, then you can do a GET with http://yoursite.com/pageTo.php?data='hello' as the href that links to the button. Where pageTo.php would $_GET['data']
Insert the jquery code to try out the click counts:
$(document).ready(function(){
var count =0;
$('button').click(function(){
count = count +1;
$('#showcount').html('click count' + count);
return false;
});
});
and somewhere in your body make a div with id = ' showcount' to show the click counts.
Or you can then save the click count into a text file to look at...or whatever
I hope this give you some ideas...

Categories