So i have this loop that shows data from my database, each of the rows will create a button that will be used later for activating/deactivating user. Now my problem is after clicking the button the output from the action.php is something like this
action.php?course_action=1&action=activate&course_action=2&action=activate&course_action=3&action=activate&course_action=4&action=activate&course_action=5&action=activate&course_action=6&action=activate&course_action=7&action=activate&course_action=8&action=activate
it looks like after pressing the button it stores all the value from input, the expected output is something like this only
action.php?course_action=1&action=activate
I completely forgot how to use php or just an excuse. Anyways hope you guys share some knowledge
<form action="db_connection/action.php" method="get">
<?php
$learningcenters = json_decode(learningCenters());
foreach ($learningcenters as $obj) {
echo '<tr>
<td>'.$obj->lc_id.'</td>
<td class="txt-oflo">'.$obj->lc_name.'</td>
<td>'.$obj->lc_emailadd.'</td>
<td>'.$obj->lc_contactnum.'</td>
<td class="txt-oflo">'.$obj->lc_datereg.'</td>
<td><span class="text-success">'.$obj->lc_timereg.'</span></td>
<td>
<input type="hidden" name="course_action" value='.$obj->lc_id.'>
<input type="hidden" name="action" value="activate"/>
<input type="submit" class="act-user" value="Activate User"></input>
</td>
</tr>';
}
?>
</form>
A submit button with a name attribute is included into the form data when it is clicked. To manage a caption differing from the value, use the button element. There should be no reason to prevent additional form data from being sent.
To be more conform to PHP as an embedded language, you should frequently close <?php tags rather than generating HTML outputs from strings. This also helps you to develop an MVC or similar pattern.
To get additional data per button click being sent, consider a composed string format, e.g. : as a delimiter.
<?php
var_dump($_GET);
?>
<form>
<?php
foreach ([3,5,6] as $id)
{
?>
<button name="action" type="submit" value="activate:<?php echo $id;?>">Activate</button>
<?php
}
?>
</form>
You can even use array parameters. This approach also enables you to perform an action on multiple items selected by checkboxes at once.
<?php
var_dump($_GET);
if(isset($_GET['action']) && is_array($_GET['action']))
foreach ($_GET['action'] as $id => $action)
echo "<div>$id: $action</div>"
?>
<form>
<?php
foreach ([3,5,6] as $id)
{
?>
<button name="action[<?php echo $id;?>]" type="submit" value="activate">Activate</button>
<?php
}
?>
</form>
Related
I have a form like so:
<?php if (isset($_POST['artist'])) {
// do something
} ?>
<form name="admin_on_artist_<?php echo $artist->ID; ?>" action="" method="POST">
<p class="artist-negative">
<label for="artist"><input type="checkbox" name="artist_<?php echo $artist->ID; ?>" id="artist_<?php echo $artist->ID; ?>"> Check this?</label>
</p>
<button type="submit">Update</button>
</form>
On the page in question, this form is shown many times in a foreach loop. However, when I submit any given form, it updates all of the forms, which is not what I want.
How can I append the $artist->ID to $_POST['artist'] so that I get something like:
$_POST['artist_1'] to match the checkbox attributes?
You could pair your foreach that generates the frontend form markup with a foreach that processes the form submission. Something like:
<?php
$regex = '/^artist_([0-9]+)$/'
foreach (array_keys($_POST) as $key) {
if (preg_match($regex,$key,$matches)) {
$artistId = (int)$matches[1];
// do something with $_POST[$key] according to $artistId
}
}
This works for a single field submission or a multiple field submission.
Alternatively, you could do something on the frontend in JS (as #smith suggests in the comments) to ensure the form submission always has the same, well-known keys, populating a hidden form with the current submission. With this approach you would have to add another field to the form that contains the ID.
The solution for this was much simpler than I was able to grasp at first, but basically I just had to do this, the key difference between this and my original question being the first two lines:
<?php $artist_form_id = 'artist_'.$artist->ID;
if (isset($_POST[$artist_form_id])) {
// do something
} ?>
<form name="admin_on_artist_<?php echo $artist->ID; ?>" action="" method="POST">
<p class="artist-negative">
<label for="artist"><input type="checkbox" name="artist_<?php echo $artist->ID; ?>" id="artist_<?php echo $artist->ID; ?>"> Check this?</label>
</p>
<button type="submit">Update</button>
</form>
I'm creating a page using HTML and PHP that accesses a Marina database(containing boats/owners ETC...), but I dont know how to capture and use the choice selected from the drop down <select> form and then display all the boats under that owners name(on the same page).
Here is my relevant code...
echo '<form align="left"; top="200"; action="page2.php"; method="post">
<p>Select an owner:</p>
<select top="200"; name="form1"; id="form1">';
foreach($values as $v){
echo '<option value="'.$v['LastName'].'">'.$v['LastName'].'</option>';
}
echo '</select>
</form>
<form align="left"; top="250">
<input type="submit" value="Submit">
</form>';
$form1 = $_REQUEST['form1'];
if($form1){//if there is data submitted to the page
echo '<p>$form1</p>';
}
When I use this code I get an error stating "form1 is an undefined index"
My question is how would I capture the name chosen as a variable from the drop down list when the submit button is clicked? (I apologize as I am very new to HTML and PHP and am only posting here because I cannot find a simple or clear answer anywhere else)
The problem is your <input type="submit"> is not part of the same form. Because you create an independent form for the submit button, it only submits that independent form. This independent form does not have the name attribute set, so your $_REQUEST['form1'] will indeed be undefined.
To correct this, simply have the one form, which contains both the selection and submission:
echo '<form align="left" top="200" action="page2.php" method="post">
<p>Select an owner:</p>
<select top="200" name="form1" id="form1">';
foreach($values as $v){
echo '<option value="'.$v['LastName'].'">'.$v['LastName'].'</option>';
}
echo '</select>
<input type="submit" value="Submit">
</form>';
$form1 = $_POST['form1'];
if($form1){ // if there is data submitted to the page
echo '<p>$form1</p>';
}
Note that you also shouldn't have the semicolons separating the HTML attributes; I've removed these. You'll also really want to use $_POST instead of $_REQUEST, as you don't want $_GET access. I've changed this as well.
You also might want to consider extracting the logic from your markup, and separating the two out.
Suppose I have a form. After I submit my form, the data is submitted to dataprocess.php file.
The dataprocess.php file processes the variable sent via form and echoes desirable output.
It seems impossible to echo to a specified div in specified page only using PHP (without using AJAX/JavaScript as well). I do not want to use these because some browsers might have these disabled.
My concern is that I want to maintain the same formatting of the page that contained the form element. I want the form element to be there as well. I want the query result to be displayed below the form.
I could echo exact html code with some modification but that's memory expensive and I want it systematic.
Is it possible to process the form within the same page? Instead of asking another .php file to process it? How does one implement it?
The above is just for knowledge. It will be long and messy to include the PHP script within the same HTML file. Also, that method might not be efficient if I have same process.php file being used by several forms.
I am actually looking for efficient methods. How do web developers display query result in same page? Do the echo all the html formatting? also, does disabling JavaScript disable jQuery/AJAX?
Yes it is possible to process the form on the same page.
<?php
if (isset($POST))
{
//write your insert query
}
?>
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<!-- Your form elements and submit button -->
</form>
<table>
<?php
//your select query in a while loop
?>
</table>
</body>
</html>
But if you choose this technique instead of ajax, you have to refresh all the page for each insert action.
An example
<div id="dialog-form">
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<table>
<tr>
<td>Job</td>
<td>
<input type="text" name="job" />
</td>
</tr
</table>
<input type="submit" value="Insert" />
</fieldset>
<input type="hidden" name="doProcess" value="Yes" />
</form>
</div>
<?php
$myQuery= $db->prepare("INSERT INTO Jobs (job) VALUES (:p1)");
if (isset($_POST['doProcess']) && $_POST['doProcess'] == 'Yes')
{
$myQuery->bindValue(":p1", $_POST['job'], PDO::PARAM_STR);
$myQuery->execute();
}
?>
if you really dont want to use ajax (which i think you should). You can do something like this.
<form action="" method="POST">
<input type="text" value="something" name="something_name"/>
<?php
if(isset($_POST['something_name'])){
echo '<div id="display_something_name_if_exists">';
echo $_POST['something_name'];
echo '</div>';
}
?>
</form>
Basically what it does is submits to itself and then if there is a submission (tested with isset), it will echo a div with the correct information.
i have a form with action='#'
that outputting some inputs
and a statement when the submit button clicks
if($_POST['edit'] == 'Edit')
{
manipulation here
with printout
}
what happen here is the output of the MAIN FORM
and the output of the IF STATEMENT print out when SUBMIT button is click
what i want is, when the SUBMIT button is click, ONLY the PRINTOUT on IF STATEMENT will shows.
Have you tried extending the if statement with an else - one or the other type of situation?
//when form is submitted
if($_POST['edit'] == 'Edit')
{
manipulation here
with printout
}
//when form not submitted
else
{
//display form
}
If I understand correctly, you do not want to display the form after it has been submitted. If that is the case, you can check to see if one of the values submitted by the form is set or not and display the original form based on that.
Here is an example:
<?php if( !isset($_POST['edit']) ): ?>
<form action="#">
<input type="text" name="edit" />
...
<input type="submit" value="Submit" />
</form>
<?php else: ?>
Display other information.
<?php endif; ?>
<form id="enter" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return validateForm(this);" >
<p>
<input id="submitBtn" name="submitDetails" type="submit" value="Submit Details" onClick="myClickHandler(); return false;" />
</p>
</form>
<script type="text/javascript">
function myClickHandler(){
if(validation()){
showConfirm();
}
}
</script>
<?php
session_start();
$outputDetails = "";
$outputDetails .= "<table id='sessionDetails' border='1'>
<tr>
<th>Number of Sessions:</th>
<th>{$_POST['sessionNum']}</th>
</tr>";
$outputDetails .= "</table>";
echo $outputDetails;
?>
Above is the code for my form. What I am trying to do is that if the user submits the form, then it will go back to its own page. But if the "SessionNum" equals '1', then instead of posting the form to itself, it should post the form or in other words navigate to the "session_marks.php' page but it is not idng this, if sessionNum equals 1 then it still submits form or navigate back to its own page, what am I doing wrong?
Also lets say it displays a number for the sessionNum and then I submit the form and it submits the form back to itself, the number disappears, how do I keep the number displayed when submitting the form to itself?
Thanks
Where is the conditional logic to change the target of the form post? All I see in the form tag is this:
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"
This will always set the form's action to be the current PHP file, not any other PHP file. If you want to conditionally post to a different file, you'll need to add conditional logic in there. Something like this (though there may be better ways to do it, keep in mind that I'm very out of practice with PHP):
action="<?php $_POST['sessionNum'] == 1 ? echo 'session_marks.php' : echo htmlentities($_SERVER['PHP_SELF']); ?>"
As for the number disappearing, I don't see any form element with the name sessionNum. If there isn't such a form element, then there will be nothing in $_POST['sessionNum'], so the number will "disappear" because there's no value to be displayed.
If the above is your actual code, session_start(); has to be placed before ANY other output (html, php's echo, print etc...)