call js inside php not worked - javascript

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.

Related

Javascript: How to print a value from a $_SESSION array?

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 });

Passing current page URL via hidden input field within form

I'm trying to figure out how to pass the URL of a current page via a hidden field so that I can redirect back to that page after the form's input has been handled. I've tried using javascript:location.href, however it looks as though it'll pass that as a literal string.
<input type="url" id="location" name="location" value="javascript:location.href" hidden />
When viewing the page source, I can see that the value of this input box is "javascript:location.href" rather than the page's URL. Any ideas here? Thanks in advance!
You can access the element in Javascript and change the value there
document.getElementById('location').value = location.href;
Example:
http://jsfiddle.net/6zxD5/
I don't think it works that way. You could just use the onload callback to insert it when the page is completely loaded:
<body onload="document.getElementById('location').value = document.location.href">
If your document containing the form is already a PHP file , you can do
$yourPath = $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
And in your html you would do
echo '<input type="url" id="location" name="location" value="'.$yourPath.'" hidden />';
You can use JavaScript to set the value of the hidden field:
document.getElementById('location').value = window.location.href;
Example:
https://jsfiddle.net/moogs/mhjh0je3/
Since you also tagged PHP, here's the PHP version:
<input type="hidden" id="location" name="location" value="<?php echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>" />

Get Input Value on the same page with PHP?

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
}

Javascript Form Input Retrieval

Why is it that in a form that contains a Text Box and a Submit Button, I can Alert what has been typed in the text box by the user, but can't print it on the page? What am I doing wrong?
Here's the code
<form name="Serb" action="" method="get">
<input name="Name" type="text" size="15" maxlength="20" />
<input name="Join" type="submit" value="Join" onClick="serb(this.form)" />
<script type="text/javascript">
function serb(form){
var x = document.Serb.Name.value;
alert(x); \\this alerts
document.write(x); \\this should print on page
}
</script>
For some reason, the alert works fine and displays exactly what the user typed in the username box after pressing 'Join'. However, it won't print the information on the page. Why is that?
It does work. The value in the textbox is printed on the page.
BUT:
\\ do not mean anything in Javascript. Comments begin with //. This is most likely the reason why you are not seeing the value being written
document.write replaces whatever is in the HTML page with its argument. (If it is called after the document is loaded). So unless you are trying to learn Javascript this is not a very good idea.
Actually it is not a very good idea to use it even when learning Javascript, unless you are trying to learn how document.write works.
There are flexible (and better) ways to manipulate the content of a page, starting from the humble getElementById to complex DOM manipulation
It is not a good idea to use document.write() after the page has been loaded/parsed. At that point, it will overwrite the page HTML with new content. document.write() is generally used while the page is being loaded to insert content at a particular point into the page as it's being loaded.
If you want to put the value into some item on the page, then you need to use appropriate DOM methods for that, putting the value into an input field, setting the innerHTML on a div, etc...
You can read about document.write here: https://developer.mozilla.org/en/document.write.
Here's an example of fetching the value from the field and putting it in another object on the page without using document.write(): http://jsfiddle.net/jfriend00/dU8Sr/.
HTML:
<form name="Serb" action="" method="get">
<input name="Name" type="text" size="15" maxlength="20" />
<input name="Join" type="button" value="Join" onClick="serb(this.form)" />
</form>
<br>
<br>Output: <span id="output"></span>
Javascript:
function serb(form) {
var x = document.Serb.Name.value;
document.getElementById("output").innerHTML = x;
}

Passing javascript variable to html textbox

i need help on html form.
I got a javascript variable, and I am trying to pass the variable to a html form texbox. I want to display the variable on the textbox dynamically. but i do not know how to pass the variable to the html form and call the variable?
var test;
<INPUT TYPE="TEXT" NAME="lg" VALUE="" SIZE="25" MAXLENGTH="50" disabled="disabled"><BR><BR>
How do i pass test to html form and change its value?
Thanks
Pass the variable to the form element like this
your form element
<input type="text" id="mytext">
javascript
var test = "Hello";
document.getElementById("mytext").value = test;//Now you get the js variable inside your form element
<form name="input" action="some.php" method="post">
<input type="text" name="user" id="mytext">
<input type="submit" value="Submit">
</form>
<script>
var w = someValue;
document.getElementById("mytext").value = w;
</script>
//php on some.php page
echo $_POST['user'];
instead of
document.getElementById("txtBillingGroupName").value = groupName;
You can use
$("#txtBillingGroupName").val(groupName);
instead of groupName you can pass string value like "Group1"
This was a problem for me, too. One reason for doing this (in my case) was that I needed to convert a client-side event (a javascript variable being modified) to a server-side variable (for that variable to be used in php). Hence populating a form with a javascript variable (eg a sessionStorage key/value) and converting it to a $_POST variable.
<form name='formName'>
<input name='inputName'>
</form>
<script>
document.formName.inputName.value=var
</script>
You could also use to localStorage feature of HTML5 to store your test value and then access it at any other point in your website by using the localStorage.getItem() method. To see how this works you should look at the w3schools explanation or the explanation from the Opera Developer website. Hope this helps.
document.getElementById("txtBillingGroupName").value = groupName;

Categories