Is there a way to have javascript create php code, to be executed when it runs on the php page? I'm trying to set the value of an input to , where div_id is a js variable, and inputs is a 2D associative array.
The problem is that it literally sets the value to "" instead of the value of the floor_type field. Everything else seems to work.
inputs_div.innerHTML += `<div id="`+div_id+`" class="dynamic_div">
<ul class="dynamic_ul">
<li><input type="text" onblur="$('.save').click();" placeholder="Flooring Type: Wood Floor, Tile, etc." name="inputs[`+div_id+`][floor_type]" maxlength="255" value="<?php echo $_SESSION['inputs']['`+div_id+`']['floor_type']; ?>"></li>
</ul>
</div>
I'm creating some html inputs dynamically with javascript, but I'm looking for a way to prevent them from getting cleared if the page is refreshed. Maybe there is a simpler way to make the inputs stay there?
My idea was to use ajax to run some php code from the onblur event of each input element, which saves the inputs[][] array into the session, so it doesn't get lost when refreshing the page.
Use a hidden field to store $_SESSION[] value and then on document ready get the value of hidden field and set to the javascript variable.
<input type="hidden" id="hdn" value="<? php echo $_SESSION['inputs'] ?>" />
$(document).ready(function()
{ var session_value = $('#hdn').attr('value'); // now use session_value variable when set the innerhtml of inputs_div });
Related
I have issue with script js inside php so i want ipnut get the somme of the value return by function js in dispaly in input with condition in php inside the input in below my script php:
function getSum(){
let NotePresence= document.getElementById("NotePresence").value;
let NoteValidation= document.getElementById("NoteValidation").value;
let NoteEvaluation= document.getElementById("NoteEvaluation").value;
let t=NotePresence+NoteValidation+NoteEvaluation;
return t;
}
calling in php:
<div class="form-group" style="display: flex">
<input readonly="" style='width: auto' type="text"
name="NoteFinale" id="NoteFinale"
class="form-control" maxlength="2"
value="<?php echo '<script type="text/javascript">getSum();</script>';?>
size="4" />
<label for=""> /80</label>
</div>
Thanks in advance
You can't put a script element inside an attribute value.
If you want to assign a value to the attribute from JS, then:
Put the script somewhere after the element in the DOM
Find the element with DOM methods (e.g. document.getElementById)
Assign the value to the element with DOM
Note, however, that since you should know the initial values for the three elements you are reading from, you should be able to eliminate JS entirely and just do this with PHP.
$NotePresence = 50;
$NoteValidation = 100;
$NoteEvaluation = 150;
<input name="NotePresence" value="<?=htmlspecialchars($NotePresence)">
<input name="NoteValidation" value="<?=htmlspecialchars($NoteValidation)">
<input name="NotePresence" value="<?=htmlspecialchars($NotePresence)">
<input name="NoteFinale" value="<?=htmlspecialchars($NotePresence + $NoteValidation = $NoteEvaluation)">
That is, assuming you want to combine them when the document loads.
If you want to do it later, then…
In JavaScript you would need to move the code into a function that you call when a suitable event (e.g. change on an input or submit on a form occurs).
In PHP you would need to move the code to the place the data is submitted to and combine the data from $_POST.
I basically have a search box, I am trying to get the value inserted and use on a separate /results.htm screen via the submit button. I've been able to get and process the results within the same page; but I am trying to do this after redirecting to a new page
/search.html
<script>
jQuery(document).ready(function() {
jQuery("#submit").click(function() {
var ZipSearch = jQuery("#Zipcode").val();
});
});
</script>
<div class="search_bar">
<form action=""><input type="text" name="Zipcode" id="Zipcode" value="" maxlength="5" placeholder="Zipcode">
<input type="button" name="submit" id="submit" value="Submit"/></form>
</div>
Want to keep input value content from /search.htm within a variable on next page, separate, /results.htm
How can I keep the user input value and use on my separate 'results' page?
Remove all the JavaScript from search.htm. There's no point in writing custom software to do things HTML has built-in.
Set the action of the form to the URL of the second page (action="/results.htm").
Change the button to a submit button (type="submit")
Read the data from the query string (location.search in JavaScript or $_GET in PHP).
I get data.my_id from an AJAX call. I want to place it inside a hidden input named my_id, but I am not able to do it. Is there a special way to succeed with this sort of input? Is it normal not to see the AJAX return in the sources of the page? Thanks and sorry for my school English.
// dat.my_id is working in an alert
$(#my_id).html(data.my_id);
<input type="hidden" name="my_id" id="my_id" value="<?= $my_id; ?>" />
You need to keep value of hidden empty initially.
<input type="hidden" name="my_id" id="my_id" value="" />
Then on ajax response you can set the value as below:
$("#my_id").val(data.my_id);
To pass a value to your input you can use
$('#my_id').val(data.my_id);
Some examples
I am trying to fetch the whole innerHtml of a DIV to a PHP variable.
For this what I have done is to put the whole of the innerHtml into a Hidden field, and then call the Hidden field into php.
My code is:
<form method="post">
<div class="container" id="content">
<h1>Header1</h1>
<p>this is the para</p>
<textarea id="address" name="address"></textarea>
</div>
<input type="hidden" id="hidhtm" name="hidhtm"/>
</form>
In my JS file I have:
document.getElementById('hidhtm').value = document.getElementById('content').innerHTML;
In my PHP I have:
$test = $_POST['hidhtm'];
echo $test;
Can anyone please help me on this.
I wanted to do the same thing, and tried your method and it works.
In my test, the Javascript function which extracts the innerHMTL and places it in the hidden field of the form is called upon the user clicking a button which enabled the "submit" button (eg. acceptance of terms and conditions) but which does not actually submit the form. Therefore the hidden field was populated before the form was submitted. I haven't tried doing the same thing "onSubmit" but I suspect it might be too late to do so then.
I found that the material which could be inserted into the hidden field included all the values inserted by the user onto the form as well as things inserted by Javascript itself. All this could be picked up from the $_POST variable for the hidden field.
I needed to do this so that the PHP server could create a new form dynamically and send it back to the user.
function add_question(){
var count=document.getElementById("nofquest").value;
var container = document.getElementById("container");
// Clear previous contents of the container
while (container.hasChildNodes())
{
container.removeChild(container.lastChild);
}
for (i=1;i
please find text boxes in center how to align
Well, first i'm new in PHP. Is there a way to get the input value from a existing input on the page on page load with php and pass it to a variable?
For example i have this input: <input type="text" name="g_id_p" id="example1" value"foo">
I want to do something like this: $got_it = $_GET['g_id_p'];
Sorry again if i wrote my code wrong, im noobie on this. Hope to someone help me.
First, would be great to know what method is the form. (GET or POST)
Then after know what type of method you could call it in PHP:
METHOD POST:
<input type="text" name="g_id_p" id="example1" value"foo">
$variable = $_POST['g_id_p'];
METHOD GET:
<input type="text" name="g_id_p" id="example1" value"foo">
$variable = $_GET['g_id_p'];
If you haven't defined a method, in html the tag for a form is:
<form>
<!-- Here goes your input and some stuff -->
</form>
Then it would be something like:
<form name="form_name" class="form_class" id="form_id" method="TheFormMethod" action="ThePageThatExecutesThisForm">
<!-- Here goes your input and some stuff -->
</form>
TheFormMethod can be post, get, delete, put.
You can't really get an associated value of an input tag within the same PHP page but what you can do is set the value of a variable beforehand.
What I mean is, create an array that will store all the values of all the input tags.
$inputValues = array();
$inputValues['g_id_p'] = 'foo';
Then when you have the tag later on just echo it from the PHP var.
<input type="text" name="g_id_p" id="example1" value="<?php echo $inputValues['g_id_p']; ?>">
As you can see, we aren't really 'getting' the value that you set but the end result is the same.
You have to check if the values is set isset(), where you want to use the variable do:
if(isset($_GET['g_id_p'])){
//your code
}