Submit form without input field. - php

I am trying to submit a form (or a request) to do some tasks. The form doesn't have input field except a submit button.
I know it's kinda strange but I want to submit the form and get the data by using only php.
Is that possible?
My codes
if($_GET['submitImage']){
//do the stff I want......
}
<form id='formSubmitImage' name='submitImage' method='get' action='cm_ct_generate_preview.html'>
<?php
echo "<table>";
echo "<th>ID</th>";
echo "<th>Type</th>";
echo "<th>Name</th>";
echo "<th>Image</th>";
foreach($tests as $test){
echo '<tr>';
echo '<td>';
echo $test['ID'];
echo '</td>';
echo '<td>';
echo $test['Type'];
echo '</td>';
echo '<td>';
echo $test['Name'];
echo '</td>';
echo '<td>';
echo $test['FileName'];
echo '</td>';
echo '</tr>';
}
echo "</table>";
echo "<input type='submit' value='Set Images'></input><br>";
?>
</form>
Are there any alternative way to do this? Thanks a lot!

Instead of:
echo "<input type='submit' value='Set Images'></input><br>";
Why not:
echo "<button type='submit'>Set Images</button><br>";

You can add a GET parameter to the end of your action URL (in your case it would then be something like cm_ct_generate_preview.html?submitImage=1 or you can add a hidden input field in the form and check if that is submitted, for example:
<input type="hidden" name="submitImage" value="1" />
As your form method is already set to GET it should be sent as a GET parameter.

If the user needs to activate it, let them just press the submit button. However if you want to do it automatically, you will need to use a little javascript. To send the data with it, you can just specify your inputs like this:
echo '<input type="hidden" name="id" value="'.$test['ID'].'">';
the input fields will not appear but the data will be send like with an input field.

Related

How to clear select option in jQuery after submit

Hi I have a simple form which sends data to process.php.
My problem is how do I clear the select option after submitting the form?
I have a jQuery script that clears the select and brings back its default value but the problem is the default value is the one being passed in my process.php file instead of the selected value.
Here is my jQuery code and my form:
jQuery(document).ready(function() {
jQuery('#custom-submit-input').click(function(){
jQuery('#form-option').prop('selectedIndex',0);
});
});
echo "<form action='file-here/process.php' method='post' enctype='multipart/form-data' target='upload_target' id='form-reset'>";
echo "<iframe id='upload_target' name='upload_target' ></iframe>";
echo "<select name='id' id='form-option' class='test-only'>";
echo '<option selected="selected">' .'Choose a User'. '</option>';
foreach ($registeredUsers as $key => $value) {
$registered = JFactory::getUser($value);
echo '<option value="'.$registered->id.'">'.$registered->name.'</option>';
}
echo "</select>";
echo "<input name='uploadedfile' type='file' id='custom-file-input' class='test-only' /><br/>";
echo '<input type="submit" name="submit" value="Upload" id="custom-submit-input" >';
echo "</form>";
You can use either
jQuery('#form-option').val(''); // sets the value of the select to the first option with a value of ''
Or
jQuery('#form-option').get(0).selectedIndex = 0; // sets the value of the select back to the first option
you can use .reset() function.
jQuery(document).ready(function() {
jQuery('#custom-submit-input').click(function(){
jQuery('#form-option').reset();
});
});

Send column name from button

I have made a php page. On which I am displaying table 'prod' from my data base.
each row is displayed nicely. Today i tried to add a button named 'rate' at the end of each row of my table. which I did successfully. Now I want to send the the value of the first column of that row to another php page when that button is clicked. I am stuck that how to do so? can you help please ??
I know i have to use the method post in my form and i have to use $_post[that value] on the other php page to inculcate the value for further function.
I just need to ask that where to add the value of my first column in the button line. so that onclick it can send that value. I hope I am clear over this. Thank You very much for help :)
<?php
include("connection.php");
$query = "select * from prod";
$res = oci_parse($conn,$query);
usleep(100);
if (oci_execute($res)){
usleep(100);
print "<TABLE border \"1\">";
$first = 0;
while ($row = #oci_fetch_assoc($res)){
if (!$first){
$first = 1;
print "<TR><TH>";
print implode("</TH><TH>",array_keys($row));
print "</TH></TR>\n";
}
print "<TR><TD>";
print #implode("</TD><TD>",array_values($row));
print "</TD></TR>\n";
echo "<td><form action='detailform.php' method='POST'><input type='submit' name='submit-btn' value='Rate'/></form></td></tr>";
}
print "</TABLE>";
}
?>
you have to add inputs in your form whatever kind u prefer
echo "<td>
<form action='detailform.php' method='POST'>
<input type='hidden' name='your_val_key' value='".$row[your_val_key_in_query]."'> <!-- input hidden, change to text 4 debug -->
<input type='submit' name='submit-btn' value='Rate'/>
</form>
</td></tr>";
and than, in your detailform.php u can get the val with
echo $_POST["your_val_key"];
if u are not sure how much data u send or somthing, try this and u get the full data:
echo "<pre>".print_r($_POST,true)."</pre>";
BTW: why are u mixing print and echo?
Use hidden input
echo "<td><form action='detailform.php' method='POST'><input type='hidden' name='col-name' value='you-col-value'><input type='submit' name='submit-btn' value='Rate'/></form></td></tr>";

How to check which button have been submitted

I have a form, like this:
echo '<table align=center border=1>';
echo '<form method="post">';
echo '<tr>';
echo '<th>Lista Echipamente</th>';
echo '<th>Actiune</th>';
echo '</tr>';
while($row=mysql_fetch_array($sql)){
echo '<tr>';
echo '<td><input class="search-logi" id="tip" type="text" name="tip" value="'.$row['tip'].'"></td>';
echo '<td><input type=submit name=modifica value=Modifica></td>';
echo '</tr>';
}
echo '</form>';
echo '</table>';
Because of the loop, this form will have multiple rows. For each input, will be a button.
Lets say I want to add a value in the second input, then I will submit it (also the second button, obviously). Here comes the problem, in my code I have just a button, but the user see as many as the loop goes. How can I check which button is submitted?
Alternatively, you could utilize <button> tags for this purpose:
echo '<form method="post">'; // form tags are outside to be valid!!!
echo '<table align=center border=1>';
echo '<tr>';
echo '<th>Lista Echipamente</th>';
echo '<th>Actiune</th>';
echo '</tr>';
while($row = mysql_fetch_assoc($sql)){
echo '<tr>';
echo '<td><input class="search-logi" type="text" name="tip['.$row['id'].']" value="'.$row['tip'].'"></td>';
echo '<td><button type="submit" name="modifica" value="'.$row['id'].'">Modifica</button</td>';
echo '</tr>';
}
echo '</table>';
echo '</form>';
Then on the processing form:
$modifica = $_POST['modifica']; // shoud return the index num
$tip = $_POST['tip'][$modifica]; // select which textbox
Keep some unique reference to both input and button. Let say start a counter as 1 and then make the input id as input_1 and button as button_1 and then when you click button get the counter value that is 1 for above and then get the value of input_1 as per counter 1 and submit the form. And then increase counter so that id will increase as well. I think this will help you what you need.
Check all your $_POST variales which are empty and which are not.
Else you could write a javascript function to set all unused fields the value to "0" or to a string and check if it is used or not e.g "unused". In your php script you can check now for the value if it is set to "0" or "unused".
One way is to prefix the name of the input text field with a counter or other prefix. Bases on the name of the input field you can determine which button fired.

PHP newbie, how to send data on new page?

I'm learning PHP and I need help to "send" informations on an another page.
I read a text file and I put data on 2 arrays :
$nomVille;
$nomFichier;
$index; // index is use for arrays
Now I want to build a drop-down list and a submit button.
The drop-down list : show all items on $nomVille
The button : open "villes.php"
villes.php will have to retrieve $nomFichier and $index but I'm not able to do that.
Here is a part of my code :
echo 'Make your choice : ';
echo "<select name='ville'>";
foreach($nomVille as $option){
echo "<option value='{$option}'>{$option}</option>";
}
echo "</select>";
echo '<FORM METHOD="post" ACTION="villes.php">';
echo '<INPUT TYPE="submit" VALUE="Display">';
echo '</FORM>';
Is that correct ?
You've not got your select element wrapped in form tags which will prevent it being sent. What you actually want is this:
echo '<FORM METHOD="post" ACTION="villes.php">';
echo 'Make your choice : ';
echo "<select name='ville'>";
foreach($nomVille as $option){
echo "<option value='{$option}'>{$option}</option>";
}
echo "</select>";
echo '<INPUT TYPE="submit" VALUE="Display">';
echo '</FORM>';
On the page "villes.php", you should then be able to refer to the variable $_POST["ville"] which will contain the selected option in the select.
echo $_POST["ville"];
In order to transfer the values of $nomFichier and $index, you want to send them as hidden elements as part of the form, so add the lines:
echo "<input type='hidden' name='nomFichier' value='".$nomFichier."'>";
echo "<input type='hidden' name='index' value='".$index."'>";
Which will again, be able to be grabbed from the $_POST array.
Moving your select into the FORM element will allow the application to post the data on submit.
echo 'Make your choice : ';
echo '<FORM METHOD="post" ACTION="villes.php">';
echo "<select name='ville'>";
foreach($nomVille as $option){
echo "<option value='{$option}'>{$option}</option>";
}
echo "</select>";
echo '<INPUT TYPE="submit" VALUE="Display">';
echo '</FORM>';
From the villes.php page you then have data retrievable in the data that you can access like this:
if (isset($_POST['ville'])) {
$someVar = $_POST['ville'];
}
You'll find it worth your time to Google "PHP form tutorial" and seeing how things work from there.

Assign every TD in PHP While Loop Table a Link

I have a while loop that retrieves info from the mysql db. Now there is a column called profile. So I want every <td> in the profile to be a button that would have an action which would lead to a PHP page. How do I do that?
<form action="profile.php" method="get">
while($result)
{
echo '<table>';
echo '<tr>';
echo '<td>';
echo $result['profile'];
echo '</tr>';
echo '</td>';
echo '</table>';
}
Now every $result['profile'] should be a submit like - <input type="submit">
Thanks.
rewrite
echo $result['profile'];
to this code:
echo ''.$result['name'].'' ;
First of all,
echo '</tr>';
echo '</td>';
should be:
echo '</td>';
echo '</tr>';
To add a Submit button, change
echo '<td>';
to
echo '<td><input type="Submit" value="Submit">';
and
echo '</td>';
to
echo '</input></td>';
You will need to add the appropriate attributes to the submit button if applicable. Also, you will need to surround the submit button with a form element. Whether each link has its own form, or the entire table is surrounded by one is up to you.
Edit
To surround the table with a form tag, change echo '<table>'; to echo '<form><table>';. Then, change echo '</table>'; to echo '</table></form>';. Submit buttons are automatically "linked" to the form element they are contained in. All you have to do is to define the form action.
Edit
Actually, because you are using a GET request, this entire thing can be simplified by using links instead of forms. So go with RAMe0's answer.

Categories