I have two submission buttons on my webpage which are assigned to do different things. My problem is that clicking 1 of the buttons is invoking the actions of both buttons. How can I make it so that clicking 1 button only invokes the associated form submission action?
(This is my first day using php).
First form:
<form id="frm1" onsubmit="document.getElementById('frm1').submit();">
<center><br>
<input style="width:150px" name="data" type="text" autocomplete="off" placeholder="Add Time (hh:mm:ss)">
</input>
<input name="id" type="hidden" value="<?php echo $ID; ?>">
<input name="addTime" type="hidden" value="true">
<button style="width:40px" type="submit" class='btn btn-success pull-right'>
<i class="icon-plus"></i>
</button
</form>
Second form:
<form id="frm2" onsubmit="document.getElementById('frm2').submit();">
<center>
Edit Group:
<?php
#
$styles = sqlToQuery("SELECT groupID, Name from sha_scheduler_dev.sha_scheduler_map_job_group");
echo '<select style="width:100" name="groupNum">';
for($i = 0; $i < count($styles); $i ++){
echo ' <option style="width:100" value="'.$styles[$i][0].'">'.$styles[$i][1].'</option>';
}
echo '</select><br>';
?>
<input name="editGroup" type="hidden" value="true"><br>
<button type="submit" class='btn btn-info pull-right'>
Submit Group
</button>
</form>
Checking if buttons are submitted:
if ($_GET['addTime']==='true'){
sqlToQuery("replace into sha_scheduler_dev.sha_scheduler_map_jobtimes (job_ID, time_to_run) values (".$ID." ,'".$_GET['data']."')");
echo '<script>window.location.assign("'.$redir.'?id='.$ID.'");</script>';
}
if ($_GET['editGroup']==='true'){
sqlToQuery("UPDATE sha_scheduler_dev.sha_scheduler_jobs a SET a.group = '".$_GET['groupNum']."' WHERE a.job_ID = ".$ID." ");
echo '<script>window.location.assign("'.$redir.'?id='.$ID.'");</script>';
}
In your first form, button tag is not closed properly, that's why first form is tag is not closed properly,use this,
<form id="frm1" onsubmit="document.getElementById('frm1').submit();">
<center><br>
<input style="width:150px" name="data" type="text" autocomplete="off" placeholder="Add Time (hh:mm:ss)">
<input name="id" type="hidden" value="<?php echo $ID; ?>">
<input name="addTime" type="hidden" value="true">
<button style="width:40px" type="submit" class='btn btn-success pull-right'>
<i class="icon-plus"></i>
</button>
</center>
</form>
Also change your php code like this
if ( isset($_GET['addTime']) && $_GET['addTime']==='true'){
sqlToQuery("replace into sha_scheduler_dev.sha_scheduler_map_jobtimes (job_ID, time_to_run) values (".$ID." ,'".$_GET['data']."')");
echo '<script>window.location.assign("'.$redir.'?id='.$ID.'");</script>';
}
if ( isset($_GET['editGroup']) && $_GET['editGroup']==='true'){
sqlToQuery("UPDATE sha_scheduler_dev.sha_scheduler_jobs a SET a.group = '".$_GET['groupNum']."' WHERE a.job_ID = ".$ID." ");
echo '<script>window.location.assign("'.$redir.'?id='.$ID.'");</script>';
}
Give you're submit buttons a value and a name catch the different values using $_GET(in your case) or $_POST
Update: (just to give it a try maybe)
First Form
<form id="frm1" action="" method="post">
<center><br>
<input style="width:150px" name="data" type="text" autocomplete="off" placeholder="Add Time (hh:mm:ss)" />
<input name="id" type="hidden" value="<?php echo $ID; ?>" />
<input style="width:50px" type="submit" class='btn btn-success pull-right' name="submit_form1" value="submit" />
<i class="icon-plus"></i>
</button>
</center>
</form>
Second Form
<form id="frm2" name="frm2" action="" method="post">
<center>
Edit Group:
<?php
$styles = sqlToQuery("SELECT groupID, Name from sha_scheduler_dev.sha_scheduler_map_job_group");
echo '<select style="width:100" name="groupNum">';
for ($i = 0; $i < count($styles); $i ++)
{
echo ' <option style="width:100" value="' . $styles[$i][0] . '">' . $styles[$i][1] . '</option>';
}
echo '</select><br>';
?>
<input type="submit" class='btn btn-info pull-right' name="submit_form2" value="Submit Group" />
</form>
PHP
<?php
if (filter_input(INPUT_POST, "submit_form1"))
{
// {Check for empty var on data}
sqlToQuery("replace into sha_scheduler_dev.sha_scheduler_map_jobtimes (job_ID, time_to_run) values (" . $ID . " ,'" . $_GET['data'] . "')");
echo '<script>window.location.assign("' . $redir . '?id=' . $ID . '");</script>';
}
if (filter_input(INPUT_POST, "submit_form2"))
{
sqlToQuery("UPDATE sha_scheduler_dev.sha_scheduler_jobs a SET a.group = '" . $_GET['groupNum'] . "' WHERE a.job_ID = " . $ID . " ");
echo '<script>window.location.assign("' . $redir . '?id=' . $ID . '");</script>';
}
?>
This is pretty tight and keeps your url clean.
Related
I have designed a form and a dropdown that fetches its option from MYSQL Database. But after getting the options the rest of the code like the button and other parts disappeared not showing. The rest of the code works fine every time I comment out the PHP Query.
<form action="" method="POST">
<label><b>Add Student to this User ID</b></label>
<select name="student_id" class="form-control">
<option value="0">--Select Student--</option>
<?php
$query = mysqli_query($con, "SELECT * FROM students ORDER BY first_name, last_name, Other_name ASC");
while ($row = mysqli_fetch_assoc($query)) { ?>
<option value="<?php echo $row['id'] ?>"><?php echo $row['first_name'] . ' ' . $row['last_name'] . ' ' . $row['Other_name'] ?></option>;
<?php } ?>
</select>
<br />
<input type="hidden" name="page_id" value="<?php echo $user_id; ?>">
<div class="row">
<div class="col-sm-3 col-sm-offset-4">
<p><input type="submit" class="btn btn-xs btn-success" name="submit" value="Add Student"></p>
</div>
</div>
</form>
There's a semicolon missing after $row['Other_name']
I have a product list that filled automatically from the DB, I want to get the data corresponding to the selected product and fill the form.
<form name='login' action='<?php echo ($_SERVER['PHP_SELF']); ?>' method='post' enctype="multipart/form-data">
<?php
$sql = "SELECT product_name FROM products ORDER BY product_name";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
echo "<select>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option>" . $row["product_name"]."</option>";
}
echo "</select>";
} else {
echo "0 results";
}
$mysqli->close();
?>
<input type="text" name="product_code" placeholder="Product Code" />
<input type="text" name="product_name" placeholder="Product Name" />
<input type="text" name="product_price" placeholder="Price" />
<input type="file" name="fileToUpload" accept="image/*">
<textarea rows="8" name="product_desc_de" placeholder="Deutsch description"></textarea>
<textarea rows="8" name="product_desc_en" placeholder="English description"></textarea>
<textarea rows="8" name="product_desc_es" placeholder="Spanich description"></textarea>
<button type="submit" class="btn btn-default">Submit</button>
</form>
I know it should be somehow with AJAX
You have not shown any AJAX code so I assume that was just a mistake.
You do not NEED Ajax to make a form work.
So first give your <select> tag a name
echo '<select name="prods">';
And then your <option> tags a value
echo '<option value="' . $row["product_name"] . '">' . $row["product_name"] . '</option>';
Now when you look at the $_POST array you will have a $_POST['prods']
Personally I would add the id field to the SELECT
$sql = "SELECT id, product_name FROM products ORDER BY product_name";
And use the id as the value like this
echo '<option value="' . $row['id'] . '">' . $row['product_name'] . '</option>';
Im having trouble getting my form code to work, i want it to when they submit it to change the current page url, ex: weaw.com?id=Summitedtext and i am echoing it out from php.
$cmd = 'htmlspecialchars($_SERVER["PHP_SELF"])'
echo '<form id="idform" action="' . $cmd . '" method="GET">';
echo '<label><center>To search an inventory please put the users Profile URL in the input box below.</center></label>';
echo '<div class="input-group">';
echo '<span class="input-group-addon" id="basic-addon1"><span class="glyphicon glyphicon-user" aria-hidden="true"></span></span>';
echo '<input type="text" class="form-control" placeholder="Profile URL" aria-describedby="basic-addon1">';
echo '<span class="input-group-btn">';
echo '<button class="btn btn-default" type="submit" name="submit" value="Submit">Go!</button>';
echo '</span>';
echo '</form>';
echo '</div>';
Totally disagree with echoing form but change your code to this and add your ingredients such class and so on:
<?PHP
if(isset($_GET['url'])
$cmd = htmlspecialchars($_GET['url']);
else
$cmd = htmlspecialchars($_SERVER["PHP_SELF"]);
?>
<form id="idform" action="<?PHP echo $cmd; ?>" method="GET">
<input type="text" name="url">
<input class="btn btn-default" type="submit" name="submit" value="Go!" />
</form>
I am creating a simple MVC site that uses CRUD. For the delete function I have a button, the code below is what I am trying to use to send the rowID to the controller to handle.
I am having trouble in that the code doesnt print out on the screen correctly, The php code is being printed as well as the button.
The rowID isnt being passed either and I am wondering if my approach with the php code is the correct way?
Code
<?php
$itemsDAO = new ItemsDAO();
$result=$itemsDAO->getItems();
foreach ( $result as $row ) {
$uid = $row['id'];
var_dump($uid);
?>
<form action="index.php" method="post">
<fieldset>
<input id='action' type='hidden' name='action' value='deleteItem' />
<p>
<div class="form-group">
<div class="controls">
<input type="hidden" id="fId" name="fId" value="<?php echo $uid; ?>">
<input type="submit" class="btn btn-success" value="Delete">
</div>
</div>
</p>
</fieldset>
</form>
<?php } ?>
Delete Function
public function getItems () {
$sqlQuery = "SELECT *";
$sqlQuery .= " FROM items";
$result = $this->getDbManager()->executeSelectQuery($sqlQuery );
return $result;
}
You could try a string replace in you forloop if you have one.
I think it would look something like this
foreach ( $this->model->itemList as $row )
$HTMLItemList .= "<li><strong>" . $row ["title"] . ": </strong>" . $row ["price"] . "<blockquote>" .
$row ["description"] . " " . str_replace("value2replace", $row['id'], $buttons) . "" . $update . "</blockquote></li>";
And then you would change your form to the following
<form action="index.php" method="post">
<fieldset>
<input id='action' type='hidden' name='action' value='deleteItem' />
<div class="form-group">
<div class="controls">
<input type="hidden" id="fId" name="fId"
value="value2replace"> <input type="submit"
class="btn btn-success" value="Delete" />
</div>
</div>
</fieldset>
Assuming you want a delete form for each and every result, you need to wrap the HTML inside the foreach statement:
<?php
$result=$itemsDAO->getItems();
foreach ($result as $row) {
$uid = $row['id'];
?>
<form action="index.php" method="post">
<fieldset>
<input id='action' type='hidden' name='action' value='deleteItem' />
<p>
<div class="form-group">
<div class="controls">
<input type="hidden" id="fID" name="uid" value="<?php echo $uid; ?>">
<input type="submit" class="btn btn-success" value="Delete">
</div>
</div>
</p>
</fieldset>
</form>
<?php } ?>
Is it possible to attach the data to the url like this?
<?php
$query = mysql_query(......) or die(mysql_error());
while($row = mysql_fetch_assoc($query)){
echo "<a href='#foo?id=" . $row['Id'] . "'>" . $row['Name'] . "</a>";
}
?>
<div id="foo">
<form action="process.php" method="post">
<input type="hidden" name="id" value="<?php echo $_GET['id'] ?>"/>
<input type="text" name="message" value=""/>
<input type="submit" name="sendMessage" value="Send Message"/>
</form>
</div>
I want to pass the $row['Id'] to the div so that it can be put in the value of the hidden input type. Thank you for your answers and suggestions!
Try changing your code like this.
<?php
$query = mysql_query(......) or die(mysql_error());
while($row = mysql_fetch_assoc($query)){
echo "<a href='#foo?id=" . $row['Id'] . "'>" . $row['Name'] . "</a>";
$id = $row['Id'] ;
}
?>
<div id="foo">
<form action="process.php" method="post">
<input type="hidden" name="id" value="<?php echo $id ; ?>"/>
<input type="text" name="message" value=""/>
<input type="submit" name="sendMessage" value="Send Message"/>
</form>
</div>
Cheers!
Prasad.
If you're trying to take the row number that is clicked on and then make that the value for the hidden input, You probably want to use some javascript/jQuery. Its not even that difficult. Take a look at an example I made here
<?php echo "<div id=".$_GET['id'].">"?>....html....</div>
When you loop this, I don't know how you're applying it but if it's with a lot of other stuff, you might want to consider building an array and then outputting to a template format.