Setting value of hidden element in php - php

I have a hidden field in form. On form submit I call a database function to add the inputs to database and return result of query. I wish to set the value returned to hidden field. But when I am not able to assign a value to it
<form name="frmAddBook" id="frmAddBook" method="post">
<input type="hidden" name="actionResult" id="actionResult">
PHP
echo '
<script type="text/javascript">
alert("hello");
document.getElementById("actionResult").value=1;
alert("2222222");
alert(document.frmAddBook.getElementById("actionResult").value);
</script>';

you need to specify the value of that hidden input field, so if you wanted to do this in PHP it'd look like this:
<input type="hidden" name="actionResult" id="actionResult" value="<?php echo $result; ?>"/>

Related

Get form attribute value with PHP

Is there a way to get submitted form's attribute value using PHP, when the submit button is clicked?
I have a form which has attribute data-id: someid, I need to get this value.
There is no direct way to get the data attribute value in PHP.
You have to first get data-attribute, set this value to hidden field and then use PHP to get it.
If You Wants Data id on submit
$(document).ready(function(){
$('#formid').on('submit', function(){
var id = $(this).data("id")
alert(id);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="" method="POST" name="myformName" id="formid" data-id="someid">
<input type="submit" name="submit">
</form>
You can achieve this in php with two ways:
1. Either pass your 'someid' in input with hidden type
<input type="hidden" name="data-id" value="someid" />
2. Just pass your 'someid' in action attribute
<form name="" action="filename.php?data-id=someid">

how can we pass input type value in to an variable with out submit or click

i know the question doesn't looks good but am not getting any idea because am new to here.my question is can we store the value of an input type in to an variable.here in this case
<input type="text" name="statustochange">
having an value now i want to store that value in to an variable with out submit or click
i got the value in to
name="statustochange"
from this way
<script type="text/javascript">
$('.modal').modal({
ready: function(modal, trigger) {
modal.find('input[name="statustochange"]').val(trigger.data('status'))
}
});
</script>
Try this:
<input type="text" name="statustochange" value="<?php echo $str; ?>">
Into a js variable? Use a onChange event!
Into a php variable? Use a js onChange event to trigger a postback in Ajax (invisible to the user)
Edit: More realavent link

How do I use get method to input values to form field with PHP

I have the following code snippet of my fields I have in my form:
<input id="username" type="text" placeholder="E-mail Address" value="" name="username"></input>
This is what I have in my input field. Is there anybody who will tell me how to get input values to the field using a url? e.g https://mysite?username=ken and it will show "ken" in the input field?
In your HTML, add the input field like this:
<input type="text" name="username" value="<?php echo htmlspecialchars($_GET['username']); ?>" />
Basically, the value attribute of the text field needs to be set to:
<?php echo $_GET['username']; ?>
The code right above this is how you would output a get variable in php whether you are putting it in a text field or not.
To access get variables, always use:
$_GET['variable_name'];
Then you can assign it to variables or pass it as a function parameter.
**However, I strongly do not recommend passing sensitive information like usernames and passwords through GET variables. **
First off, users could change the URL hence changing the variable. They could also accidentally share the URL with someone and that could give someone else access to their account. I would recommend that you create a cookie on their machine that is set to a random ID, and then in a MySQL database, associate that ID with a username so that you know the user can't accidentally share their account or change their username through the URL.
You can do it like this, make an isset in your php form input that can catch your ken variable from GET post, never forget the method="get" inside the form tag and if you are planning on submitting on the same page you can use action="<?php echo $_SERVER['PHP_SELF']; ?>" inside your form tag.. hope this helps, here is your code.. ^_^
<form id="form" name="form" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<p>Input</p>
<div>
<input type="text" name="nameoffield" id="nameoffield" value="<?php if(isset($_GET['ken'])){echo $_GET['ken'];} ?>"> <br />
</div>
</fieldset>
<div>
<button type="submit" value="Submit" name="submit">
Submit
</button>
</div>
</form>
The <input> tag and other fields of form must be in a <form>tag.
<form action = "https://mysite" method = "get">
<input id = "username" type = "text" placeholder = "E-mail Address" name = "username" value = "<?php echo $_GET['username']; ?>" />
</form>
In the above code, form tag specifies that the method of submission is 'GET' and the action that will be taken on submission is URL to which your form data will be submitted and processed.
Now assuming that your form is in the same URL to which you are submitting your form, you will get the GET value in the same page (or URL), so in the input text field set the value which is obtained by GET method and use it.
All the GET key-value pairs are stored in an associative array $_GET from which you can access the value of a given key by using that as the index of the array.
e.g. Key is username in this case, so to get the value of the username, $_GET['username'] was used.

Passing ID value in autocomplete in Scriptaculous

I have the scriptaculous autocompleter working. It pops up the data and I can select it, but it does not transfer the ID number in the form I'm using. I just cannot get the ID number. I can show the ID number on the screen using the following command:
alert (li.id);
I have used the following but no luck:
document.getElementById('inv_id').value=li.id;
and
$("#inv_id").val(li.id);
How do I pass that ID number to a value in the form? Here is the form code...
<form action="page2.php" method="post" name="Add_Form">
<input type="hidden" name="inv_id" value="0">
<input type="text" id="autocomplete" name="autocomplete_parameter"/>
<div id="autocomplete_choices" class="autocomplete"></div>
</form>
<script type="text/javascript">
new Ajax.Autocompleter("autocomplete", "autocomplete_choices", "ajax_inv.php", {
afterUpdateElement : getSelectedId
});
function getSelectedId(text, li) {
alert (li.id);
document.getElementById('inv_id').value=li.id;
or
$("#inv_id").val(li.id);
}
</script>
Where is the inv_id field? Make it a hidden input if you don't have it existing already, then document.getElementById('inv_id').value=li.id; should work

Trouble passing slider value to php mail script

I want to email the value of the slider along with some things they entered into my form to my php email script. This is how the form looks that will pass data to the POST method of mailer.php:
<form method="POST" action="mailer.php">
Name:
<input type="text" name="name" size="19"><br>
<br>
E-Mail:
<input type="text" name="email" size="19"><br>
<input type="hidden" name="slider_value" size="19" value=document.getElementById('sliderValue').value><br>
<br>
<input type="submit" value="Submit" name="submit">
</form>
document.getElementById('sliderValue').value is the call I make to get the value of the slider but when I pass this to the value of my hidden input slider_value I get back "document.getElementById('sliderValue').value" in the email that is sent by my php script. How would I pass the value instead?
document.getElementById is a Javascript function. It probably won't do what you want it to unless you put it in a place where Javascript is accepted.
One way to pass the slider value is by putting the sliderValue element in the form that you are submitting.
If that is not an option, you can set the value of the slider_value element with Javascript. Either set an onsubmit attribute to the form like this:
<form method="POST" action="mailer.php"
onsubmit="this.slider_value.value = document.getElementById('sliderValue').value">
or add an event listener to do it:
var form = document.forms[0]; //this may need to be changed depending on your form
function addSliderValue() {
this.slider_value.value = document.getElementById('sliderValue').value;
}
if (form.addEventListener) { //most browsers
form.addEventListener("submit", addSliderValue, false);
} else if (form.attachEvent) { //IE
form.onsubmit = addSliderValue;
}
Bind a function to the sliders "onSlide/onChange/whatever it's called" event that will update the hidden input field with the slider value. Also set the input value to the initial slider value on slider init. That should do the trick.

Categories