I am trying to pass the select values for form action URL for wordpress,
$args_shortcode = shortcode_atts(array(
'category_name' => ''
), $attr);
$condition = "";
if(isset($_POST['Collateral_services'])){
$condition = "/?Collateral_services=".$_POST['Collateral_services'];
}
$action_url = site_url() . "/category/" . $args_shortcode['category_name'] . $condition;
As you can see, the action url will redirect user to a different page which is getting fetched from shortcode attribute, so $_POST['Collateral_services'] is not fetching any data because it needs to be on search result page,
But the action URL needs to be based on selected option of the form, so I am not getting how can I change values in url after "/category/" using javascript or jQuery ?
form:
<form method="POST" action="<?php echo $action_url; ?>">
<div class="form-group">
<div class="col-xs-12 col-sm-12">
<select name="Collateral_services" class="custom-select-input">
<option value="" disabled selected>All Services</option>
<?php
foreach ($terms_ser as $term_ser) {
?>
<option value="<?php echo $term_ser->slug; ?>"><?php echo $term_ser->name; ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="custom-search-button-align">
<input type="submit" value="Filter" class="custom-search">
</div>
</form>
Related
I have a PHP code and JSON as shown below:
PHP Code:
<?php if (!empty($_POST) && isset($_POST['savechanges']) && $_POST['savechanges'] == 1 && isset($_SESSION['pageadmin'])) {
$output = array();
$output['en_desc']=$_POST['en_desc'];
$output['code']=$_POST['code'];
$fp = fopen('../feeds/ptp-ess_landing_scommittees.json', 'w');
fwrite($fp, json_encode($output));
fclose($fp);
}
if(file_exists('../feeds/ptp-ess_landing_scommittees.json')){
$data = json_decode(file_get_contents('../feeds/ptp-ess_landing_scommittees.json'));
}
?>
<?php if($data) { ?>
<form method="post" id="myform" style="text-align:left;">
<input type="hidden" id="savechanges" name="savechanges" value="1">
<div style="text-align:center; margin-right:9px; margin-bottom:24.703px;">
<button type="submit">Save</button>
</div>
<?php foreach ($data->code as $key => $value) { ?>
<div class="house-senate-committee" style="text-align:center; margin-top:15px;">
<button type="button" onclick="removeRow(this)" style="margin-right:10px;">Delete</button>
<input type="text" name="code[]" style="margin-right:10px;" value="<?= $data->code[$key] ?>">
<input type="text" name="en_desc[]" value="<?= $data->en_desc[$key] ?>">
</div>
<?php } ?>
</form>
<?php } else { echo 'Cannot read JSON settings file'; }?>
JSON:
{"code":["AEFA","AGFO"], "en_desc":["Foreign Affairs and International Trade","Agriculture and Forestry"]}
The following DOM is generated through the PHP/JSON code above:
DOM (HTML):
<div class="house-senate-committee" style="text-align:center; margin-top:15px;">
<button type="button" onclick="removeRow(this)" style="margin-right:10px;">Delete</button>
<input type="text" name="code[]" style="margin-right:10px;" value="AEFA">
<input type="text" name="en_desc[]" value="Foreign Affairs and International Trade">
</div>
<div class="house-senate-committee" style="text-align:center; margin-top:15px;">
<button type="button" onclick="removeRow(this)" style="margin-right:10px;">Delete</button>
<input type="text" name="code[]" style="margin-right:10px;" value="AGFO">
<input type="text" name="en_desc[]" value="Agriculture and Forestry">
</div>
The following JS code deletes a row from the DOM on click of a delete button. On refreshing the page,
the deleted row comes back again as everything is rendered through JSON.
JS code:
<script>
function removeRow(el) {
el.parentNode.remove();
}
</script>
Problem Statement:
The above JS code is deleting the row (on click of a delete button) from the DOM but on refresing the page, everything is rendered again.
I am wondering what PHP code I need to add so that it delete the values from the JSON on saving the form when row is deleted from DOM through JS.
Step 1: User delete the row from the DOM on click of a delete button.
Step 2: On saving the form and rendering the page, that deleted row should not be present.
I know I have to use unset function in order to remove the values from the JSON but I am not sure how I can integrate it in the form.
unset($data->code);
unset($data->en_desc);
You have a typo here:
$data = json_decode(file_get_contents('../feeds/ptp-ess_landing_scommittees.json'));
it should be
$data = json_decode(file_get_contents('../feeds/ptp-ess_landing_committees.json'));
Look at the "s" :)
Edit: you also were saving the new file without actually checking if there is a post happening, here is the full code:
<?php
if (isset($_POST['submit'])) {
$output = array();
$output['en_desc'] = $_POST['en_desc'];
$output['code'] = $_POST['code'];
$fp = fopen('../feeds/ptp-ess_landing_committees.json', 'w');
fwrite($fp, json_encode($output));
fclose($fp);
}
if (file_exists('../feeds/ptp-ess_landing_committees.json')) {
$data = json_decode(file_get_contents('../feeds/ptp-ess_landing_committees.json'));
}
?>
<?php if ($data) { ?>
<form method="post" id="myform" style="text-align:left;">
<div style="text-align:center; margin-right:9px; margin-bottom:24.703px;">
<button type="submit" name="submit">Save</button>
</div>
<?php foreach ($data->code as $key => $value) { ?>
<div class="house-senate-committee" style="text-align:center; margin-top:15px;">
<button type="button" onclick="removeRow(this)" style="margin-right:10px;">Delete</button>
<input type="text" name="code[]" style="margin-right:10px;" value="<?= $data->code[$key] ?>">
<input type="text" name="en_desc[]" value="<?= $data->en_desc[$key] ?>">
</div>
<?php } ?>
</form>
<?php } else {
echo 'Cannot read JSON settings file';
} ?>
<script>
function removeRow(el) {
el.parentNode.remove();
}
</script>
My drop down is showing blank then when i select the value of dropdown the same value is showing, but i have to show dropdown value as select first then when I click on button the respective value should show
I am doing a Php program
<form class="form-horizontal" name="form" method="post" action="<?php $_PHP_SELF?>">
<label for="courseDisp" class="col-sm-2" style="margin-top:10px;">Course : </label>
<?php
$course="SELECT * from course";
$res= $conn->query($course);
if($res->num_rows>0)
{
echo '<select name="courseDisp" id="courseDisp" class="form-control col-sm-3" style="margin-top:8px;display:inline;padding:10px;">';
echo '<option value="0" selected> -- SELECT --</option>';
while($row=$res->fetch_assoc())
{
echo '<option value='.$row["course_id"].'>'.$row['shortname'].'</option>';
}
echo '</select>';
} else {
echo "0 result";
}
?>
<label for="yearDisp" class="col-sm-2" style="margin-top:10px;">Year : </label>
<?php
$year="SELECT distinct(year) from syllabus";
$res= $conn->query($year);
if($res->num_rows>0)
{
echo '<select name="yearDisp" id="yearDisp" class="form-control col-sm-3" style="margin-top:8px;display:inline;padding:10px;">';
echo '<option value="0">-- SELECT --</option>';
while($row=$res->fetch_assoc())
{
echo '<option value='.$row["year"].'>'.$row['year'].'</option>';
}
echo '</select>';
} else {
echo "0 result";
}
?>
<script type="text/javascript">
document.getElementById('courseDisp').value = "<?php echo $_POST['courseDisp'];?>";
document.getElementById('yearDisp').value = "<?php echo $_POST['yearDisp'];?>";
<input type="submit" class="btn col-sm-2" style="margin-left:15px;margin-top:10px;width:60px;font-weight:bold;font-size:15px;" value="GO" name="btnGo" id="btnGo" />
</form>
I think you are doing it in a wrong way:
your code should look like this
<script type="text/JavaScript">
var valueSelected=document.getElementById('course').value;
alert(valueSelected);// do here according to the need
</script>
This is because there is no $_POST variables present before you submit a form.
$_POST variables can only be 'accessed' whenever a POST form is submitted, so when the form is not submitted, $_POST['course'] will be undefined. If you want to use persistant, but also relative variables, use $_GET.
This can be done the following way:
<script type="text/javascript">
document.getElementById('course').value =<?php echo $_GET['course'];?>";
</script>
(this will cause an error if value is not set, make sure to make exceptions for that, using if statements in PHP)
but the value also needs to be fetched from the URL.
so your url needs to have ?course=<course_value> in it, for example:
https://example.com/index.php?course=Course%201
Click here for more about POST vs GET requests
Instead of setting the value with javascript, you should directly write the selected attribute.
<select name="course">
<?php foreach ($options as $key => $value): ?>
<option value="<?= $key ?>"<?php if ($key == $_POST['course']) echo " selected" ?>>
<?= $value ?>
</option>
<?php endforeach; ?>
</select>
If you have to do this in javascript, keep sure, you use the correct syntax. Your example has a wrong " at the end of the line. Also you should use json_encode, if you want to output vars into javascript. And a last thing - if you don't put this inside the document ready event, the script has to be placed after the select element, which you wan't to manipulate
<select name="course">...</select>
...
<script type="text/javascript">
document.getElementById('course').value = <?= echo json_encode($_POST['course']) ?>;
</script>
Needed to keep the <option value="">-Select-</option>
I have a multiselect dropdown, which allows me to select more than one user at a time. I want to be able to select more than one user and when I click on the "Send code" button, the application will snow send email to these users using the email addresses. Currently when I select and click "Send Code", the application will only send email to only one user leaving other once. Please I will like to know to be sending email to all selected users at a time. I'm using CodeIgniter and below is my code:
Below is the view section of my code, which enable user to select users and send code:
<head>
<meta charset="UTF-8">
<title>Member Invite</title>
</head>
<body>
<form class="form-horizontal" method="post" action="<?php echo site_url('root/administrator/sendinvite'); ?>">
<fieldset>
<div class="control-group">
<label class="control-label" for="select01">Select Set</label>
<div class="controls">
<select id="select01" class="chzn-select" name="set_code" title="Select the set this student(s) will belong to">
<option value="">Select set</option>
<?php foreach ($sets as $set) { ?>
<option <?php echo set_select('set_code', $set->group_id); ?> value="<?php echo $set->group_id; ?>"><?php echo $set->group_name; ?></option>
<?php } ?>
</select>
<font size="1" color='red'><?php echo form_error('set_code'); ?></font>
</div>
</div>
<div class="control-group">
<label class="control-label" for="multiSelect">Select student(s)</label>
<div class="controls">
<select multiple="multiple" id="multiSelect" name="student_email" class="chzn-select span4" title="Select student(s) for this set">
<option value="">Select Student</option>
<?php foreach ($students as $student) { ?>
<option <?php echo set_select('student_email', $student->uacc_email); ?> value="<?php echo $student->uacc_email; ?>"><?php echo $student->uacc_username; ?></option>
<?php } ?>
</select>
<p class="help-block">Start typing to activate auto complete!</p>
<font size="1" color='red'><?php echo form_error('student_email'); ?></font>
</div>
<input type="hidden" name="student_id" value="<?php echo $student->upro_uacc_fk; ?>" class="span6 m-wrap"/>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Send code</button>
<button type="reset" class="btn">Cancel</button>
</div>
</fieldset>
</form>
</body>
</html>
Below is my Controller, which accepts the selected users and used same to send email:
public function sendinvite() {
if ($this->input->post()) {
$this->form_validation->set_rules('student_email', 'Student name', 'required');
$this->form_validation->set_rules('set_code', 'Set name', 'required');
if ($this->form_validation->run() == FALSE) {
$this->data['students'] = $this->super_model->get_members_with_out_group();
$this->data['sets'] = $this->super_model->get_group_list_for_code_invite();
$this->data['content'] = 'root/invitestudent';
$this->load->view('layout/root/template', $this->data);
} else {
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'mail.mydomain.com',
'smtp_port' => 25,
'smtp_user' => 'root',
'smtp_pass' => '347y6Z45Rwlfub020',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$len = 8;
$type = 'HhJjKkMmNnPpQqRrSsTt';
$set = $this->input->post('set_code');
//$new_password = rand(34546, 89757);
$this->email->from('admin#passwordmanager.com', "Admin Manager");
$this->email->to($this->input->post('student_email'));
//$this->email->cc($this->input->post('student_email'));
$this->email->subject('BSN-Set Code');
//$this->flexi_auth->forgotten_password($identity);
$this->email->message("You are invited to join the group. Your set code is: $set");
$data['message'] = "Sorry Unable to send email...";
if ($this->email->send()) {
$data['message'] = "Mail sent...";
}
redirect('root/administrator/invitesuccess');
}
} else {
$this->data['students'] = $this->super_model->get_members_with_out_group();
$this->data['sets'] = $this->super_model->get_group_list_for_code_invite();
$this->data['content'] = 'root/invitestudent';
$this->load->view('layout/root/template', $this->data);
}
}
Image is here
you can use like this
// Array ( [0] => c#gmail.com [1] => a#gmail.com [2] => b#gamil.com )
$addresses = implode(',',$this->input->post('student_email');
// c#gmail.com,a#gmail.com,b#gamil.com
$this->email->to($addresses);
I saw few pages but tried all and none worked for me.
This is what i want:
on my html:
<form class="form-horizontal" role="form" method="POST" action="">
<select name="customer_id" id="customer_id" class="form-control">
<option>Select customer</option>
<?php foreach($customers AS $customer): ?>
<option value="<?php echo $customer->id;?>"><?php echo $customer->name; ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="form-group">
<label for="category" class="col-xs-4 control-label">Category</label>
<div class="col-sm-4 col-xs-8">
<input type="text" class="form-control" name="category" id="category" placeholder="Category" disabled="disabled">
</div>
</div>
Then i have a script to auto fill the category field depending on the customer selected, ass on the database each customers fall into different categories.
<script type="text/javascript">
$(document).ready(function() {
$('#customer_id').change(function() {
var customer_id = $("#customer_id").val();
$.ajax({
type: "POST",
url: <?php echo base_url(). 'customer/getCustomerCat' ; ?>,
data: form_data,
success: function(cat)
{
$("#category").val(cat);
}
});
});
});
</script>
Then on my controller i have:
public function getCustomerCat(){
$id = $this->input->post('customer_id');
$category = $this->customer_model->getCustomerCat($id)[0]->category;
echo $category;
}
On my Model i have:
public function getCustomerCat($id){
$query ="SELECT category FROM customers WHERE id=$id";
$query = $this->db->query($query);
return $query->result();
}
I am not so good with ajax but expected the categoory field to be populated after selecting a customer without reloading the page.
This question already has answers here:
jQuery Ajax POST example with PHP
(17 answers)
Closed 8 years ago.
I developed a php form which initiates a database request to fetch data depending on a drop down choice.
PHP Form:
<form method="get" action="<?php echo $url = basename($_SERVER['PHP_SELF']); ?>">
<select name="town" onchange='this.form.submit()'>
<?php $result= mysql_query('Query'); ?>
<option value="x" selected>Select Choice</option>
<?php while($row= mysql_fetch_assoc($result)) { ?>
<option value="<?php echo htmlspecialchars($row['town']);?>" >
<?php echo htmlspecialchars($row['town']); ?>
</option>
<?php } ?>
<input type="hidden" name="action" value="submit" /><br>
</select>
</form>
Form action:
<?php
if(isset($_GET["action"])) {
$var1= $wpdb->get_results("Query");
$var2= $wpdb->get_results("Query");
Content to show once executed }
?>
How can I make the form fetch the Data using AJAX not to stay refreshing the whole page continuously but only the form part?
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<form id="form_id" action="<?php echo $url = basename($_SERVER['PHP_SELF']); ?>" method="post">
<select id="town" name="town" onchange="send_to_server()">
<?php $result= mysql_query("Query"); ?>
<option value="x" selected>Select Choice</option>
<?php while($row= mysql_fetch_assoc($result)){ ?>
<option value="<?php echo htmlspecialchars($row['town']); ?>">
<?php echo htmlspecialchars($row['town']); ?>
</option>
<?php } ?>
<input type="hidden" name="action" value="submit" /><br>
</select>
</form>
<script type='text/javascript'>
/* attach a submit handler to the form */
function send_to_server(){
var value = $("#town").val();
/* get some values from elements on the page: */
var $form = $("#form_id"); var url = $form.attr('action');
/* Send the data using post */
var posting = $.post( url, { option_value: $("#town").val() } );
posting.done(function( data ) {
alert('success');
});
}
</script>
</body>
</html>
The above does exactly what you want. Check it in localhost