Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
iam trying to post a file to database,i first created an empty form and then tried to add input using javascript,but i dont know how to link the newly created input to form and then submit hence the php shows no result.
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.min.js"></script>
<title>image upload</title>
<script>
function sub()
{
var form=document.getElementById('nform');
var fil1 = document.createElement("input");
fil1.setAttribute("type","file");
fil1.setAttribute("name", "image1");
fil1.setAttribute("value", document.getElementById('one1').value);
form.submit();
return
}
</script>
</head>
<body>
<form id="nform" action="http://mydomainname.net/somepost.php"method="post">
</form>
<input type='file' id="one1"/>
<input type="button" onclick="sub();" value="Submit">
</body>
</html>
PHP code:
<?php
$con = mysql_connect("mydomainname.net","489583788","39853953");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("489583788", $con);
echo $_FILES["image1"]["name"];
echo $_FILES["image2"]["name"];
echo $_FILES["image3"]["name"];
?>
Your input is out side of the form element, it wont be passed to action page. So you need to append it to the form and then submit. Use Node.appendChild().
function sub(){
var form=document.getElementById('nform');
var fil1 = document.createElement("input");
fil1.setAttribute("type","file");
fil1.setAttribute("name", "image1");
fil1.setAttribute("value", document.getElementById('one1').value);
fil1.setAttribute("style","display:none;");
form.appendChild(fil1);
form.submit();
return
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
<!Doctype html>
<html>
<title>JavaScript Tutorial</title>
<body>
<script language = "javascript">
var cleanCities = ["Cheyenne ","Santa Fe ","Tucson ","Great Falls ","Honolulu"];
var cityToCheck = "Tucson";
if(cityToCheck === cleanCities[i]) {
alert("You are clean");
}
</script>
</body>
</html>
I cannot find the errors
it's a javascript code
<!Doctype html>
<html>
<title>JavaScript Tutorial</title>
<body>
<script language = "javascript">
var cleanCities = ["Cheyenne ","Santa Fe ","Tucson ","Great Falls ","Honolulu"];
var cityToCheck = "Tucson";
if(cityToCheck === cleanCities[i]) {
alert("You are clean");
}
</script>
</body>
</html>
It says there is some problem in line 14
Where is your for loop for which you're an index i? If its removed intentionally then Tucson in your array has a space in the end. Use trim method.
Also language = "javascript" is wrong, its usually type="text/javascript"
The code should be like below:
enter code here
var cleanCities = ["Cheyenne ","Santa Fe ","Tucson ","Great Falls ","Honolulu"];
var cityToCheck = "Tucson";
for(var i=0; i<cleanCities.length; i++) {
if(cityToCheck === cleanCities[i].trim()){
alert('you are clean');
}
}
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 5 years ago.
<form name="from" id="from">
<input type="text" name="x" onkeyup="Get_Value()"/>
<input type="text" name="y" onkeyup="Get_Value()"/>
</form>
<script type="text/javascript">
function Get_Value() {
var x = Number(document.from.x.value);
var y= Number(document.from.y.value);
var total=x+y;
document.getElementById("total").innerHTML =total;
}
</script>
<?php
$total='<div id="total"></div>';
echo $total;
?>
When I execute this and put x=10, y=20 then it shows me 30 to the browser. Its fine.
When I save it the DB then it storing <div id="total">30</div>. But I need the 30 only.
Though the title of the question is a common one, I didn't found my answer.
What should I do now?
If you want send data from PHP that would be accessable in JS, then you can use
<script type="text/javascript">
function Get_Value(x, y) {
var total=x+y;
document.getElementById("total").innerHTML =total;
}
Get_Value(<?php echo 10; ?>, <?php echo 20; ?>);
</script>
If you have, any variable , then replace echo 10 with your choice.
<html>
<head>
<script type="text/javascript">
function showdivv(el, idclicked) {
var iddd = idclicked;
var display = document.getElementById(el).style.display;
if(display == "none")
document.getElementById(el).style.display = 'block';
else
document.getElementById(el).style.display = 'none';
}
</script>
<?php $showw = "<script>document.write(iddd)</script>"; ?>
</head>
<body>
<div id="myDiv" style="display: none;">ID Selected: <?php echo $showw; ?></div>
<?php $variable = 4; ?>
<button type="button" onclick="showdivv('myDiv', '<?php echo $variable; ?>')">Show / Hide</button>
</body>
I'm trying to make a way when a person presses the button pass the variable, in this case ID, to JavaScript and then show the hidden div in PHP. It does not work, can someone help me? THX
If you're okay with the page reloading, you can simply do
window.location.href('php_script_that_needs_your_input.php?id=input_id_from_js');
If not, I absolutely recommend using JQuery as it makes Ajax queries a breeze.
Inside the <head> tags:
<script src='http://code.jquery.com/jquery-2.2.0.min.js'></script>
Upload the script to your own server for production purposes, obviously.
in the <body>, where you want the results from the PHP script to appear (skip this if you don't want output from PHP):
<div id="phpResult"><!--content to be set by ajax--></div>
and put this JS directly below this <div>:
function ajax_on_button_press(resultDiv, id) {
var phpUrl = "php_script.php?id="+id+"&other_variable=cheese&more_info=rats";
$(resultDiv).load(phpUrl);
}
I have used example values here, but you can easily use variables from JavaScript like this:
var other_variable=$('#otherVariableInput').val(); //gets input from a textbox with the html id otherVariableInput
var phpUrl = "php_script.php?id="+id+"&other_variable="+other_variable+"&more_info=rats";
To start the process, you need a button that runs this script. Change your button markup to
<button type="button" onclick="ajax_on_button_press('#phpResult', '<?php echo $variable; ?>')">Show / Hide</button>
If I have understood you correctly, that should solve your problem.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi below is my html and javascrpt which is having two text boxes and one submit button and I am getting the form values and storing the values in variable and appending to a url. Now my requirement is to execute the Url but my below code is opening the url, is there any way to execute the url without opening it.
<HTML>
<HEAD>
<script type="text/javascript">
function validate()
{
var name = document.getElementById("name").value;
var value = document.getElementById("value").value;
var url ="http://example.net.net/ftp/Ftp? name="+ name+"&value="+value;
window.open(url);
}
</script>
</HEAD>
<BODY>
<form name="Form1" method="post" action="" onsubmit="return validate();">
<input type="text" size="15" id="name" value=""/>
<input type="text" size="15" id="value" value=""/>
<input type ="submit" value ="submit" >
</form>
</BODY>
</HTML>
I have tried window.location =url; also bt not able to execute it, please suggest me.
Thanks
It seems like you want to do a GET request.
I would use AJAX for this:
$.ajax({
type: 'GET',
url: '/my/url',
success: function(jqXhr) {
console.log(jqXhr.responseText);
},
error: function() {
}
})
If you're not comfortable with JQuery you could do this (IE8+):
request = new XMLHttpRequest
request.open('GET', '/my/url', true)
request.send()
request.onload = function() {
console.log(this.response);
}
request.onerror = function() {
}
I think you are trying to open a url in new window right?
Try window.open
window.open(url,'_blank');
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a code in HTML using Spring notation.
The code is:
<c:if test="${testCSV==true}">
<img src="images/icons/edit.png" width="25" height="25" />
</c:if>
I want to check this condition in jQuery, So the condition checks dynamically.
please help me.
Add this code in html where you want to edit button to show
<a id="edit" href="#" onclick="editCSV(${command.id})" rel="tooltip-top">
<img src = "images/icons/edit.png" width = "25" height = "25" />
</a>
js
if (test == true) {
$('#edit').show();
} else {
$('#edit').hide();
}
pass true
<script type="text/javascript">
var test =pagetype('<%=${testCSV}%>');
if (test == true) {
$('#edit').show();
} else {
$('#edit').hide();
}
</script>
You can use this:
<script type="text/javascript">
var x = ${testCSV};
// some other code
</script>
The browser will see it as:
<script type="text/javascript">
var x = true; // or false
// some other code
</script>
Then, do whatever you like with it.