I'm trying to empty a textarea that is populated from a database with php.
Here's the code:
<select name="facility" id="facility">
<option></option>
<?php
global $__CMS_CONN__;
$sqlqry = "SELECT * FROM facility_db";
$stmt = $__CMS_CONN__->prepare($sqlqry);
$stmt->execute();
while($row = $stmt->fetchObject())
{
echo "<option value=\"$row->id\">$row->facility</option>";
}
?>
</select>
<button type="button" onclick="addFacility()">Add</button><button type="button" onclick="reset()">Reset</button>
<textarea readonly="readonly" id="facilities" name="facilities" rows="10" cols="50" value="<?php echo $facilitylist; ?>" ><?php echo $facilitylist; ?></textarea>
<input type="text" id="facilityids" name="facilityids" value="<?php echo $facilities; ?>"/>
The select box is populated from the database, and the Add button adds the selected facility to the textarea, and the id to the hidden field for database storage.
Up to this point everything works beautifully. The reset button will clear both the textarea and the hidden field and start over. The problem arises whan a user has saved their profile and had some facilities selected. The reset button only works on additions, but won't clear anything that was pulled from the database.
The javascript for the reset is just setting the value of those fields id to empty.
function reset()
{
document.getElementById('facilities').value = null;
document.getElementById('facilityids').value = null;
}
But it doesn't work on the hidden field either. I set it to text for testing and even when i manually delete what's in the textbox, if I click the reset button it comes back.
What is going on?
Give the function another name. There is a predefined method reset() inside forms, that function restores the initial values of form-fields.
A button is a form-field, by this the reset()-method of his form is invoked instead of your custom method.
function reset(){
document.getElementById('facilities').value= '';
document.getElementById('facilityids').value = '';
}
for textarea might have to do innerHTML = '';
as dr molle says the default reset method might have been used over your reset()
reset method
Related
I have a set of input boxes and you can add more and more sets of these forms if you click the add more button. In my form I can submit data and I have got it to show up when you reload the page, when the page shows it it also adds a value into a hidden form in case the user updates this information.
However, how can I see all the sets of data which do not have a hidden form value? And all the sets with do have a hidden value so I can do different things to them.
Here is my code:
HTML:
<form>
<div class = "fieldset-1">
<input type="text" id="Name1" name="name[]">
<input type="hidden" id="id1" name="id[]">
</div>
<div class = "fieldset-2">
<input type="text" id="Name2" name="name[]">
<input type="hidden" id="id2" name="id[]">
</div>
</form>
PHP:
$data = $_POST;
extract($data, EXTR_PREFIX_SAME,"br");
//Prints The Variables To Make Sure They Are Correct
print_r($id);
$name = preg_replace("/[^a-zA-Z- ]/", "", $name);
print_r($name);
You have all the post data in a $_POST. It doesn't depend on field's type. The only thing matters — field's name.
The reason why you can't see it with your code is that you do
$name = preg_replace("/[^a-zA-Z- ]/", "", $name);
For what, btw? preg_replace is for string, $name here is an array (cause your form field has a name name[]), so function fails, and you lost your data.
And don't ever use extract, it's considered harmful.
Once I add session in edit form, I could not get selected values in dropdown menu. I am using bootstrap framework. When I remove session from the same beginning it works.
edit.php file
<?php
session_start();
$car=$_POST['car']
?>
<form method="post" action="">
<div class=from-group>
<label for="exampleInputEmail1>Car</label>
<select class="form-group" name="car" id="car" value="<?php echo $car; selected?>">
<option value=<?php echo $car?> selected>
<?php cars()?>
</option>
</div>
</form>
I have created "cars" function in which I get values of all cars from database.
function cars(){
$link = new mysqli("", "", "", "");
$link = set_charset("utf8);
$sql = mysqli_query($link, "SELECT * FROM db_cars ORDER BY CarId")
echo '<option value=""> Choose car </option>'
while ($record = mysqli_fetch_array($sql)) {
echo '<option value = "'.$record['CarId']'"> . $record['CarName'].' </option>
}
}
Any help or advice is appreciated.
The select element has no name attribute, so it cannot be a successful control (i.e. submit any data).
The form has no submit button, so there is no obvious way for you to trigger the form submission. (You have to submit the form in a new HTTP request to the server to run the PHP and get the data from the user).
First, add the semicolons after every line when you use PHP. That can make a lot of troubles.
Second, some attributes have just one ("), like the label in the HTML.
Third, you are adding "something" called selected after echoing $car.
In the function cars() you are echoing without a semicolon (;) and just after doing a while() loop.
Check out all, you are missing very important and essential things
Also, I gonna guess this is not a "copy-paste" from your code, but if it's, you also are missing </select>
I have a while loop in PHP that selects data from a database
I want to have a complete button for each row returned which, when pressed will run an SQL Query to change the value of the status column of that particular row
my while loop is:
$stmt = $pdo_conn->prepare("SELECT * from messages where status = :status and (assigned_to = :assigned_to1 OR assigned_to = :assigned_to2) ");
$stmt->execute(array(':status' => '', ':assigned_to1' => $user_result["sequence"], ':assigned_to2' => ''));
$records = $stmt->fetchAll(PDO::FETCH_ASSOC);
$i=0;
if(count($records) > 0) {
echo '<tr>
<td colspan="7">You have '.count($records).' Messages</td>
</tr>';
foreach($records as $Messages) {
$i++;
echo '<tr>
<td>'.AdminNameLookup($Messages["assigned_to"]).'</td>
<td>'.$Messages["caller_company"].'</td>
<td>'.$Messages["caller_telephone"].'</td>
<td>'.$Messages["caller_email"].'</td>
<td>'.$Messages["caller_message"].'</td>
<td><input type="submit" name="CompleteMessages['.$i.']" value="" /></td>
</tr>';
}
}
but I'm not too sure on how to handle the PHP on submit?
Before sending data you need to create html form tag. And also you have pass values using input tag values.
format tag should be like this below code.
<form action="" method="">
<input type="" value="">
<input type="submit" name="CompleteMessages['.$i.']" value="" />
</form>
I would use this instead:
<td><input type="submit" name="CompleteMessages" value="'.$i.'" /></td>
You can then get the id with:
$Id = $_POST['CompleteMessages'];
Personally I'd have $i set to $Messages["message_id"] to you can find what Id you have actually submitted.
You also need to wrap everything in a form tag:
<form action="submit.php" method="POST">
...
</form>
If you only want to change the value of the row where you clicked the submit button,
then you will need a unique key for each record.
Lets assume that the messages table has a MessageID column.
One approach would be to call a javascript function.
Let's say your javascript function was called UpdateColumn(ID,ColName,Index)
Here's what would need to be added to each input button (pseudocode)
onclick="UpdateColumn($Messages['MessageID'],'Status',$i)"
Then your javascript will need to lookup value from input CompleteMessages[Index]
The Javascript could call your request php via ajax ...
update.php?MessageID=MessageID&Column=Status&Value=CompleteMessages[Index].value
And finally your php which handles the submit would take the values
using
$MessageID=$_REQUEST["MessageID"];
$Column=$_REQUEST["Column"];
$Value=$_REQUEST["Value"];
Then you will want to run a sql query which updates your database accordingly.
Well i heard that Value attribute is just a default attribute, so i used the value attribute to fetch the data from a previous file and fill form area.
But it is only using value of value attribute() to store in DB. For Example if the customer changes value passed in the text box(passed by value attribute) and submit the information the changed value is not being stored in DB rather value stored in value attribute is getting saved.
I am passing values to a html form, from a previous PHP page like this..
<form action="parentid1.php" method="post">
<label>id:</label>
<input type="text" name="id" value="<?php echo htmlspecialchars($_GET['id']) ?>" size="50"/> <br/>
<label>Name:</label>
<input type="text" name="name" value="<?php echo htmlspecialchars($_GET['name']) ?>" size="50"/><br/>
<input type="submit" value="submit" /><br/>
</form>
In my next file i am trying yo update the values declared like this:
$value = mysql_real_escape_string($_POST['id']);
$value1 = mysql_real_escape_string($_POST['name']);
$sql = "UPDATE table SET name='$value1'WHERE doi ='$value'";
Here is the case:
A form structure of text type will be formed with some value already in it.
Now customer wants to change the data in form box changes it and submits it but the changed is not being saved in the DB rather the previous value is only saved.
What am i doing wrong?
It seems that you mix GET and POST data. In html you use GEt but in php POST.
If you don't know about GET or POST try to use $_REQUEST.
$sql = "UPDATE `table` SET `name`='".$value1."' WHERE `doi`='".$value."'";
$result = mysql_query($sql) or die(mysql_error());
and show your mysql error.
any help would be great. Here's what I have so far:
PHP:
<?php
$arr1 = array("1080", "939", "769", "696","572", "498", "408", "369");
$arr2 = array("1275", "1095", "890", "799", "676", "580", "472", "423");
?>
PullDown Menu:
<select name="age" id="age">
<option value="<?php echo $arr1[0]; ?>">18-24</option>
<option value="<?php echo $arr2[0]; ?>">25-29</option>
</select>
Textfield:
<input name="planA" type="text" id="planA" value="<?php echo $arr1[0]; ?>" size="10" />
I need the textfield to show the correct value from the array, based on what I choose from the pulldown menu.
Thx.
When a user selects a value from the pulldown, they'll just see what you have between option tags; so for the second value, 25-29 would be displayed. According to your code, this has a corresponding value of 1275. If that's the value you need to populate in a text box, you could do so using Javascript:
<select name="age" id="age" onChange=PopulateField(this.value);>
...
And in the head of the document:
<script type="text/javascript" language="javascript">
function PopulateField(numValue)
{
document.getElementById('planA').value = numValue;
}
</script>
Of course realize that if Javascript is disabled on the user's browser, this won't work. When the form is submitted, you'll still be able to get the value of course, by working with the age variable; its value already is set to the element from the array. (I assume the text box is just for user feedback.)
Now, if the form is already submitted, and you want to show the results of the submission in the text field, then all you need to do is change your PHP code to reflect the selected value as so:
<input name="planA" type="text" id="planA" value="<?php echo $_POST['age']; ?>" size="10" />
If your form uses GET instead of POST, change $_POST to $_GET accordingly.
Edit (per comments):
If you have multiple textboxes to populate, it kind of depends whether you have just one pulldown menu to trigger them, or if there are multiple pulldown menus. I'll assume you have another pulldown menu to populate another textbox. In that case, you can use a second javascript function like this:
<script type="text/javascript" language="javascript">
function PopulateFieldA(numValue)
{
document.getElementById('planA').value = numValue;
}
function PopulateFieldB(numValue)
{
document.getElementById('planB').value = numValue;
}
</script>
On the pulldown that is associated with the textfield for plan B, use the appropriate onChange event:
<select name="ageB" id="ageB" onChange=PopulateFieldB(this.value);>
You could pass multiple arguments to a function so you could populate multiple boxes:
function PopulateFields(numValueA, numValueB)
{
document.getElementById('planA').value = numValueA;
document.getElementById('planB').value = numValueB;
}
Anything else is just speculation on my part because I don't really know the specifics of your application. Hopefully this helps set you on the right track.