I'm kinda rookie at PHP but i'm trying to developing a backoffice with some tables and value-editing options.
By now, i have tables like this:
But i'm kinda bugged with this issue:
The change button is being implemented like this:
for ($i = 0; $i < count($a); $i++) {
?>
<tr>
<td><i onclick="
document.form1.deviceFeatValID.value = <?php echo $a[$i][DEVICE_FEATURE_VALUE_ID] ?>;
document.form1.deviceID.value = <?php echo $a[$i][DEVICE_ID] ?>;
document.form1.deviceClassFeatID.value = <?php echo $a[$i][DEVICE_CLASS_FEATURE_ID] ?>;
document.form1.deviceFeatureVal.value = <?php echo $a[$i][DEVICE_FEATURE_VALUE] ?>;
document.form1.submit();" class="icon-refresh" ></td>
<td><?php echo $a[$i]['DEVICE_FEATURE_VALUE_ID']; ?><td><?php echo $a[$i]['DEVICE_ID']; ?><td><?php echo $a[$i]['DEVICE_CLASS_FEATURE_ID']; ?><td><?php echo $a[$i]['DEVICE_FEATURE_VALUE']; ?>
</tr>
<?php
}
?>
That is, by Javascript, i set the input values of this form, in the same .php:
<form name="form1" method="post" action="deviceFeatureValueFRM.php">
<input type="hidden" name="deviceFeatValID"/>
<input type="hidden" name="deviceID"/>
<input type="hidden" name="deviceClassFeatID"/>
<input type="hidden" name="deviceFeatureVal"/>
<input type="hidden" name="hiddenTypeField"/>
</form>
But...do you consider that this is a good practice? Is there another solution?!
The goal of this code is to detect which line is going about to be edited.
Kind regards,
Sam
Why do this with javascript? Just give the input field a value as such:
<input type="hidden" name="deviceFeatValID" value="<?php echo $a; ?>"/>
Where $a can be any variable you would like.
Well, I would recommend that you had all files separately. So let's say you had one file for PHP. One file for Javascript, and one for HTML. So if you want to use php functions or whatever just need to call them via AJAX.
That way you will have a cleaner code and a bit more secure one.
Related
I'm working on a CakePHP project. I want to merge the values of $value into a string/array so I can later merge it with the string "TESTING" then implode the outcome to a single string without spaces!!!
<?php
$merge="TESTING"; ?>
<form method="post" name="payment_form" action="<?php echo $action; ?>">
<?php foreach ($fields as $name => $value): ?>
<input type="hidden" name="<?php echo $name; ?>" value="<?php echo $value; ?>"/>
<?php endforeach; ?>
<p>some text<input type="submit" value="submit"/></p>
</form>
How can this be done? Thanks in advance.
EG: Please I want to keep the code as is. Meaning no for(var i=0;i++...) loops
Example
Let's say the values of the form are 1 2 3 productnr the outcome should be a string 123productnrTESTING no spaces just a string.
Well i wanted to send that data to a bank gateway to be processed. So it goes like this
1)Bank has sent me a string (The one called TESTING)
2)I print my form data (with the above code)
3) I need to send that data + the string provided into an imploded string to the bank gateway. Hope that clarifies some things :)
If I understand your question correctly, you would need to add a hidden field merge as a last one
The form page (View)
<?php
$merge="TESTING";
?>
<form method="post" name="payment_form" action="<?php echo $action; ?>">
<?php foreach ($fields as $name => $value): ?>
<input type="hidden" name="<?php echo $name; ?>" value="<?php echo $value; ?>"/>
<?php endforeach; ?>
<input type="hidden" name="merge" value="<?php echo $merge; ?>
<p>some text<input type="submit" value="submit"/></p>
</form>
The action page (Action Controller)
If you are using CakePHP, you would need to get POST data from the Cake Request object.
$merge = implode($this->request->params);
echo $merge;
here is my code:
<?php
ob_start();
session_start();
require 'connection.php';
require 'core.inc.php';
?>
<?php
$take_thread_pid_query = #mysql_query(" select pid from threads ");
$row_take_thread_pid = mysql_fetch_array($take_thread_pid_query);
$pid = $row_take_thread_pid['pid'];
while($row_take_thread_pid = #mysql_fetch_array($take_thread_pid_query))
{
?>
<form action="kill_threads.php" method="POST" >
<label> <?php echo "<br/><br/>thread".$row_take_thread_pid['pid']; ?><input type="submit" value = " <?php echo $row_take_thread_pid['pid'];?> " name = " <?php echo
$row_take_thread_pid['pid']; ?> " /> </label>
<?php }?>
</form>
<?php
$t = "4756";//[4756 is on of the pids in my thread table].this is for testing but doesnt works,it cant find any button with this name.
if ( isset($_POST[$t] ) ) echo "im a killed thread..";
?>
The big problem is that im trying to give different names to each button i create,but it seems this is not working because when im trying to see if a button isset['???']
What i have to do...?
For example
thread 1 [button 1]
thread 2 [button 2]
thread 3 [button 3 ]
So if now i click button 1 i want thread1 row deleted from database.
phpmyadmin works like this.
Im so complicated..please help,thanks in advance.
Your code is:
value = " <?php echo $row_take_thread_pid['pid'];?> "
name = " <?php echo $row_take_thread_pid['pid']; ?> "
You are placing spaces before and after the php text so you either have to remove the spaces or code for them
I would suggest you to use another hidden input element within the form instead of doing it with submit form, in this way you can kill all the threads with one if block by passing thread id to it.
while($row_take_thread_pid = #mysql_fetch_array($take_thread_pid_query))
{
?>
<form action="kill_threads.php" method="POST" >
<label> <?php echo "<br/><br/>thread".$row_take_thread_pid['pid']; ?>
<input type="hidden" name="pid" value="<?php echo $row_take_thread_pid['pid'];?>" />
<input type="submit" value = "Delete" name = "delete_thread" /> </label> <?php }?>
</form>
<?php
$t = "4756";//[4756 is on of the pids in my thread table].this is for testing but doesnt works,it cant find any button with this name.
if ( isset($_POST[$t] ) ) echo "im a killed thread..";
?>
Another problem you may come across is using multiple forms without name and id. So add a dynamic number in the form tag like like this
<form name="<?php echo $i; ?>" id="<?php echo $i; ?>" action="kill_threads.php" method="POST" >
Here $i is counter variable or you can use $row_take_thread_pid['pid'] for this purpose.
I have a problem showing the quantity for each article I check through a form which is getting data from a MySQL database I made.
Here is my code:
<?php foreach ($results_articles as $input) : ?>
<input type="text" name="<?php echo $input->id_article; ?>_txtbox[]" style="text-align: center; width:30px;"></input>
<input
type="checkbox"
name="checked_articles[]"
value="<?php echo $input->id_article; ?>">
<?php echo "(".
($input->ref_article).")".
($input->nom_article)." (".
($input->prix_article)." €)"; ?><br>
<?php endforeach; ?>
So, the "checked_articles" are showing up properly on my next page; but I really don't know how to show the quantity for each article checked.
I would like help from you !
Thanks! If you have any other infos, I'll reply of course!
I presume "quantity" is put in the text input, if so it should be accessible using:
foreach ($_POST['checked_articles'] as $checked_id)
{
$quantity = $_POST[$checked_id . '_txtbox'];
}
However, since the txtbox input is named to become an array, $quantity would result in an array. Maybe that is not what you intend. Then you could do it this way instead:
<input type="text" name="txtbox[<?php echo $input->id_article; ?>]" style="text-align: center; width:30px;" />
Having that, the textbox would be accessible using:
$_POST['txtbox'][$checked_id];
so i have this code fragment here..
if($numTF > 0)
{
echo "TRUE-AND-FALSE QUESTIONS: Enter them below followed by their correct answer.";
echo "<br>";?>
<form method="post" action="" name="quizform">
<?php for ($i=1; $i<=$numTF; $i++)
{
echo "Question"." ".$i;
?>`
<p><textarea name='question<?php echo $i; ?>' rows=3 cols=90></textarea></p>
<input type="radio" name="answer<?php echo $i; ?>" value="True"> True
<input type='radio' name="answer<?php echo $i; ?>" value="False"> False<br><br><br>
<?php
}
}
... i am making a quiz maker in php...
the first thing to do is to set up the desired number of questions, so the value entered will go on the $numTF variable. Depending on the entered value, the textarea part will be printed. and there will be different names for each text area. AND THE CODE ABOVE IS WHERE U PRINT THE FORMS AFTER U ENTER THE DESIRED VALUE.
The next thing is to save that in a database. since the name of each textarea will be based on a variable value($i) that is used in a loop (name="answer") , HOW CAN I USE IT IN $_POST??? Like, would i do it like this?? ($_POST['question']).
HOW CAN I SAVE THESE QUESTIONS IN A DATABASE??
PLEASE HELP ME ....
I WOULD BE SO MUCH MUCH MUCH GRATEFUL FOR A LIL HELP.
<?
var_dump($_POST);
?>
<form method="post">
<?
$numTF=4;
if($numTF > 0)
{
echo "TRUE-AND-FALSE QUESTIONS: Enter them below followed by their correct answer.";
echo "<br>";?>
<form method="post" action="" name="quizform">
<?php for ($i=1; $i<=$numTF; $i++)
{
echo "Question"." ".$i;
?>`
<p><textarea name='question[<?php echo $i; ?>]' rows=3 cols=90></textarea></p>
<input type="radio" name="answer[<?php echo $i; ?>]" value="True"> True
<input type='radio' name="answer[<?php echo $i; ?>]" value="False"> False<br><br><br>
<?php
}
}
?>
<input type="submit" name="submit" value="submit"/>
</form>
Use $_POST['question'][1] // To get first question
Use $_POST['answer'][1] // To get first answer
Use loop to get all question and answers
I agree with Sachin as far as using name='question[]'. To answer question a little more as far as storing it in a database. Personally I would use a JSON array.
$store_answers = json_encode($_POST['answer']);
$store_questions = json_encode($_POST['question']);
Then just store $store_string in a TEXT field in your database. Then when you pull it back out of the database you can simple use:
$answers = json_decode($store_answers);
$questions = json_decode($store_questions);
Then you can loop through using a foreach like so:
foreach($questions as $key=>$question) {
echo "Question $key = {$answers[$key]} <br />";
}
This will display the results for each question.
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
How to loop through dynamic form inputs and insert into an array
I have a php script and a form. The php script makes an xml file but what i need is for someone to enter a number and that would set that amount of textboxes that would be for someone to write data for that xml file.
So i need it to write <input type="text" name="a #"> however many times the user enters. Also the name needs to be a number but it counts by one ex:<input type="text" name="1"> <input type="text" name="2">... Thanks
<?php
session_start();
if(isset($_POST['quantity']){
// code here to check isnum and min/max
$count = $_POST['quantity'];
for ($i=1; $i<=$count; $i++){
#$s.= "<input type=text name=".$i."><br>";
}
?>
now just echo out $s in your html
This?
<form method="get" action="">
<div><input type="text" name="num_inputs" value="1" placeholder="Number of inputs"/></div>
</form>
<?php $num_inputs = isset($_GET['num_inputs']) ? $_GET['num_inputs'] : 1; ?>
<form method="post" action="">
<?php for ($i = 0; $i < $num_inputs; $i++) : ?>
<div><input type="text" name="inputs[]"/></div>
<?php endfor ?>
</form>
Edit: yes, an array is much better than input_x. Updated my answer.
I think what you want is an array of form fields.
You want something like this:
<?php
$number_of_textboxes = 5; // you'd get this from a $_GET parameter
echo str_repeat('<input type="text" name="mybox[]" />', $number_of_textboxes);
?>
This will print five text boxes:
<input type="text" name="mybox[]" />
Then, when you reference these boxes' values, you do so like thus:
<?php
foreach ($_POST['mybox'] as $i) {
echo $i;
}
?>
That is, by using "mybox[]" as the name of each input field, you create an array of textboxes, which you can then iterate through.