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.
Related
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>';
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.
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 } ?>
I am echo values by the codes below. but i want to create for each values form for saving to database.
How can i insert form inside echo ?
I am beginer in php and i want form like this and it must replace with all echo values.
----FORM----
Thumbnail url :
url :
description:
submit Button
----FORM----
this is the code
foreach($posts as $post){
if ($post){
echo $post->thumbnail . "<br />";
echo $post->url . "<br />";
echo $post->description . "<br />";
}
}
I solved how to add.
foreach($posts as $post){
if ($post){
echo '<input id="title" class="form-control" type="text" name="title" value="'.htmlspecialchars($post->thumbnail).'">';
echo '<input id="teaser" class="form-control" type="text" name="teaser" value="'.htmlspecialchars($post->Url).'">';
echo '<input id="text" class="form-control" type="text" name="text" value="'.htmlspecialchars($post->description).'">';
echo '<button class="btn btn-primary" type="submit">add</button>';
}
}
this works fine... My second question is how i can add the code below codes to inside echo in a same way ?
i tried to add same way but (if isset line ) gives error
<input type="hidden" name="mode" value="news" />
<input type="hidden" name="edit_news_submit" value="true" />
<?php if(isset($edit_news['id'])): ?>
<input type="hidden" name="id" value="<?php echo $edit_news['id']; ?>" />
<?php endif; ?>
I'm trying to echo $url in the hidden field but I can't get it to work. I'm already inside an echo and I've tried escaping but that just gives me a parse error.
Sorry if this is a bit vague, Please can somebody help me to echo $url in that value???
echo'<form name="login" method="post" action="checklogin.php">
<input type="hidden" name="current_url" value="echo $url;" readonly/>
</form>';
Thanks
1) Don't echo so much using PHP, you are literally torturing it
2) You are using ' so whatever you write between will be treated as a literal string
3) If it's so, you need to concatenate the string using a period .
4) Use double quotes " when you are using variables inside a string
?>
<form name="login" method="post" action="checklogin.php">
<input type="hidden" name="current_url" value="<?php echo $url; ?>" readonly/>
</form>
<?php
echo'
<form name="login" method="post" action="checklogin.php">
<input type="hidden" name="current_url" value="'.$url.'" readonly/>
</form>
';
This is the correct usage.
Try like this
<input type="hidden" name="current_url" value="'.$url.'" readonly/>
or simple do like this
<form name="login" method="post" action="checklogin.php">
<input type="hidden" name="current_url" value="<?php echo $url;?>" readonly/>
</form>
Don't echo inside echo
echo'
<form name="login" method="post" action="checklogin.php">
<input type="hidden" name="current_url" value="', $url, '" readonly/>
</form>
';
Just off topic: Why commas instead of . for concatenation.
You can't echo an echo. You're already echoing out the string so all you need to do is add your variable to it:
change
value="echo $url;"
to
value="' . $url . '"
You could simply use double quotes and let the variable expand in the string, just so:
$url = 'http://www.example.com';
echo "Hello World! This is my url $url";