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'];?>" />
Related
I have a html page where the user can input some text, it is then posted to a php file and then stored in a database.
<form action="postphp.php" method="post" enctype="multipart/form-data">
<center><input id="postTitleStyle" type="text" name="title" size="100" maxlength = "180" ></center>
<center><textarea id="postTextStyle" name="Text" rows="10"cols="96" maxlength = "40000"><?php echo $text;?></textarea></center>
<center><input id="postTagStyle" type="text" name="tags" size="100" maxlength = "900" ></center>
<center><input type="submit" class = "Post2" value="post"></center>
</form>
Above is my current code for posting the data in the text field to a php file. I want to be able to click a button that when clicked will not go to the php file it will be stored and then when the user clicks the submit button it is posted. For example the user clicks it, a one is stored and then sent later when the user clicks the submit button after they are finished filling in other details. Is this possible?
P.S. I want to avoid Javascipt as much as possible for the moment, so if there is a non-java way of doing it then it would be much appreciated.
Many thanks, Jack.
There are two easy solutions to this problem without using Javascript. I'm assuming by your wording that you can currently post a form, but you don't know how to do so without leaving the current page. That's what I'll be answering below, but please note that there is no way to post a form without reloading at all without Javascript.
Solution 1: Put the PHP code into the same page the form is on and change the form tag to: <form action="" method="post" enctype="multipart/form-data">
A blank action field will cause it to run the PHP on the current page. You will likely need to look into using isset($_POST['submit']) in PHP, which will check whether the submit button has been clicked on before running that particular PHP code. You can read more about that HERE.
Solution 2:
In the postphp.php file that's currently linked to in your action field of your form, you could use a PHP header that will redirect the user after the PHP code is ran.
E.g.
<?php
{All your form processing code goes here}
header('location: my_form_page.php');
?>
In this example, my_form_page.php is the page on which your form is on. You can read more about headers in the answer of THIS question.
Hopefully this helps a bit.
$title = $_POST['title'];
$text= $_POST['text'];
$tags = $_POST['tags'];
mysql_query("INSERT INTO `table_name` (`colname1`,`colname2`,`colname3`) VALUES ('$title,'$text','$tags')");
$id = mysql_insert_id();
if($id){
echo "inserted";
}else{
echo "Not inserted";
}
For this you need to use Ajax (JavaScript will be used) because you need a button which send data to server without form submission and page reload it can be easily achieved using Ajax.
I am new to OO php so this may seem basic..
Basically I have a list of courses a user can book. I have got it so the user can remove the course from their list, but I want a message to be displayed to them after they delete. I have done something similar to what I want here:
<form name="removecourse" action="<?php bloginfo('url');?>/user/<?php echo $current_user->first_name ; ?>" method="post">
<input type="hidden" value="<?php the_id();?>" name="courseid" />
<input id="removebutton" type="submit" name="removecourse" value="Remove">
</form>
The form sends the required data to the same page, and at the top of that page is a check to see if the forms post name is present in $_POST[] like so:
if(isset($_POST['removecourse']) && !empty($_POST['removecourse'])){
$courseManager->delete_post($_POST['courseid'], $_POST['cancel-reason']);
echo $courseManager->delete_response;
};
This is where the Class and object part comes in...
public $delete_response;
function delete_post($postid, $reason){
//stuff to actually delete the post
$this->delete_response = 'Thanks, your course has been removed.';
}
So here I am adding a value to the delete_response variable and calling it above at the top of the page. This works, but when I refresh the page the message is still there as I am resubmitting the POST. I am just wondering if what I am doing is along the right track, and how to implement a Post/Redirect/Get type functionallity to stop the messaage from appearing on page refresh?
You have to check, either your course has been already deleted, is it simple as that :).
Yours displaying it again because:
if(isset($_POST['removecourse']) && !empty($_POST['removecourse'])){
//is always true when posted again.
}
You have to check the existiance
I have setup a form that allows a user to make selections from drop-down lists, and clicking the submit button will redirect them to the appropriate page based off of their selections. Here is the PHP:
<?PHP
function redirect($where){
header("Location: $where");
}
if ($_REQUEST['os1'] == 'xp' && $_REQUEST['browser'] == 'ffx'){
redirect('http://mysite.com/index.php/prodemo/prodemo-categories/73-prodemo-xp- firefox');
}elseif($_REQUEST['os1'] == 'xp' && $_REQUEST['browser'] == 'ie8'){
redirect('http://mysite.com/index.php/prodemo/prodemo-categories/72-prodemo-xp-ie');
}elseif($_REQUEST['os1'] == 'win7' && $_REQUEST['browser'] == 'ie8'){
redirect('http://mysite.com/index.php/prodemo/prodemo-categories/74-prodemo-win7-ie');
}elseif($_REQUEST['os1'] == 'win7' && $_REQUEST['browser'] == 'ffx'){
redirect('http://mysite.com/index.php/prodemo/prodemo-categories/75-prodemo-win7- firefox');
}
?>
I am manually telling the query where to take the user.
My dilemma is that I want a simple line that says:
"Hi, _. How can I help you?"
The blank would be filled in by the name that is inputted on the form as the Caller's name.
Here is an example of the form:
<form action="" method="post" name="form1">
Caller's Name: <input type="text" name="callersname" /><br />
<select name="os1"> <option selected="selected" value="xp">XP</option>
<option value="win7">Win7</option> </select> <br />
<select name="browser"> <option selected="selected" value="ie8">IE8</option>
<option value="ffx">Firefox</option></select> <br />
<input type="submit" value="Next" />
I'm very new to PHP. If I create a PHP page to post the caller's name, then how do I avoid the page redirecting to that php page instead of the redirect I have manually setup with the drop-down menu options selected?
Thanks for any advixce
If you want to store the user's name on a page other than the first page or the same page that is processing the form values (IE the script that's getting the redirect) you want to store that information either in a database, or more simply in a session.
First, initialize the session on each page where you need to pull that information from:
session_start();
Then, create a session variable (unique to every user/browser) like so:
$_SESSION['user_name'] = $_REQUEST['callersname'];
Now, on the page you wish to retrieve their name (IE Hi ) just do so like this:
echo "Hi {$_SESSION['callersname']} welcome to mcdonalds how may I help you?";
In HTML a form will submit to the url that is given in the value of the action attribute of the form tag. In your example above you have:
action=""
This tells the browser to submit the form to the current page.
It sounds like you are trying to do two somewhat unrelated things in the same step. Are you trying to get the caller's name or have them choose what url to go to? If you want to do both, you may be better off by just accepting the os1 and browser values as POST variables, and dynamically changing the page based on the value of those variables rather than redirecting the user to entirely separate pages.
Try using $_POST instead of $_REQUEST... It may be the problem and too, add action="yourpage.php" to tell the code to send the form to the current page or another page...
What I have so far is a paginated page, showing all users in a database system. Each page shows 30 users and has a checkbox next to each user, what I need is a way for users to select and deselect these users and for these selections to propagate through, so if the user goes back to page 1 from page 2, all users from page 1 will still be checked.
I also need a way to record this information, so that once the user has looked at all the pages and clicks a submit form all checked users information can be processed. I am thinking of using javascript to record the information and php sessions to store it, but with the way I am trying now, when a user clicks a checkbox, it is not ticked.
Does anyone have a better way of doing this/see how I can fix this problem?
Thanks.
<script type="text/javascript">
function log_export($str) {
document.check.data.value = $str;
document.check.submit();
}
</script>
<?php
if(isset($_POST['data'])) {
echo $_POST['data'];
}
?>
<form name="check" method = "post" action = "">
<input type="hidden" name="data">
<input type="checkbox" name="A" onclick="log_export('1')" />
<input type="checkbox" name="B" onclick="log_export('2')" />
<input type="checkbox" name="C" onclick="log_export('3')" />
</form>
Few ways you could do this, but I'd avoid using javascript to do it. You could use an array in your $_SESSION to keep the list across pages.
<form name="check" method = "post" action = "">
<input type="checkbox" name="person[A]" />
</form>
(note: The form elements are named person[A], person[B]...etc, so they can be accessed as an array in php and make your life easier.)
Then in the php you can store this in the session...
$_SESSION['saved_list'] = $_REQUEST['person'];
This way the session variable saved_list will contain the array person with all the checked boxes in it. You'll need to be careful not to overwrite the array each time however, so adding...
$_REQUEST['person'] = array_merge($_SESSION['saved_list'], $_REQUEST['person']);
...before this should keep them (if I'm remembering my merge functions correctly).
Alternatively, you could just use html to save the checkboxes already ticked. When page 2 receives the results from page 1, it could print them out as hidden elements at the end of the page 2 form. This way they can exist across pages, however this could become a bit unwieldy with 30 names a page.
I'd suggest storing it in a php session array, this shouldnt really involve using Javascript, it just over-complicates matters.
Why you don't change your form method to GET and make the pagination link so it contains every parameters passed to the form and the page number. I think it make everything more simple to handle that case with parameters passed on the URL against posted one.
you can create those links like this:
for ($i =0; $i < $max_page; $i++){
echo "{$_SERVER['REQUEST_URI']}?$_SERVER['QUERY_STRING']&p={$i}";
and you just have to change your backend to use $_GET instead of $_POST.
Hey I am very new to Web Programming. I have been learning PHP from the past few days and I am stuck at one thing.
I have a form tag in my code which has two submit buttons to manipulate on the data.
Since I can have only one action definition on my form tag, it can lead me to one page only. (Not very sure)
Now depending on the button clicked on the form, I want to load a different page.
One way is to check the button clicked in an if-else construct and then use echo '...' in the branches and show as if it is a different page. But it doesn't seem right for some reason. Can some one give me a better solution? Thanks.
One way is to use Javascript to switch the form's action depending on which control has been clicked. The following example uses the jQuery library:
<form id="theForm" action="foo.php">
...
<input id="first" type="submit"/>
<input id="second" type="submit"/>
</form>
$(document).ready(function() {
$("#theForm input").click(function(e) {
e.preventDefault();
if(e.target.id == 'first') {
$("#theForm").attr("action", "somePage.php");
} else {
$("#theForm").attr("action", "anotherPage.php");
}
alert($("#theForm").attr("action"));
$("#theForm").submit();
});
});
Demo here: http://jsfiddle.net/CMEqC/2/
But it doesn't seem right for some reason.
That's wrong assumption.
Any other solution would be much worst.
Checking on the server side is the only reliable solution.
However echo in branches isn't necessary. There are a lot other ways.
To use include statement is most obvious one.
just as a reference... int HTML5 buttons can redefine the form's action,method,type etc. http://w3schools.com/html5/tag_button.asp for me, that's a good way to control a form :)
to add another solution based on #karim79's, since it's tagged with PHP:
<form id="theForm" action="foo.php">
...
<input id="first" name="button" value="first" type="submit"/>
<input id="second" name="button" value="second" type="submit"/>
</form>
in your foo.php, do something like this:
<?php
$submit = isset($_GET['button']) ? trim($_GET['button']) : '';
if($submit == 'first')
{
header('Location: somePage.php');
}
else if($submit == 'second')
{
header('Location: anotherPage.php');
}
?>
Summary:
to be able to read on your button (2 submit buttons), you need to add name on each one. To make it simple, just use the same name on both. Then, add different value. Next, you need to know what button is being clicked by checking what value is sent on that particular button.