I have this code:
echo "<form action='activity1.php' method='post'>";
echo "<input type='checkbox' name='checkbox_test[]' value='1'>aaa";
echo "<input type='checkbox' name='checkbox_test[]' value='2'>bbb";
echo "<input type='checkbox' name='checkbox_test[]' value='3'>ccc";
echo "<br><br>";
echo "<input type='submit' name='activity1' value='Activity1'>";
echo '</form>';
This will results 3 checkboxes and 1 summit button. The selection will be handled by acvitity1.php.
I would like to add another submit button for each checkbox line something like this:
echo "<form action='activity1.php' method='post'>";
echo "<input type='checkbox' name='checkbox_test[]' value='1'>aaa "."<input type='submit' name='activity2' value='Activity2'><br>";
echo "<input type='checkbox' name='checkbox_test[]' value='2'>bbb "."<input type='submit' name='activity2' value='Activity2'><br>";
echo "<input type='checkbox' name='checkbox_test[]' value='3'>ccc "."<input type='submit' name='activity2' value='Activity2'><br>";
echo "<br><br>";
echo "<input type='submit' name='activity1' value='Activity1'>";
echo '</form>';
If the user press the activity2 buttons, how can i pass the value another php file (for ex activity2.php) ?
So how can I put a form into another form ?
Think about a table / form where you can select any line for delete (activity1), and buttons for the end of each line to edit the table row where the button has pressed (activity2).
Thank you!
Post edit:
As I am unable to comment, I was unable to ask for clarification. You said:
Ammadu: after pressing activity2 buttons the page get (needs to be) redirected to another page (activiy2.php). At activity2.php i want to catch checkbox_test[] value with $_POST.
In that case, AFAIK, you can not explicitly redirect the request to activity2.php as form nesting is not allowed and "Activity2" submit buttons will always POST to activity1.php. The simplest thing you can do is check which submit button POST-ed the request and react acorrdingly (code for checking the POST variable is shown below in the pre-edit section).
Pre-edit:
Your questions seems a little bit unclear to me, but I'll try to answer it based on what you wrote at the end of the question:
Think about a table / form where you can select any line for delete (activity1), and buttons for the end of each line to edit the table row where the button has pressed (activity2).
Also, I am no professional, merely a student, and an amateur programmer.
I had a similar problem while attending web programming course at my college. Specifically, there were table rows with checkboxes at the end of the each row for deleting the rows and the "Edit" button next to each of them. There was a "Delete" button below the table that was used to call the script that would remove the rows that were marked for deletion. We were using some dirty, dirty hacks to make that work.
You asked about form nesting, quick Googling revealed that form nesting is not a valid code.
Unless you really need to do your tasks in a separate PHP script I would suggest checking the POST variable to see which button was used to POST the form data to the server:
<?php
if (isset($_POST['activity1'])) {
//code for activity1 button
}
elseif (isset($_POST['activity2'])) {
//code for activity2 buttons
}
?>
This approach is also causing another problem - there is no easy way to identify which row that button belongs to. What you can do is dynamically name the buttons in the process of creation for each row (activity2_1, activity2_2...) and then create a loop in the PHP script that would check which button was clicked, which is a very ineffective way of doing things. That was the dirty hack I used back when studying WP course.
What I would go for are simple anchors. You can create them inside a PHP loop like this:
<?php
//...rest of the code in the loop...
echo 'Edit';
//...rest of the code in the loop...
?>
The script activity2.php should then perform a simple GET check and do the rest of the job:
<?php
if (isset($_GET['rowID'])) {
//activity code
}
?>
If you really need to use the buttons:
You can style the anchors using CSS to look like and behave like buttons and then use the code shown above;
...or, if you are allowed, you can use simple JavaScript code that you can generate inside the PHP loop in the same manner, like this (out of my head) and then echo it:
<?php
//...rest of the code in the loop...
echo '<input type="button" onclick="location.href="/activity2.php?rowID=' . $your_row_id . '";" value="Edit" >';
//...rest of the code in the loop...
?>
Hope this helps.
Related
i am new to PHP so please bear with me.
in my php project using mysqli queries i get results from mysql database and display them "as an html table with pagination" (e.g. 10 results per page).
my main piece of code to achieve that is the code below and works fine:
<table>
<tr>
<td> word-combinations </td>
</tr>
<?php
while ( $row=mysqli_fetch_assoc($result) ) { ?>
<td><?php echo $row['words'],$_POST['fname'];?></td>
</tr>
<?php } ?> <!-- end of while loop -->
<?php ?> <!-- the end of php tag -->
</table>
so the url of first page of the table is :
http://example.com/mytable.php?page=1
the url of second page of the table is :
http://example.com/mytable.php?page=2
and so on.
i also have created an start.php page which contains a "simple HTML form" with a "single input field" with POST action:
<?php
session_start();
echo "<form method='POST' action='http://example.com/mytable.php?page=1'>";
echo "<label for='fname'>First name:</label>";
echo "<input type='text' name='fname'> <br>";
echo "<input type='submit' value='Submit'>";
echo "</form>";
?>
My goal is to be able to get (access) the string user entered in the input field of start.php page on all pages of table and append that string to the end of all results of the table pages when is displays to the user.
so to achieve this I've used php session concept.
when user fills out the input filed and hit submit button, user is redirected to http://example.com/mytable.php?page=1 url and the $_POST['fname'] value is present and is successfully added at the end of the results of the first page of the table.
when user fills out the input filed and hit submit button, user is redirected to http://example.com/mytable.php?page=1 url and the $_POST['fname'] value is present and is successfully added at the end of the results of the first page of the table.
but when user clicks on the same page1 pagination link (actually tag) under the table, the $_POST['fname'] is not present anymore !
also when user clicks on next pages links (page2, page3, etc) $_POST['fname'] is not present anymore !
My piece of code to create "pagination links" under table are:
<?php
for ($i=1;$i<=$total_pages;$i++){
echo "<a href='mytable.php?page=".$i."'> $i </a>";
}
?>
I don't know where I am wrong.
whether I need to place some code in somewhere to make session_start(); be executed on all pages of the table?
or for a reason which i don't know, the $_POST['fname'] can not be retrieved on all pages of the table.
any help would be appreciated.
The problem is that you are not setting the $_SESSION data after starting the session_start.
Your can try this..........But the Processor will be on the same page:.
1)create File to fetch the data....."index.html".....
<form action="index2" method="post">
2)Process the form When the user hit submit....."index2".....
<?php
session_start();
if(!empty($_POST['name1'])){
$_SESSION['name1']=$_POST['name1'];
}else{
echo "There is no Data in the Variable";
}
3)Now the SESSION variable will be absent in all the script you just have to start the session in each page, but i recommend using a one script processor page
Thanks to all guy who guided me.
so according to your guidance i change the form action from POST to default GET.
then according to CBroe guidance i added $_GET['fname'] into pagination link code.
in this way seems there is no need to deploy php sessions at all.
<?php
$userinput = $_GET['fname'] ;
for ($i=1;$i<=$total_pages;$i++){
echo "<a href='mytable.php?page=".$i."&fname=$userinput'> $i</a>";
}
?>
now everything works fine as expected but i don't know whether my codes and settings are perfect!
thanks to all
Hello i have a strange problem i have the following form
echo "<td class='contact-delete'>
<form method='post' action='update_record.php'>";
echo "<button type='submit' value='" . $row['ID_PER'] . "' name='recordID' id = 'recordID'>edit</button>";
echo "</form>";
in the update_record.php I have the following code
$id2update = $_POST['recordID'];
echo $id2update ;
session_start();
if(!$_SESSION['logged']){
header("refresh:3; url=start_page.php" );}
// above this part of code there is an other form with a submit button
else if(isset($_POST['submit'])){
echo $id2update ;
}
The problem is that the first echo outputs the variable as it supposed but the second echo does not output anything it is null. Can anyone give me an explanation?
edit:
The 2nd echo is being called but the value is null!
You say "above this part of code there is an other form with a submit button". I assume it is that you are trying to echo with the second submit? In that case the data is never submitted. When you submit an HTML form only that form is submitted. Any inputs/buttons/etc in other forms on the page are not sent to the server and so not available in the PHP code. This is by design.
Thus your code as it stands should never reach the second echo.
(Technically only non-disabled form elements with non-null name in the current form are submitted)
Edit:
I think I now understand the comment in your code. You have one form in another file which submits to update_record.php In that request you render a new form (as part of update_record.php) with a submit button. That new form submits to itself with the submit button. If this is correct then the point is that the submit of the form from update_record.php (the one with the submit button) is a new request. You set the value of $id2update in one request but then do a new request and in that it is not set. You should include that value as a hidden input in the form rendered in update_record.php:
<input type="hidden" name="recordID" value="<?php echo $id2update;?>" />
then, when the second request is made (when the update_record.php form is submitted) the vlaue will be fetched again.
Each request must be treated for itself - the server does not automatically know which requests go together (up to data saved in the session of course).
I am a bit at lost as my PHP knowledge is very basic to say the least, but I am learning on the fly.
In a Wordpress plugin, I have the following php function:
$pool->get_leagues( true );
which gives a an array of league values: the id number and the name of the league.
Then there is this function:
$pool = new Football_Pool_Pool;
$pool->update_league_for_user( get_current_user_id(), <<THIS IS WHERE SELECTED ID NUMBER GOES>> );
I need to create an HTML form that lists the available league names that the user on a page can select in either an dropdown form, with radio buttons or plain links, whatever is easiest for the example.
Then, when the user makes a choice and submits the values, the league value should be updated into the database as per the above function.
Here are my total newbe / dummy questions:
How does the PHP look that would create the desired action? where would I put this code? Do I create a whole new PHP page to handle this form, or do I need to enter it into one of the existing php pages somewhere?
Based on answer 1, how does the HTML look that would display the form and call the php once submitted?
If this is easier with javascript, please feel free to share that example.
Help is much much appreciated!
I think you have to create a whole new PHP file. Here the PHP code and the HTML are in a single PHP file.
<?php
if(!isset($_POST['submit'])){
//if the form has not been submitted yet, display the form
echo "<form name='myform' action='' method='POST'>";
//Get array of leagues
$leagues = $pool->get_leagues(true);
//Make a drop down
echo "<select name='league'>";
foreach($leagues as $league){
echo "<option>$league</option>";
}
echo "</select>";
echo "<input type='submit' name='submit' value='Submit'>";
echo "</form>";
}else{
//If the form has been submitted, run the PHP function to update database
$pool = new Football_Pool_Pool;
$pool->update_league_for_user(get_current_user_id(), $_POST['league']);
echo "Database updated!";
}
?>
I've been working on a way to build an archive for new threads. The over all goal was to make it so that if someone wanted to edit or delete a news thread they could, as well they could save a thread as a draft so that it ain't displayed to the public. I am using MySQL to store all the news threads, and I have it so that it prints out every news feed and the information for it. But when i click the edit button to edit that thread, it ALWAYS uses the id for the last MySQL entry called and NOT the ID I set it to use via a hidden form. Anyways here's the code and all parts to it. I'm so confused, and could really use some help. If you got questions just ask.
Main Script: http://pastebin.com/hn3cgVXu
Article_Post: http://pastebin.com/hhaLkuXe
Article_Archive: http://pastebin.com/X2fDg4dk
The original value for ID is called from the database, and set from article_archive
Display:
http://i25.photobucket.com/albums/c51/dog199200/Untitled-2.png
The Pencil is Edit, Trash Can is Delete. The image clearly shows that the loop is getting the ID, but that specific ID isn't being passed when the edit image is clicked.
In your Article_Archive when you loop through your database results you are naming your hidden input field the same thing for all the results.
<?php
while($row = mysql_fetch_array($news_list)) {
echo "<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"post\" id=\"result_".$row['id']."\" name=\"result_".$row['id']."\">";
// ...
echo "... <input type=\"hidden\" name=\"id\" value=\"".$row['id']."\">";
// ...
echo "</form>";
} ?>
You're calling it id, so when you place multiple hidden input fields on the same form it will just grab the last one. Where is the javascript for when you click edit? You won't be able to do a standard form submit with that code since you're overwriting all the input fields with the same name attribute.
I have a database which holds the residents of each house in a certain street. I have a 'house view' php web page which can display an individual house and residents when given the house number using 'post'. I also have a 'street view' web page which gives a list of houses. What I want to know is if you can have links on the street view which will link to the house view and post the house number at the same time without setting up a form for each?
Regards
If you want to pass the data using POST instead of GET, you can do it using a combination of PHP and JavaScript, like this:
function formSubmit(house_number)
{
document.forms[0].house_number.value = house_number;
document.forms[0].submit();
}
Then in PHP you loop through the house-numbers, and create links to the JavaScript function, like this:
<form action="house.php" method="POST">
<input type="hidden" name="house_number" value="-1">
<?php
foreach ($houses as $id => name)
{
echo "$name\n";
}
?>
</form>
That way you just have one form whose hidden variable(s) get modified according to which link you click on. Then JavaScript submits the form.
I assume that each house is stored in its own table and has an 'id' field, e.g house id. So when you loop through the houses and display them, you could do something like this:
<a href="house.php?id=<?php echo $house_id;?>">
<?php echo $house_name;?>
</a>
Then in house.php, you would get the house id using $_GET['id'], validate it using is_numeric() and then display its info.
You cannot make POST HTTP Requests by some_script
Just open your house.php, find in it where you have $house = $_POST['houseVar'] and change it to:
isset($_POST['houseVar']) ? $house = $_POST['houseVar'] : $house = $_GET['houseVar']
And in the streeview.php make links like that:
Or something else. I just don't know your files and what inside it.
This is an old thread but just in case anyone does come across i think the most direct solution is to use CSS to make a traditional form look like an anchor-link.
#ben is correct you can use php and javascript to send a post with a link, but lets ask what the js does -- essentially it creates a form with style='display:none' sets an input/text line with value='something' and then submits it.
however you can skip all this by making a form. setting style='display:none' on the input/text lines (not the form itself as above) and then using CSS to make the button look like a normal link.
here is an example is i use:
in PHP Class,
public function styleButton($style,$text){
$html_str = "<form id='view_form' action='".$_SERVER['REQUEST_URI']."' method='post' >";
$html_str .= "<input style='display:none;' name='list_style' type='text' value='".$style."' >";
$html_str .= "<input id='view_button' type='submit' value='".$text."' >";
$html_str .= "</form>";
return $html_str;
}
Then in the CSS id="view_form" set "display:inline;"
and in the CSS id="view_button" set to something like: "background:none;border:none;color:#fff;cursor:pointer"
I would just use a value in the querystring to pass the required information to the next page.
We should make everything easier for everyone because you can simply combine JS to PHP
Combining PHP and JS is pretty easy.
$house_number = HOUSE_NUMBER;
echo "<script type='text/javascript'>document.forms[0].house_number.value = $house_number; document.forms[0].submit();</script>";
Or a somewhat safer way
$house_number = HOUSE_NUMBER;
echo "<script type='text/javascript'>document.forms[0].house_number.value = " . $house_number . "; document.forms[0].submit();</script>";
This post was helpful for my project hence I thought of sharing my experience as well.
The essential thing to note is that the POST request is possible only with a form.
I had a similar requirement as I was trying to render a page with ejs. I needed to render a navigation with a list of items that would essentially be hyperlinks and when user selects any one of them, the server responds with appropriate information.
so I basically created each of the navigation items as a form using a loop as follows:
<ul>
begin loop...
<li>
<form action="/" method="post">
<input type="hidden" name="country" value="India"/>
<button type="submit" name="button">India</button>
</form>
</li>
end loop.
</ul>
what it did is to create a form with hidden input with a value assigned same as the text on the button.
So the end user will see only text from the button and when clicked, will send a post request to the server.
Note that the value parameter of the input box and the Button text are exactly same and were values passed using ejs that I have not shown in this example above to keep the code simple.
here is a screen shot of the navigation...
enter image description here