Automatically click the button for you if there is a date - php

I have a file called file.php which has:
$row2= pg_fetch_array($result2);
$website = 'http://validate/?id='.$newDateString;
<tbody>
<tr>
<td><?php echo'<a href="'.$website.'">'. $row['name']; ?></td>
The code sends to a file named jasmine.php the url with the date since jasmine contains a calendar. It sends it when you click on the date which contains a link to jasmine.
Jasmine.php:
<div id="content">
<h2>Hello, Please let me know which date you are looking for </h2>
<div><label style="font-size: 14pt; font-family: Verdana;">Posting Date: </label></div><br />
<input type="text" id="date" value= "<?php echo $_GET['id']?>" class="css-input" placeholder="Posting Date..." readonly="readonly" /><br /><br />
<input type="button" id="validate" value="Let's work!" class="btn" />
<input type="button" id="clear" value="Wanna go Again?" class="btn" />
</div>
<div id="contentarea2" style="display: none;">
<h2>Results:</h2>
<div class="view_alerts">
<table>
<tr>
<td>
Code
</td>
<td>
# Posted
</td>
</tr>
<tbody id="view_alerts_tbody">
</tbody>
</table>
</div>
</div>
In Jasmine, I get the date already written for me from file.php, all I need to do is click the button so it could show me a table, is there a way, that it clicks the button on its own if theres a date written, so when I go on that link, it already puts the date and takes me to the table in jasmine.php

Taking the code from the answer above the if statement here should evaluate the input fields value (id is '#date'). An empty value will default to false hence the ! before the selector.
jQuery(document).ready(function()
{
if(!$('#date').val()) {
jQuery("#Your-Button-ID").click();
}
});

You can use below JQuery code for your purpose. When page load is completed (i.e. Page is ready)
jQuery(document).ready(function()
{
jQuery("#Your-Button-ID").click();
});

Related

Codeigniter - Appended Array items lost when post to controller

I have a view page
$nameArray = array('name' => 'Formname', 'id' => 'Formname');
echo form_open_multipart('controller/file_upload_function',$nameArray);
<table id="companytable" >
<tr><td><input type="file" id="dummyfile" name="dummyfile"></td></tr>
<tr>
<td >
<label id="addlabel" style="cursor: pointer;">
<i class="fa fa-plus-circle" aria-hidden="true"></i></label>
</td>
<tr>
<td>
<input type="text" name="companyname[]" class="cmp_textbx" placeholder="Enter Company">
</td>
</tr>
<tr>
<td>
<input type="submit" id="btn_upload" name="btn_upload" value="Save">
</td>
</tr>
</table>
<?php echo form_close(); ?>
Using Jquery I need to append more 'companyname' fields according to user(On cliking 'add company' label).'companytable' is the id of the table.
$("#addlabel").click(function(){
$("#companytable").append('<tr><td><input type="text" name="companyname[]" class="cmp_textbx" placeholder="Enter Company"></td></tr>');
});
The problem is when I try to submit my view and try to get the array items 'companyname'.
$company_items = array();
$company_items = $this->input->post('companyname');
print_r($company_items);
I am able to get only the first item of the array that is already given the view page.The appended array item is lost.Can somebody help me, please?
Your HTML code works fine. The problem is from jquery or add button. if you are using html button tag for that, then you have to use preventDefault() in the jquery like
$('.add').click(function(e){
e.preventDefault();
$("#companytable").append('<tr><td><input type="text" name="companyname[]" class="cmp_textbx" placeholder="Enter Company"></td></tr>');
});
or you can use input while selecting type as button, for this you don't need any changes for your code.

Get value of a table data on button click

I am building a online exam application, here paper name and no. of papers are retrieved from database. Now I want to get the paper code of that paper for which I clicked the start button. Code for the table is here:
<form method="post" action="exam_page.php" >
<table >
<tr style="background-color: #7F859E;color:white; height:50px;">
<th style="padding-left:140px; width:550px;">Paper</th>
<th style="padding-left:40px;">Time</th>
<th style="padding-left:40px;">Duration</th>
<th style="padding-left:40px; width:250px;"></th>
</tr>
<?php
$i=1;
while($row=mysql_fetch_array($rs)){?>
<tr style="height:80px; background-color: #CCCCCC;">
<td style="padding-left:40px;">
<input type="text" value="<?=$row['paper_code']?>" name="paper_code<?=$i?>" readonly><?=$row['paper_name']?>
</td>
<td style="padding-left:40px;">
<input type="text" value="<?=$row['time']?>" readonly style="width:90px;">
</td>
<td style="padding-left:40px;">
<input type="text" value="<?=$row['duration']?> Min" readonly style="width:90px;">
</td>
<td style="padding-left:40px;"><button style="width:100px;">Start</button></td>
</tr>
<?php $i++; } $_SESSION['exam']=$i; ?>
</table>
</form>
Name your submit button, (also make it a submit type) and assign the paper code to its value attribute.
<button type="submit" style="width:100px;" name="clicked" value="<?=$row['paper_code']?>">
Start
</button>
Now, in exam_page.php you can get the value of the clicked button from $_POST['clicked']. (Or whatever you decide to name it.)
To get the values from the other inputs associated with the button you clicked, you can add the paper code to their names instead of using $i.
<input type="text" value="<?=$row['time']?>" name="time[<?=$row['paper_code']?>]">
and in exam_page.php you can get the value from $_POST['time'][$_POST['clicked']], etc.
If they aren't intended to be editable in your form, though, I would recommend using something else to display them and just loading them from the database in exam_page.php instead. Otherwise, your users will be able to override the readonly attribute and submit different values.
Try using javascript onclick functions:
<script>
function getPaperCode(paperCode)
{
alert(paperCode);
}
</script>
Then edit your input add onclick event:
<input type="text" value="<?php echo $row['paper_code']; ?>" onclick="getPaperCode('<?php echo $row["paper_code"]; ?>');" name="paper_code<?php echo $i; ?>" readonly><?=$row['paper_name']?>
Once you click. it will alert the value of the button
passed your unique id value or examcode via hidden field like this
<input type="hidden" name="id" value="<?=$row['eid']?>">
and on button click perform query like
$id=$_POST['id'];
select * from table_name where id='$id';

Reset button not working in html (php)

I have placed my input type reset button inside form but its still not working.
<form method='post' action='process/update_news_action.php' >
<tr>
<td colspan='2' class="col-md-4">Update News Content</td>
</tr>
<tr >
<td colspan='2' class="col-md-8"><textarea name="news" id="text"><?php echo $rows["news"] ; ?></textarea></td>
</tr>
<tr>
<td class="col-md-4"><input name='submit' type="submit" value='Publish' class="btn btn-success" /></td>
<td class="col-md-4"><input name='reset' type="reset" value='Reset' class="btn btn-primary" /></td>
</tr>
</form>
I have also tried <button type="reset" value="Reset">Reset</button>
If you're expecting the reset button to empty out the form, that's not the way reset works. It will reset the form fields to what they were when the page was loaded. In this case, you have pre-filled content in your form, so clicking reset will simply undo any user changes since the page-load.
To reset all form fields to blank/nothing, you will need javascript. First get the form DOM object, then iterate over all the input types you want to reset, and set each field.value = '' (input text types / textarea), or modify attributes (for select, radio, checkbox etc.) to deselect and so on. Turn that into a function (there's probably a ready piece somewhere out there, I seem to have lost mine), and attach it to your reset button's click event -- or your form's reset event.
Edit: On a quick search, here's a basic example of how to clear a form to empty values.
Edit: If you're not concerned over anything but text fields, here's a simple way to do this. We add a "reset" event listener to the form, and when it fires, all fields are set to empty string.
function setFormCleanReset(formId) {
let formEl = document.querySelector(formId);
// Add event listener for reset event
formEl.addEventListener('reset', function(e) {
// Iterate all non-hidden fields, set values to ''
for(const fieldEl of formEl.querySelectorAll('input:not([type=hidden])')) {
// #todo check input type and handle "select" etc.
fieldEl.setAttribute('value', '');
}
});
}
// usage: setFormCleanReset('my_form_id');
I think you forgot to insert form tag in your html. It should be work if you insert button code into <form>..button code here..</form>.Something like this:
<form>
<input type="text">
<td class="col-md-4">
<input name='reset' type="reset" value='Reset' class="btn btn-primary" />
</td>
</form>
You should place the <input type="reset"> in a form like this,it will definitely work
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Tring Reset</title>
<style>
</style>
</head>
<body>
<form>
<input type="text"></input>
<input type="text"></input>
<input type="text"></input>
<input type="text"></input>
<input type="text"></input>
<input type=reset></input>
</form>
</body>
</html>
You just need to put your reset button inside form. In following code I have taken one input box also an an example. I hope it helps.
<form>
<input type="text" />
<table>
<td class="col-md-4">
<input name='reset' type="reset" value='Reset' class="btn btn-primary" />
</td>
</table>
</form>
can you please try without echo anything inside textarea
try this
<form method='post' action='process/update_news_action.php' >
<tr>
<td colspan='2' class="col-md-4">Update News Content</td>
</tr>
<tr >
<td colspan='2' class="col-md-8"><textarea name="news" id="text"></textarea></td>
</tr>
<tr>
<td class="col-md-4"><input name='submit' type="submit" value='Publish' class="btn btn-success" /> </td>
<td class="col-md-4"><input name='reset' type="reset" value='Reset' class="btn btn-primary" /> </td>
</tr>
</form>
I think reset button is working fine.
Excuse Me! You are using default value in input attribute. Remove default value from it. Then type anything then click rest it will make your text box empty.
I have a form submitting variables to another PHP file. In the second file, there is a button that leads back to the first file for eventual correction od submitted values. The values are retained in the form by $_POST. The "Reset" button works only when the form is used the first time (empty). After submitting and return from the second PHP file, the "Reset" doesn't do anything, no values are cleared. The button seems to be fully inactive.
<button title="<?php echo $lang['reset-button-hint']; ?>" alt="TOOLTIP 1" type="reset" name="reset"><?php echo $lang['reset-button']; ?></button>
#Jaspreet Kaur You need to add form tag outside the table. Please check my previous comment for it.

PHP POST value from a looped table

Please help me out of this.....
I am designing a table which is inside a form.
the table is generated based on while loop.
In each row there is a download button.
when i click download the POST value should get the same row information.
But my POST variable is giving me the last row information only.
I tried using input-type as hidden... But it did not work
Here is the code for your reference
enter code here
<form name="simpleform" method="post" action="insert.php">
<?php
$data = "environment";
$user_name = $_SESSION['username'];
$serch = mysql_query("SELECT * FROM data WHERE (data_category = '" . $data . "') ");
while ($record=mysql_fetch_assoc($serch))
{?>
<tr class="warning">
<td >
<input type="text" value=<?php echo $record['data_ID'];?> readonly="readonly" >
<input type="hidden" value=<?php echo $record['data_ID'];?> name="dataid" />
</td>
<td >
<input type="text" value=<?php echo $record['data_name'];?> readonly="readonly" >
<input type="hidden" value=<?php echo $record['data_name'];?> name="dataname" />
</td>
<td >
<input type="text" value=<?php echo $record['data_downloads'];?> readonly="readonly">
<input type="hidden" value=<?php echo $record['data_downloads'];?> name="datadown" />
</td>
<td >
<input type="text" value="" >
<input type="hidden" value="" name="datause" />
</td>
<td>
<input type="submit" name="simplesubmit" value="Go to download" />
</td>
</tr>
<?php }
exit;
?>
</tbody>
</form>
The problem is that you are using the same name attribute for all your controls. Thus, when PHP receives the form, they get overwritten, and you only see the last value of the form.
The simplest way to avoid that is just appending [] to the end of your names -- eg name=dataid[]. This will make PHP take all arguments as an array, so you don't lose data.
The second problem, is that your submit button also has the same name - you should diversify it by using some row-specific data in its name, such as 'name="submit-'.$record['data_name'].'"'
For more info, more code from you is needed, such as what are the data you are printing like.
Every post button can have its name and value, so if you change your code to produce a traceable post button name you can just do whatever you want with that.
<table>
<tr>
<td>...</td>
<td>...</td>
<td><input type="submit" name="submit[1]" value="OK" />
</tr>
<tr>
<td>...</td>
<td>...</td>
<td><input type="submit" name="submit[2]" value="OK" />
</tr>
</table>
When the form is posted its very easy to capture which button is clicked;
if ($_POST["submit"]) {
$id = key($_POST["submit"]);
}
Thanks for info... and good response. As you said , i replaced the same and saw the post value is giving me all arguments as array. My purpose is to let the client download file that he clicks. so if the client click the first row button in the table, the post value should get only that Data_name. So that i can run a query to get the URL of that data_name and download

List box sending data to textarea upon click of send button

I am using a micro message PHP script that allows user to submit messages in a simple and clean form. It uses a database for the entries. The form part of it looks like this:
<form name="new_post" method="post" action="admin.php?page=wp-admin-microblog/wp-admin-microblog.php" id="new_post_form">
<table>
<thead>
<tr>
<td>
<div id="postdiv" class="postarea" style="display:block;">
<textarea name="wpam_nm_text" id="wpam_nm_text" style="width:100%;" rows="4"></textarea>
</div>
<p style="text-align:right; float:right;"><input name="send" type="submit" class="button-primary" value="<?php _e('Send', 'wp_admin_blog'); ?>" />
</td>
</tr>
</thead>
</table>
</form>
I want to add a drop-down list to the form and depending on what the user selects in the drop-down list, a custom text should be added to the beginning of the textarea (wpam_nm_text) message when the user clicks the send button.
Let me explain this. I want to add something like this:
<select>
<option>Milk</option>
<option>Coffee</option>
<option>Tea</option>
</select>
...to the form, and if the user selects the option "Coffee", then I want to add the text "Coffee keeps me awake" to the textarea when the user clicks on the send button. So when the form is sent to the database, it will contain "Coffee keeps me awake" followed by what the user wrote in the textarea.
I would presume that this is possible using Javascript or PHP, both of which I have no good knowledge of, so detailed replies are beneficial. Also, if you require more info or script, by all means, feel free to ask.
Here is a fiddle if you want one: http://jsfiddle.net/tNrfA/
The answer posted by codingbiz will work for you ..(can't upvote,no priveleges :( ).
However if you want a javascript solution I think this should work for you ..
<script type="text/javascript">
function setText(){
var drink = document.getElementById('drink');
var feed = document.getElementById('feed');
feed.value = drink.value + " keeps me awake:" + feed.value;
}
</script>
<form name="new_post" method="post" id="new_post_form">
<table>
<thead>
<tr>
<td>
<div id="postdiv" class="postarea" style="display:block;">
<textarea name="wpam_nm_text" id="feed" style="width:100%;" rows="4"></textarea>
</div>
<select id = "drink">
<option>Milk</option>
<option>Coffee</option>
<option>Tea</option>
</select>
<p style="text-align:right; float:right;"><input name="send" type="submit" onClick="setText()" class="button-primary" id ="submit" value='go' />
</td>
</tr>
</thead>
</table>
</form>​
I think you can access your select dropdown value from the server side and append it to the text
HTML
<select name="choice">
<option>Milk</option>
<option>Coffee</option>
<option>Tea</option>
</select>
PHP
<?php
$wpam_nm_text = $_POST['wpam_nm_text'];
$choice = $_POST['choice'];
$customText = "default";
if($choice == "Coffee")
$customText = "Coffee keeps me awake";
else if($choice == "Milk")
$customText = "WhatStuffYouWantHere";
//combine the dropdown value and the textarea value
$requiredVal = $customText.' '.$wpam_nm_text;
print($requiredVal);
//do whatever you want with $requiredVal
?>
That should do it
You are correct, a simple bit of javascript can help you accomplish this.
<script>
var drinks = {
Milk: "does a body good",
Coffee: "keeps me up",
Tea: "is yummy"
};
function optionChange(){
var drinkText = drinks [document.getElementById('drinkSelection').value]
document.getElementById('wpam_nm_text').value = drinkText;
}
</script>
here is the updated fiddle
http://jsfiddle.net/tNrfA/6/
EDIT:
I did not see at first that this was on submission of the page. For that, what codingbiz posted should work just fine. This way should only be used if they can update the custom text.
If you did need to do it on the front end though, you could do something like this.
fiddle: http://jsfiddle.net/tNrfA/14/
var drinks = {
Milk: "does a body good",
Coffee: "keeps me up",
Tea: "is yummy"
};
function submitForm(){
drinkText = drinks [document.getElementById('drinkSelection').value]
document.getElementById('wpam_nm_text').value = drinkText + ' ' +document.getElementById('wpam_nm_text').value;
document.getElementById('new_post_form').submit();
}
<div id="postdiv" class="postarea" style="display:block;">
<textarea name="wpam_nm_text" id="wpam_nm_text" style="width:100%;" rows="4"></textarea>
</div>
<select id="drinkSelection">
<option>Milk</option>
<option>Coffee</option>
<option>Tea</option>
</select>
<p style="text-align:right; float:right;"><input name="send" type="button" class="button-primary" value="<?php _e('Send', 'wp_admin_blog'); ?>" onclick="submitForm()" />
</td>
</tr>
</thead>
</table>
</form>​

Categories