I've got the following javascript function that reloads the form once a select option is chosen. I'm using this to pull data from database into each additional select dropdown based on which option they chose in the first, second, etc.
function reloadForm(form)
{
var cat=form.cat.options[form.cat.options.selectedIndex].value;
if (cat.length > 0) { var cat1 = '&cat=' + cat; }
var subcat1=form.subcat.options[form.subcat.options.selectedIndex].value;
if (subcat1.length > 0) { var subcat2 = '&subcat=' + subcat1; }
self.location = 'index.php<?php echo $webvars; ?>' + cat1;
}
But here is my problem, once I add the subcat variable in, the script stops working. I don't understand why?
Below is my html/php code so far.... just in case someone needs it.
$product_sku = $db1q->query("SELECT sku FROM Inventory_SKU WHERE id = $id");
if ($product_sku->num_rows > 0) {
while ($row = $product_sku->fetch_assoc()) { echo '<h2>CONNECTING '. $row['sku'] .' TO PRODUCT</h2>'; }
}
echo '<div class="margin20">';
echo '<form name="connectsku" action="index.php'. $webvars .'" method="POST">';
echo '<input type="hidden" name="connect_sku" value="1">';
echo '<select name="cat" onchange="reloadForm(this.form)"><option value="">SELECT CATEGORY</option>';
foreach ($db1q->query("SELECT c.id, c.name,s.name as section FROM Product_Categories as c JOIN Product_Sections as s ON c.section = s.id") as $catdd) {
echo '<option value="'. $catdd['id'] .'">'. $catdd['name'] .'</option>';
}
echo '</select>';
if ((isset($cat)) && ($cat != "")) {
echo '<select name="subcat" onchange="reloadForm(this.form)">
<option value="">Select SUBCAT</option>';
foreach ($db1q->query("SELECT id, name FROM Product_Sub_Category ") as $subcatdd) {
echo '<option value="'. $subcatdd['id'] .'">'. $subcatdd['name'] .'</option>';
}
echo "</select>";
}
if ((isset($subcat)) && ($subcat != "")) {
echo "<select name='subcat3' ><option value=''>Select one</option>";
foreach ($dbo->query($quer3) as $noticia) {
echo "<option value='$noticia[subcat2]'>$noticia[subcat2]</option>";
}
echo "</select>";
}
echo '<input type="submit" value="MOVE TO NEXT STEP">';
echo '</form>';
echo '</div>';
CONSOLE LOG
Uncaught TypeError: Cannot set property 'oninput' of null
at HTMLDocument.<anonymous> (index.php?p1=inventorymanagement&p2=ConnectSKU&id=3:65)
at c (jquery-1.10.2.min.js:4)
at Object.fireWith [as resolveWith] (jquery-1.10.2.min.js:4)
at Function.ready (jquery-1.10.2.min.js:4)
at HTMLDocument.q (jquery-1.10.2.min.js:4)
index.php?p1=inventorymanagement&p2=ConnectSKU&id=3:238 Uncaught TypeError: Cannot read property 'options' of undefined
at reloadForm (index.php?p1=inventorymanagement&p2=ConnectSKU&id=3:238)
at HTMLSelectElement.onchange (index.php?p1=inventorymanagement&p2=ConnectSKU&id=3:248)
Subcat Select field is hidden until $cat is definied by url update. Could it be because there is nothing to pull from on the page thats causing the error? Is there a line of code I can add to check to see if subcat has a valid value or exists because running the subcat portion?
Found out that it was because the select field for subcat was not being generated because I had a condition that it only generated if cat was set. I ended up always showing it and just "hide" it using html if category isnt set. That way javascript could at least detect it was there. This ended up fixing the entire problem.
Related
I have a button that is setup from database content as well as a select option that is setup from the same database. I would like to be able to click the button or choose from the dropdown to chose the option.
I have tried a lot of different options that I have found online. Currently I am able to alert the text I want to select but have been unable to change the dropdown.
PHP file
$q = "SELECT CONCAT(user_first, ' ', user_last) AS name1, user_id as id from users ";
$r = #mysqli_query($conn, $q);
// Count the number of returned rows:
$num = mysqli_num_rows($r);
if ($num > 0) { // If it ran OK, display the records.
echo '<select class="select" name="name1" id="Seltherapist" autofocus tabindex="1">';
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo '<option value="'.$row['id'].'">' . $row['name1'] . '</option>
';}
echo '</select>';
mysqli_free_result ($r);
} else {
echo '<p class="error">There are currently no registered users.</p>';
}
$num = mysqli_num_rows($r);
if ($num > 0) {
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo '<button name="name22" value="'.$row['name'].'" id="'.$row['id'].'" class="btn">' . $row['name'] . '</button>
';}
JavaScript File
$('button[name="name22"]').click(function(){
alert($(this).attr("value"));
alert($(this).attr("id"));
event.preventDefault();
return false;
});
I've not sure where you're getting the text from so lets just call that yourText.
let yourText = "someTextToMatch";
//By option text something like this.
$('#Seltherapist').find('option').each(function(i, v){
if ($(v).text() === yourText)
{
$(v).prop('selected', 'selected');
return false;
}
});
//by option value
$('#Seltherapist').val(yourText);
In my dropdown list, i put all the "pack_name(s)" the user has posted and I display it all in the list for the user to select and update. So when the user selects one and hits submit, i want to get that "value" submitted and use it for later purposes but ive been researching and only found "pre-set" values with html and the value was given using Jquery. So i wondering if its possible to basically take the "pack_name" selected and when the user hits submit, echo out the selected value.
PHP
<?php
session_start();
if(empty($_FILES) && empty($_POST) && isset($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) == 'post'){ //catch file overload error...
$postMax = ini_get('post_max_size'); //grab the size limits...
echo "<p style=\"color: #F00;\">\nPlease note files larger than {$postMax} will result in this error!</p>"; // echo out error and solutions...
return $postMax;
}
if(isset($_COOKIE['id'])){
if($_SESSION['came_from_upload'] != true){
setcookie("id", "", time() - 60*60);
$_COOKIE['id'] = "";
header("Location: developerLogin.php");
exit;
}
try{
// new php data object
$handler = new PDO('mysql:host=127.0.0.1;dbname=magicserver', 'root', '');
//ATTR_ERRMODE set to exception
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
die("There was an error connecting to the database");
}
$userid = $_SESSION['id'];
$stmt = $handler->prepare("SELECT * FROM pack_profile WHERE pack_developer_id = :userid");
$stmt->bindParam(':userid', $userid, PDO::PARAM_INT);
$stmt->execute();
echo "<select>";
while($result = $stmt->fetch()){
echo "<option>" . $result['pack_name'] ."</option>";
}
echo "</select>";
if($_SERVER['REQUEST_METHOD'] =="POST"){
$token = $_SESSION['token'];
}
}
?>
You need to give the select element a name attribute, and give each option element a value attribute.
For example:
echo "<select name=\"pack\">";
while($result = $stmt->fetch()){
echo "<option value=\"" . $result['pack_name'] . "\">" . $result['pack_name'] ."</option>";
}
echo "</select>";
Of course you should be escaping anything which could contain &, < or " with something like htmlspecialchars().
I'm using a select list function below to make a select list that have 5 values inside of it. Values 2-5 works fine when I select them, and they print out their values on the page when I select them, but value 1 does not print out no matter what. I cannot figure out what I did wrong or how to fix it. Please take a look at my code:
index.php
function limit($count,$location) {
echo "<form method = 'POST' action = '$location'>";
echo "<select name = 'value' onchange='this.form.submit()'>";
while ($tempCount < $count) {
$tempCount++;
echo "<option value='$tempCount'>$tempCount</option>";
}
echo "</select>";
echo "</form>";
}
limit(5,"index.php")
$value = $_POST['value'];
echo $value;
Add one first option to the < select >, and, check if $_POST['value'] exists. Next is your code with both changes pointed by commented arrows (//<=====) :
<?php
function limit($count,$location) {
echo "<form method = 'POST' action = '$location'>";
echo "<select name = 'value' onchange='this.form.submit()'>" .
"<option>Select an option</option>"; // <===========================
while ($tempCount < $count) {
$tempCount++;
echo "<option value='$tempCount'>$tempCount</option>";
}
echo "</select>";
echo "</form>";
}
limit(5,"xyz.php");
if ( isSet( $_POST['value'] ) ) // <===========================
{ $value = $_POST['value'];
echo $value;
}
?>
The option "Select an option" will let the user to choose option 1.
If you don't want to see "Select an option", the other solution is to make the chosen option selected, for example, if the user chooses "3", when the page reloads the option "3" will be selected, and the user will be able to choose option "1" :
<?php
function limit($count,$location) {
echo "<form method = 'POST' action = '$location'>";
echo "<select name = 'value' onchange='this.form.submit()'>";
while ($tempCount < $count) {
$tempCount++;
// MAKE THE CURRENT OPTION SELECTED IF IT WAS CHOSEN BEFORE. <==========
if ( isSet( $_POST['value'] ) && // IF 'value' EXISTS, AND
( $_POST['value'] == $tempCount ) ) // IF 'value' == CURRENT NUMBER
$selected = "selected";
else $selected = "";
echo "<option $selected value='$tempCount'>$tempCount</option>";
}
echo "</select>";
echo "</form>";
}
limit(5,"xyz.php");
if ( isSet( $_POST['value'] ) ) // <===========================
{ $value = $_POST['value'];
echo $value;
}
?>
I have this following code to handle selection in select form:
<script>
$(document).ready(function(){
$('#pilStock').change(function() {
var pilih_stock = $("#pilStock option:selected").text();
var hargaZ = $("#hargaX").text();
$.post("countme.php",{ hrg: hargaZ, dstock: pilih_stock},
function(data) {
document.getElementById("hasilhitung_pilihstock").innerHTML=data;
document.getElementById("jmlqty").innerHTML=pilih_stock;
}
);
});
});
</script>
Then I need to declare PHP variable, as:
$stock=isset($_POST['pilStock']);
$stock=strip_tags($stock);
After that, I need to save the data in $stock in the database but it gave me nothing.
This is the INSERT code I used:
$data = $xvg->prepare("INSERT INTO mytable (prod_stock) VALUES (:prod_stock)
$data->execute(array(':stock'=>$stock));
I tried it many times and makes me dizzy... please.
UPDATED:
Here's the SELECT OPTION which is related to $pilStock:
$stockz variable is from SELECT * FROM other_table (then the result must be selected by user and saved it in the database)
<?php
$stockz=$row['stock'];
if ($stockz=='1'){
echo "<select name='select' class='form-control' id='pilStock'>";
echo "<option value='1'>1</option>";
echo "</select>";
}
elseif ($stockz=='2'){
echo "<select name='select' class='form-control' id='pilStock'>";
echo "<option value='1'>1</option>";
echo "<option value='2'>2</option>";
echo "</select>";
}
?>
SELECT Script:
<?php
if ($stockz=='2'){
echo "<select name='select' class='form-control' id='pilStock'>";
echo "<option value='1'>1</option>";
echo "<option value='2'>2</option>";
echo "</select>";
}
?>
Previous:
$pilStock=$_POST['pilStock'];
Note: That didnot work because Javascript is being used by other PHP file in the same dir.
After:
$pilStock=$_POST['select'];
Note: This works when the name of the SELECT OPTION is called.
#JordanForeman.
--- please donot vote down if you feel this answer is crazy. just let me know how I should be. :p
Fatal error: Call to undefined function get_field_id() in E:\wamp\www\customfield\wp-content\plugins\custom-widget2\update.php on line 6.
i am using ajax to create second dropdown. my ajax page name is update.php but in response give me this error Call to undefined function get_field_id().
i making a plugin for widgit
echo $ret='<select id="'.get_field_id('test2').';" name="">';
$categories= get_categories();
$categoryid="";
$categoryid=5;
$taxonomy_names = get_object_taxonomies($status);
$taxonomy_name =$taxonomy_names[0];
$tax_terms = get_terms($taxonomy_name);
foreach ($tax_terms as $tax_term):
option = '<option value="' .$tax_term->name . '" ';
if ($cat->cat_ID == $categoryid) :
$option .= ' selected = "selected" ';
endif;
$option .= '">';
$option .= $tax_term->name;
$option .= '</option>';
$ret.=$option;
echo $option;
endforeach;
$ret.="</select>'";
update.php
please help me......
That function is out of scope.
To fix add
require('/the/path/to/your/wp-blog-header.php');
at the top of your file