I am trying to get a input textbox value in a PHP variable and then update a table based on this variable value in my custom WordPress page. I have written following code.
It is working but not updating the text input value. What is wrong with my code?
<form method="post" enctype="multipart/form-data">
<td>
<table> <tr><td> </td><input type="text" name="offeramt" style="height:15px"/> <td>
<input type="hidden" name="pid" value= "<?php echo $retrieved_data->id; ?>">
<input type="hidden" name="textamount" value="<?php echo htmlspecialchars($_POST['offeramt']);?>">
<input type="submit" name="update" value="update" /></td> </table></td>
</form>
<?php
if (isset($_POST['update'])) {
$sellamt=$_POST['textamount'];
if (empty($sellamt)){
echo "pls input a value in text box";
}
else{
$myid = $_POST['pid'];
?> <?php
$wpdb->update('wp_share', array('status'=>'onsell','offervalue'=>$sellamt),array('id'=>$myid));
}}
}?>
I was updating the text input value to a hidden field ["textamount"]then trying to get that hidden field ["textamount"] value to $sellamt. However I have removed the hidden field and took the value directly from textbox like this
$sellamt=$_POST['offeramt'];
where ['offeramt'] is the textbox itself
and its working.Thanks
Related
i want to get the text box value which i selected from array of text boxes..
My code is like this:
Here am displaying the data on the browser from DB..
<?php
foreach($dataDecoded['categories'] as $key => $value)
{
?>
<tr>
<td><?php echo $value['CategoryName'];?></td>
<td>
<input type="text" name="categoryId[]" id="categoryId" value="<?php echo $value['CategoryId']?>">
<input type="text" name="categoryName[]" id="categoryName" value="<?php echo $value['CategoryName']?>">
<input type="submit" name="create" id="create" value="create">
</td>
</tr>
<?php
}
?>
The data will be displayed like:
categoryId1 categoryName1 <Create Button>
categoryId2 categoryName2 <Create Button>
categoryId3 categoryName3 <Create Button>
and so on like this..
Now, suppose When i click on Create Button of CategoryName2, i want to get the categoryId and categoryName of only CategoryName2..
Am using this code:
if(isset($_POST['create']) && $_POST['create']=="create")
{
$cat_id[]=$_POST['categoryId'];
$cat_name[]=$_POST['categoryName'];
}
But with this, am getting all the categoryId and categoryName into the array.. how to get only the selected textbox value..
I want to do it using only PHP and not with JavaScript / JQuery... Am a bit new to PHP... Can someone help me / give me a suggestion about how to do it...
Thanks in advance.
Wrap each one in its own form:
<td>
<form method="POST" action="somePage.php">
<input type="text" name="categoryId" value="<?php echo $value['CategoryId']?>">
<input type="text" name="categoryName" value="<?php echo $value['CategoryName']?>">
<input type="submit" name="create" value="create">
</form>
</td>
Since the content being posted in this case is just these two values, then that's the entire scope of a single form post. So each one is logically/semantically its own form. (There's no rule that says a page can have only one form. Unless you're ever using ASP.NET WebForms, which is a lie.)
Note also that I removed your id attributes. Multiple elements can't have the same id, that's invalid HTML and any behavior as a result becomes undefined.
I am using Mysql PDO query to fetch the required result and it is saved in and array $newfamily. Now with the help of this array I am implementing check-box with the given code-
<form method="get" action="specific_family.php">
<?php
foreach($newfamily as $name)
{
?>
<input type='checkbox'<?php echo $name;?>"><?php echo $name;?></input>
<?php
}
?>
</select> <input type = "submit" name="submit" >
</form>
Now in specific_family.php how can I retrieve al the selected check-box values in an array ?
Also when I use back button of browser I can see previously selected values as ticked. How can I remove this ?
Please guide.
The checkbox should have:
A label
The checkbox needs:
A name attribute
A value attribute
It must not have:
An end tag (unless you are using XHTML)
So:
<label>
<input type="checkbox"
name="new_families[]"
value="<?php echo htmlspecialchars($name); ?>">
<?php echo htmlspecialchars($name); ?>
</label>
The values of all the checked checkboxes will then appear in the array $_GET['new_families'] when the form is submitted.
If you add the name attribute to your input thus:
<form method="get" action="specific_family.php">
<?php
foreach($newfamily as $name)
{
?>
<label for="<?php echo $name?>"><?php echo $name?></label>
<input type='checkbox' name="<?php echo $name;?>" id="<?php echo $name;?>"></input>
<?php
}
?>
</select> <input type = "submit" name="submit" >
</form>
then your checkboxes will show up by name in your $_GET array.
Hope that helps :)
this my id num column with a value
<td>ID NUMBER:</td>
<td><input type="hidden" name="txtEmpID" value="<?php echo $this->getArr['id']?>">
<input type="text" <?php echo (isset($this->postArr['EditMode']) && $this->postArr['EditMode']=='1') ? '' : 'disabled'?> name="txtEmployeeId2" value="<?php echo (isset($this->postArr['txtEmployeeId']))?$this->postArr['txtEmployeeId']:$edit[0][5]?>" maxlength="50">
how can to pass value from column txtEmpID to another page(reset.php) and display the value in the reset.php
this is reset.php
<tr>
<td align="right">Employee ID </td>
<td align="left"><input name="staffid" type="text" class="textfield_login"
value="how to display the value from 1st page into here?"/></td>
</tr>
help me please
Wrap the elements in a form and post the form a page.Then you can access the post data via $_POST if your method is post and $_GET if your method in form is get.
<form method="post" action="reset.php">
<td>ID NUMBER:</td>
<td><input type="hidden" name="txtEmpID" value="<?php echo $this->getArr['id']?>">
<input type="text" <?php echo (isset($this->postArr['EditMode']) && $this->postArr['EditMode']=='1') ? '' : 'disabled'?> name="txtEmployeeId2" value="<?php echo (isset($this->postArr['txtEmployeeId']))?$this->postArr['txtEmployeeId']:$edit[0][5]?>" maxlength="50">
</td>
</form>
In reset.php
echo $_POST['txtEmpID'];//Will contain the value of txtEmpID
There are two ways I can think of to do this.
The first is to use a form.
<form action="reset.php" method="get">
<input type="text" id="txtEmployeeId2" <?php echo (isset($this->postArr['EditMode']) && $this->postArr['EditMode']=='1') ? '' : 'disabled'?> name="txtEmployeeId2" value="<?php echo (isset($this->postArr['txtEmployeeId']))?$this->postArr['txtEmployeeId']:$edit[0][5]?>" maxlength="50">
<input type="submit" value="Submit">
</form>
The second
is to use Javascript/jQuery to get the value and pass it through the URL as an equivalent GET request.
However, you will need some sort of trigger to call the javascript function, like a button, for example.
<button onclick="sendValue()">Submit</button>
<script type="text/javascript">
function sendValue()
{
parent.location = 'reset.php?txtEmployeeId2='+document.getElementById("txtEmployeeId2").value;
}
Note that this will require you to add the ID attribute to your textbox.
For both solutions, you simply have to change the line in your reset.php file:
This is assuming that both the name attribute of your input and the ID attribute are the same. Replace "txtEmployeeId2" with whatever name or ID you want to give to the text box inputs.
<input name="staffid" type="text" class="textfield_login" value="how to display the value from 1st page into here?"/>
To
<input name="staffid" type="text" class="textfield_login" value="<?php echo $_GET["txtEmployeeId2"];?>"/>
You can also use session. ($_SESSION superglobal array)
I have a form with multiple items and an id attributed to each of these items, on Submit I want to be able to grab the id of the item that was clicked - I have tried using js, something like:
<form method="post" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo '3'; ?>" type="submit" value="Add">
</form>
I want to be able to grab this value: $item_id = $_POST["item_id"]; on add_item_cart.php, but this doesn't seem to be working.
Is this a problem with my js syntax or is my logic not plausible to solve this problem? Is it submitting before changing the value?
EDIT:
Let's see if I can explain myself better, I want to assign that hidden value dynamically, imagine that my form has 3 submit buttons (one for each item displayed). Depending on the one that is clicked, I want to pass the item's id to my hidden field, so if I click button1 - $_POST["item_id"]=1, button2 - $_POST["item_id"]=2... etc
Here is my actual form (non simplified example)
<form method="post" action="add_item_cart.php">
<table style="width:600px">
<tr>
<?php foreach ($items as $item): ?>
<td>
<center>
<span style="font-size:20px"><?php echo $item["item_name"] ?></span><br/>
€<?php echo $item["price"] ?><br/>
Quantidade: <input type="text" value="1" style="width:30px"><br/>
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo $item["id"]; ?>" type="submit" value="Adicionar">
</center>
</td>
<?php endforeach; ?>
</tr>
</table>
</form>
When your form is posted and you want to collect the item_id value on add_item_cart.php first you need to actually assign a value to id as in (Assuming item_id is a php variable). The id is just used for setting the css editing not a value...
<input type="hidden" id="item_id" value="<?php echo $item_id; ?>" name="item_id">
you cannot have id='' for the value because 'value' is value.
Then you can get that value on your other page with:
<?php
if(isset($_POST['item_id'])){
$item_id = $_POST['item_id'];
}
?>
If you want to edit the variable after post depending on which button you hit you can try.
<input name="submit_item1" id="btn_sub" name="button1" type="submit" value="Add">
<input name="submit_item2" id="btn_sub" name="button1" type="submit" value="Add">
<input name="submit_item3" id="btn_sub" name="button1" type="submit" value="Add">
Then on the top of your page you can do.
<?php
if(isset($_POST['submit_item1'])){
$item_id = 1;
}
if(isset($_POST['submit_item2'])){
$item_id = 2;
}
if(isset($_POST['submit_item3'])){
$item_id = 3;
}
?>
If you are creating forms from an array of items you could do something like, (assuming you somehow have an id associated with those items; I would need to know more information about how you are making your item list. But it would generally do like this.
<?php
foreach($item_array as $item){
?>
<form method="post" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id" value="<?php echo $item['id']; ?>">
<input name="submit_item" id="btn_sub" type="submit" value="Add">
</form>
<?php
}
?>
Then on the top of your page you can just get $_POST['item_id'] and since that value is dynamically set you do not need any conditionals you can just get that value and run any query.
UPDATE
Use normal button instead of submit button and use javascript for submitting the form.
<form method="post" name="f1" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo '3'; ?>; document.f1.submit();" type="button" value="Add">
I need to get textbox value to another php page. By clicking 'Box' icon i want to submit perticular row only. I already got row ID to 'Box' icon. How can i do that with the while loop.
thanks in advance
Tharindu
You should arrange the HTML code for this in the following fashion:
<table>
<form method="post">
<tr>
<td>Z0678<input type="hidden" name="id" value="Z0678"></td><td><input type="text" value="0" name="qty"></td><td><input type="image" src="box.gif"></td>
</tr>
</form>
<form method="post">
<tr>
<td>Z0678<input type="hidden" name="id" value="Z0678"></td><td><input type="text" value="0" name="qty"></td><td><input type="image" src="box.gif"></td>
</tr>
</form>
<form method="post">
<tr>
<td>Z0678<input type="hidden" name="id" value="Z0678"></td><td><input type="text" value="0" name="qty"></td><td><input type="image" src="box.gif"></td>
</tr>
</form>
<form method="post">
<tr>
<td>Z0678<input type="hidden" name="id" value="Z0678"></td><td><input type="text" value="0" name="qty"></td><td><input type="image" src="box.gif"></td>
</tr>
</form>
</table>
Then once you hit the image, it will submit the form. Add this code to the top of your PHP script "<?php print_r($_POST); ?> and you will see that you can now process the posted contents based on the data that was posted without the need for any while loop.
let the you have posted be list.php
in the text box like
<input type=text name="rid" value= " <?php echo $rid ?>" onclick="location.href='view.php'"/>
get the row id in next page that is view.php
$id = $_GET['rid'];
pass it as hidden in view.php
<input type="hidden" name="id" value="<?php echo $id; ?> "/>
Make sure all your db connection goes perfect and echo all data whatever you want from row.
In the original php file generate a different html form for each box.
<form action="page2.php" method="post">
<input name="Z067DA" />
</form>
In the page2.php file use code similar to this. $value contains the user
submitted information.
foreach($_POST as $key=>$value)
{
// Use submitted value.
}
If you know the input tag names in advance you can just access them directly in your php code.
$value = $_POST['Z067DA'];