I know this will require the use of AJAX but I don't know where to start with it.
<td class="dataTableContent" valign="top">
<div>
<input id="<?php echo " update_products[ " . $orders_products_id . "] [backorder_date] "; ?>" name="<?php echo " update_products[ " .
$orders_products_id . "][backorder_date] "; ?>" size="10" ?>onChange="backorderDate('
<?php echo $orders_products_id; ?>')" value='
<?php echo tep_date_short($order->products[$i]['backorder_date']); ?>'>
</div>
</td>
So what I have here is an input box that requires users to input a back order date if there is one for their product. Now if you notice I have an event that activates a function onChange for this input box.
function backorderDate(pid)
{
<?php
$prod_id = "<script language='JavaScript'>pid;</script>";
echo $prod_id;
$query_send = tep_db_query("SELECT backorder_date from pos_products WHERE
orders_products_id= '$prod_id' ORDER BY backorder_date DESC");
$final = tep_db_fetch_array($query_send);
?>
alert("There has been a change to the Back Order Date and this may
change the In Stock checkbox.");
alert("<?php echo $final; ?>");
}
Then as you can see above this is the function that is being called. I pass the variable from the input box to the function and I named it 'pid'. Then here is where the problem begins. I need that variable's content in order to call the correct info in a query. Clearly what I have there is wrong and it most likely needs AJAX. Can anyone push me in the right direction?
I'm not a PHP wiz, but I'm guessing that the PHP in your JS function gets parsed and doesn't exist when the function is actually run. Therefore, the script tag is unnecessary.
function backorderDate(pid) {
<?php echo $prod_id ?> = pid;
You can't do SQL from javascript like this. It's not only impossible, it's a huge security risk. Assuming the PHP outputs the input correctly, change your javascript function to look something like this:
function backorderDate(pid)
{
$.ajax ({
url: "backend.php",
data: pid,
success: function(data) {
///do whataever you want here
}
})
}
Then create a PHP file called backend.php where you do your query, process the results, and send back data to your jQuery function.
EDIT:
Looking over this again, I'm not sure what you're trying to do here:
<input id="<?php echo " update_products[ " . $orders_products_id . "] [backorder_date] "; ?>" name="<?php echo " update_products[ " .
$orders_products_id . "][backorder_date] "; ?>" size="10" ?>onChange="backorderDate('
<?php echo $orders_products_id; ?>')" value='
<?php echo tep_date_short($order->products[$i]['backorder_date']); ?>'>
You may mean something like this? :
<input id="<?php echo $update_products[orders_products_id][backorder_date]; ?>"
name="<?php echo $update_products[orders_products_id][backorder_date]; ?>"
size="10"
onChange="backorderDate('<?php echo $orders_products_id; ?>')"
value='<?php echo tep_date_short($order->products[$i]['backorder_date']); ?>'>
Without seeing more of your code, I can't really decipher where all these variables are coming from.
Related
I'm having trouble setting the value of the dropdown after the form input refreshes the page. I can get the value but no matter what I try I'm unable to set the dropdown after the page refreshes. I've tried a number of different ideas I've found online too. I've tried both JavaScript and PHP solutions and all I can do is get the value but not set it. This is the code I have so far, which returns the drop down ID, I just need to know how to use it. I appreciate any help, thanks!
<?php
$pdo = new PDO('mysql:host=localhost; dbname=db', 'root', 'password')'
$sql = "SELECT divid, division FROM divisions ORDER BY division ASC";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$divs = $stmt->fetchAll();
?>
<form method="post">
<select id="divi" name="divisions">
<?php foreach($divs as $div): ?>
<option value="<?= $div['divid'];?>"><?= $div['division']; ?></option>
<?php endforeach; ?>
</select>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['divisions'])){
$selected = $_POST['divisions'];
echo 'Selected: " . $selected;
} else {
echo 'Select division.';
}
}
?>
It's not very clear to me what you really want, however it looks like you want to select default:
<select id="divi" name="divisions">
<?php foreach($divs as $div): ?>
<option <?php echo ("mycondition ex: 'id == 1'") ? "selected" : NULL ?> value="<?= $div['divid'];?>"><?= $div['division']; ?></option>
<?php endforeach; ?>
</select>
You are simply missing any code that sets 'selected="selected"' in the HTML for the select field.
Also, your code is very hard to read so I've cleaned up the loop a little bit.
<form method="post">
<?php
echo '<select id="divid" name="divid">';
foreach ($divs as $div) {
$selected = '';
if (isset ($_POST['divid']) && ($_POST['divid'] == $div['divid'])) {
$selected = 'selected="selected"';
}
echo '<option value="' . $div['divid'] . '" ' . $selected . '>' . $div['division'] . '</option>';
}
echo '</select>';
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>
let's say I have a code like that:
<div id="stuff<? echo $dynID; ?>" class="bla">
<form id="myform<? echo $dynID; ?> " action="bla.php">
<input name="iname<? echo $dynID; ?>" value="<? echo $row[1]; ?>">
</form>
</div>
<div id="stuff<? echo $dynID; ?>" class="bla">
<form id="myform<? echo $dynID; ?> " action="bla.php">
<input name="iname<? echo $dynID; ?>" value="<? echo $row[1]; ?>">
</form>
</div>
<div id="stuff<? echo $dynID; ?>" class="bla">
<form id="myform<? echo $dynID; ?> " action="bla.php">
<input name="iname<? echo $dynID; ?>" value="<? echo $row[1]; ?>">
</form>
</div>
Many forms. In this example 3. (It could be more or less)
How can I trigger this form (to send ist with AJAX)? This form is live-AJAX generated content and have a dynamic ID. I did not now the specific ID of the form to trigger like:
$("#myform").submit(function(event) {
How, can I handle this?
We could use some more information to help further, for example, is the text in this form (any of it) unique when compared to other forms? You can target it by basically anything and even combine these conditions. For example, if the action attribute is unique you could always do something like this (untested):
//if the form id starts with 'myform', perform a function on submit
$("form[id^='myform']").submit(function(event) {
//if that form's 'action' attribute is 'bla.php', then do something
if($('form').attr('action', 'bla.php')){
//your code here
}
});
EDIT:
Is this unique enough to snag it? I've made it rely on the div above it having an id that starts with 'stuff', that div must also have class 'bla' and the form attribute of 'action' must be 'bla.php'
$("div[id^='stuff'] form[id^='myform']").submit(function(event) {
if($(this).parent().hasClass('bla')) {
if($(this).attr('action', 'bla.php')){
//your code here
}
}
});
I am attempting to create a way to search for a user by typing their name in a text field, then changing a list below. The easiest way I saw to do this way by using a datalist but it seems that a datalist's search go off the value and not the html of the element.
Is it possible to change the search from looking at the value to the html?
Context:
<input class="mrg-btm" type="text" placeholder="Search..." list="users" />
<datalist id="users" name="formSec" required>
<?php
$get = $users->prepare("SELECT userID,userFirst,userLast FROM users");
$get->execute();
$get->store_result();
$get->bind_result($userID,$userFirst,$userLast);
while($get->fetch()) {
?>
<option value="<?php echo $userID; ?>"><?php echo $userFirst. ' ' .$userLast; ?></option>
<?php
};
$get->close();
?>
</datalist>
As you can see, I am assigning the userID to the value and not the name, I would like to be able to search for the name of the user without having to put it as the value, is this possible?
You can make up attributes in HTML5 by prefixing them with data-. So in your case you need to do it like this:
<option value="<?php echo $userID; ?>" data-userFirst="<?php echo $userFirst ?>" data-userLast="<?php echo $userLast ?>"><?php echo $userFirst. ' ' .$userLast; ?></option>
Now you can use javascript to get the vlues of the attributes.
I have an HTML label and I would like to change its text at runtime. I know I can use innerHTML, which works, but why does it just just append my message in front of the already-set label text.
PHP:
<label for="hp_display" id="addToHP_<?php echo $x; ?>"></label><?php echo $addTo; ?>homepage: </label><input type="checkbox" id="hp_display_<?php echo $x; ?>" <?php echo $check; ?> onclick="addToHome('<?php echo md5($product['product_id']); ?>','<?php echo $add; ?>','<?php echo $x; ?>')" />
JAVASCRIPT:
function addToHome(id,a,n){
$.ajax({
url:'func/addToSlider.php',
type:'POST',
data:{type:a,id:id},
beforeSend: function(){
document.getElementById('addToHP_'+n).innerHTML = '';
document.getElementById('addToHP_'+n).innerHTML = 'Updating';
},
success:function(e){
if(e === '1'){
document.getElementById('addToHP_'+n).innerHTML = '';
if(a === 'remove'){
document.getElementById('addToHP_'+n).innerHTML = 'Add to homepage';
}else{
document.getElementById('addToHP_'+n).innerHTML = 'Remove from homepage';
}
}else{
alert('There has been a server changing your file, please try again');
}
}
});
}
It looks complicated, but surely innerHTML or JQuery's .html('') should replace the text instead of appending the text. I could just refresh page on success of the ajax but I think changing the label text is more user-friendly.
Your html markup is wrong, you have two closing label elements
<label for="hp_display" id="addToHP_<?php echo $x; ?>"></label><?php echo $addTo; ?>homepage: </label>
^^^^^^^^ ^^^^^^^
without all the php mark up it is
<label>X</label>Y</label>
You have too many </label>
<label for="hp_display" id="addToHP_<?php echo $x; ?>"></label><?php echo $addTo; ?>homepage: </label>
so while the label looks like it is being appended it is actually getting filled for the first time. remove the first </label> and problem solved.
should be:
<label for="hp_display" id="addToHP_<?php echo $x; ?>"><?php echo $addTo; ?>homepage: </label>