Javascript does not print the name of the button received from php? - javascript

The below given php code generates button based on different names received from the database. I am then using the PostUpdate(this) javascript method to get the value i.e the name of the button but it prints as blank ? I am expecting an output like for eg the name is button 1 it should alert button 1.
while ($row = mysqli_fetch_array($result))
{
$message = $row['Timings'];
$message = substr($message, 0, -2).":".substr($message, -2);
echo "<button class=\"ui-btn ui-btn-b\" onclick=\"PostUpdate(this);\" >" . $message . "</button>";
}
}
.
function PostUpdate(str)
{
alert("Called a Button");
alert(str.value);
}

The value being passed into your PostUpdate is not a string, it's a DOM element.
If you want that element's contents, you can use innerHTML to get them as a string:
function PostUpdate(button)
{
alert("Called a Button");
alert(button.innerHTML);
}
If you want the value, you can use value, but note that your button doesn't have a value (more accurately, its value is ""). You can give it a (different) value by specifying a value attribute for it, e.g., adding value="the value" in the opening <button> tag.

Related

Get the value of each value in input tag using onclick in javascript

I have a loop of button and input tags
while($row = mysqli_fetch_array($notifqry))
{
extract($row);
if($type_of_notif == '1')
{
echo '<button class="dropdown-item" onclick="notifview()">New Announcement!</button>';
echo '<input type="text" id="notifid" name="notifid" value="'.$notifid.'">';
}
}
and in my javascript
function notifview() {
var notifid = $('#notifid').val();
alert(notifid);
}
however, if I click the different button/links I only get the first row id only like this even i click the button w/ value of 3 or 4
id's need to unique for every input field. Also, if multiple input fields are created dynamically you will need to use event delegation to determine which button was clicked and then correspondingly extract that value.

Javascript, innerHTML <option> numeric value changed to string with escape and double quotes

I have a group of options created by a javascript function in a select block. I make the html string then assign it to the innerHTML of of the select block, but (as debug shows) the value of the options gets changed from integer to string with escapes and double quotes and worse, when I try to access the select value in POST I get null.
Does anybody have an idea about what's happening?
Thanks!!!
options = "<option value=64>2. TWO</option>\n"
options = options + "<option value=65>3. Three</option>\n"
document.getElementById('list').innerHTML = options;
<?php
if ($_POST['action']=="GO"){
// I want to use the value here
echo $_POST['list']
}
?>
<form action = "<?php echo $_SERVER['REQUEST_URI'];?>" method = "POST" id="userform">
<select id = "list"></select><br>
<input type = "submit" name = "action" value = "GO" />
</form>

2-step dynamical echo of a php in innerHTML

step 1: I have a form where input fields will be generated dynamically by innerHTML.
var d = document.getElementById("d1p_1");
d.innerHTML += "<input class='add' name='field_" + i + "' type='text'>";
step 2: now I would like to echo a php variable of each value to each dynamically generated input field. Something like:
d.innerHTML += "<input class='add' name='field_" + i + "' type='text' value='"<?php echo $field_" + i + "; ?>" + "'>
How can I archieve that?
Thanks in advance.
To give further information:
This is for a form where an user can add fields, depending from how many fields he needs and will be adding. Now it could be that an user adds more fields than he usually needs and enters data somewhere between field_1 and field_280. To catch the field_x where he entered data I need to echo the value of that input field.
$field_1 = value of field_1 if given;
...
$field_280 = value of field_280 if given;
The innerHTML will add the input fields dynamically by a counter for i. So I dont know what input will be given on which field. Thats why I need to generate the PHP echo part dynamical as well. Like:
<?php echo $field_" + i + "; ?>
The whole process:
form -> contains first just one input field (user will make some inputs) -> by clicking a button there will be added further input fields (1 click 1 field and user does not need to fill in data before he can add another fields) -> now imagine that a user will add 3 fields and has given input on first and third input field -> name="field_1" and name="field_3" ->
the name of each input field is generated by i++ -> the value is empty otherwise the form will be posted -> now the user will submit the form.
this means the value to echo would be $field_3 = (isset($_POST['field_3'])) ? $_POST['field_3']; : ''; -> this variable exist for all i so each variable is set in the php part before BUT to catch the right input name="field_i" with $field_i and to echo this match is the problem.
So by clicking submit the page would be reloaded and instead of only just one input field like from before now would be there 2 input fields. first would be name="field_1" and the second would be name="field_3" because user has left out input name="field_2" before. So name="field_3" needs to echo the right value depending from a dynamically generated name="field_"+ i +"what means that when the name tag is generated dynamically the php tag needs also to be generated dynamically to match each other.
i is a JavaScript variable so including it in a php declaration is giving you problems
You may implement your string concatenation out of the php code as follows
<?php
$field_="stavo";
?>
<script type="text/javascript">
var i=10;
var d = document.getElementById("d1p_1");
d.innerHTML+= "<input class='add' name='field_" + i + "' type='text'>";
d.innerHTML += "<input class='add' name='field_" + i + "' type='text' value='<?php echo $field_; ?>"+i+"'>";
</script>
Of course you must be having this in your Html
<div id="d1p_1"></div>

passing parameter from java script on input html form

I have a problem with passing javascript variable as value on input hidden type on html form.
For more details :
public function exportToPdf1($headerText=""){
// serialize the grid object into the session
$_SESSION[$this->viewPaneId."-pdf"] = serialize($this);
$pfdExport .= ' // create the export to pdf button
$("#'.$this->getViewPaneId().'").prepend("<div id=\"pdfExport\" style=\"float:right; border: none; cursor:pointer;\"><img src=\"images/stock_save_pdf.png\"> </div>");';
$pfdExport.=' // onClick function
var printToPdf = false;
var selectedRowId;
$("#pdfExport").click(function(){
selectedRowId = $("#'.$this->getViewPaneId().'input[name=\'rowSelectionRadio\']:checked").val();
if(selectedRowId){
if(confirm("Are you sure to print this object ?")){
printToPdf = true;
}
}else{
printToPdf = false;
alert("Please select an element from the table first.");
}
// create a temporarly form, in order to POST the data
$("<form id=\"pdf-form\" method=\"post\" action=\"index.php?c=gridToPdf\"><input type=\"hidden\" name=\"gridObjId\" value=\"'.$this->viewPaneId.'\"></form>").appendTo("#rightcolumn");
$("<input type=\"hidden\" name=\"headerText\" value=\"'.$headerText.'\">").appendTo("#pdf-form");
$("<input type=\"hidden\" name=\"act\" value=\"exportObject\">").appendTo("#pdf-form");
$("<input type=\"hidden\" name=\"rId\" value=\"'.selectedRowId.'\" >").appendTo("#pdf-form");
// submit the form and remove it
$("#pdf-form").submit().remove();
}
});';
Always rId gets as value string "selectedRowId", not the value of the selectedRowId var.
have any anyone any idea how to handle this problem?
well, selectedRowId doesn't seem to be defined and selectedRowId is also no valid variable in php, thats why the input field has the value selectedRowId as php thinks its a string rather than a variable.
/edit-> okey, I see that selectedRowId is a javascript variable and not a php variable. So you will need to to use "+" for concatination rather than "." and

javascript: create dynamic checkbox from dropdown menu

I have a dropdown menu which get the values dynamically from an array with a foreach loop. I know that the javascript "getElementById" need a unique key. The problem is that my unique key is a combination of "service_select" and "$value2". So that every service can be more than one.
The only unique key I have from the dropdown elements is the variable $value.
<?php
echo'<select id="service_name" name="service_select">';
foreach($array_name_new as $key=>$value)
{
echo'<option value="'.$value.'">'.$value.'</option>';
}
echo'</select>';
echo'<p>Parameter: <input name=\"$value2\" value=\"$value2\'/></p>';
?>
For each selected value in the dropbox I want a "checkbox" with the dropbox selection as name and value. I need although a seperate textfield with "$value2" as value.
I have already found this thread (How to create list of checkboxes dynamically with javascript), but I'm a newbe to javascript and don't understand the code completely.
What does the if clause in function "function populate()"? Is
this for generating the checkboxes?
Where has the codepart in the answer be added into the original code?
According to the mentioned thread I tried to modify my code like this:
<?PHP
.
.
.
echo'<select id="service" name="service_select" onchange="add_service(this.id,$_POST['service_select'],$value2)">';
foreach($array_command_name_new as $key=>$value)
{
echo'<option value="'.$value.'">'.$value.'</option>';
}
echo'</select>';
$key2 = array_search($value,$command_in_hostfile[0]);
$value2 = $command_in_hostfile[1][$key2];
$id2 = compact("$_POST['service_select']", "value2");
<script type="text/javascript">
function add_service($id2, $_POST['service_select'], $value2)
{
foreach($array_command_name_new as $key=>$value)
{
var elementid = docuemnt.getElementById($id2);
var checkbox = document.createElement('id');
checkbox.type = "checked";
checkbox.name = "$_POST['service_select']";
checkbox.value = "$_POST['service_select']";
checkbox.id = "$id";
var label = document.createElement('$_POST['service_select']')
label.htmlFor = "id";
label.appendChild(document.createTextNode('$_POST['service_select']');
container.appendChild(checkbox);
container.appendChild(label);
}
echo'<p>Parameter: <input id="parameter" name=\"$value2\" value=\"$value2\' onclick="addService('value_parameter')" /> </p>';
var s1 = document.getElementById($id2);
}
</script>
.
.
.
?>
I would be pleased if anyone can help me.
I'm afraid the solution will be a bit more complex... you need to add inputs to your form for every value the user selects...
Something like this:
[Option a] Parameter: [__________] [X]
[Option b] Parameter: [__________] [X]
[Please select... ][v]
Every time a user selects a new option you must add a new line to allow the parameter to be entered.
You will then need a [X] button in each line so the user can remove unwanted entries.
This is Javascript intensive :)

Categories