with the code I have I can echo:<a id='test'>
and it displays what was typed, however I need to make<a id='test'> into a variable so I can use it for mysql query's etc. is this possible at all?
<script type="text/javascript">
function email(){
var input = document.getElementById('input').value;
document.getElementById('text').innerHTML = input;
}
</script>
<html>
<p><input type='text' id='input' value='' />
<input type='button' onclick='email()' value='submit'/></p>
</html>
<?php
$info="<a id='text'>"//make this a variable;
echo $info;
?>
using AJAX with jQuery will help you make a simple call to the server, without page reloading. Then you can send any variables to a PHP file that will store them in your database.
Related
In same page i set the jquery value while click the button. i want pass the value to php variable in same page without form submitting.
<input type="button" name="next" class="next btn btn-primary" value="Proceed To Checkout Page" />
Jquery
$(".next").click(function(){
<?php $var1="1";?>
}
While check the php value
<?php if(isset($var1)){
echo $var1;
}else{
echo "NULL";
}?>
Every time time i got the null value. Where i getting mistake.
Ps: I cant able to send the ajax call to get the value
You simply cannot do that, you need to understand the difference between client/server side programming, you cannot assign Javascript value to PHP variable, yea but you can assign PHP value to your javascript.
You can use cookies to achieve this.
In Javascript:
<script type="text/javascript">
document.cookie = "var1=1";
</script>
And in PHP
<?php
$phpVar = $_COOKIE['var1'];
echo $phpVar;
?>
If at all you want to send php value to php page using jquery and ajax, then first of all, create input text with type hidden. put your php value in it. and when you click then get that value from that input type hidden in jquery and then pass it to whichever page you want.
Do some thing like this.
$(".next").click(function (){
var php_val=$("#php_val").val();//it is getting value from hidden filed whose id is 'php_val'.
//make ajax call with this as follows
$.post("a.php",{php_val:php_val},function(data){
//a.php is page name where u want to send php value.
})
});
<?php $var=1; ?>
<input type="hidden" id="php_val" value="<?php echo $var; ?>"/>
I hope this answers your questions
It will help you ,
<script>
$(document).ready(function() {
$(".next").click(function() {
<?php echo $var = 1 ; ?>
});
});
</script>
<?php
if (!empty($var)) {
echo $var;
}
?>
<script type="text/javascript">
function changeText2(){
var userInput = document.getElementById('userInput').value;
document.getElementById('boldStuff2').innerHTML = userInput;
return confirm('Hello!');
//return true;
}
</script>
<form method="get">
<p>Welcome to the site <b id='boldStuff2'>dude</b> </p>
<input type='text' id='userInput' value='Enter Text Here' />
<input type='button' OnClientClick="return changeText2()" value='Change Text'/>
</form>
Is this possible in PHP? I tried it but I get an error. Please suggest a solution.
Not exactly.
PHP doesn't have as complicated a template engine as ASP.NET.
You can simply use the HTML onclick attribute directly. There is no need to use a differently named one on the client because there isn't a server side attribute with the same name getting in the way.
Best practise, however, is to write unobtrusive JS and bind your event handlers programatically.
I have ajax code which take textfiled variable. it alert the value, but when I want to get in php, it gives me an error of Undefined index
JavaScript Code
<SCRIPT>
function go(UserID) {
var name= encodeURI(document.getElementById('name').value);
var design= encodeURI(document.getElementById('design').value);
http.open('get', 'testAjax.php?design='+design+'&name = '+name);
http.send(null);
}
</SCRIPT>
HTML Code
<INPUT ID='name' type='text'>
<INPUT ID='design' type='hidden' value="<?php echo $UserID; ?>">
Link
Php Code
if(isset($_GET['process'])) {
echo $_GET['design'];
echo $_GET['name'];
}
please let me know how to solve it.
You have an anchor tag on which you have an onclick event. Onclick event when fired will send an ajax request to testAjax.php? url.
However since you haven't prevented default behaviour of clicking an anchor tag, as soon as this ajax request is sent, page will redirect to ?process and where ofcourse you will have nothing to receive as $_GET parameters.
Assuming you have your ajax going right, add return false; at the end of your go() function. Also since on server side you have a clause like if(isset($_GET['process'])) { , you should also update your ajax url to something like: testAjax.php?process
Ok, a couple of things I spotted that maybe causing this issue.
Try:
<script>
function go(UserID) {
var name= encodeURI(document.getElementById('name').value);
var design= encodeURI(document.getElementById('design').value);
http.open('get', 'testAjax.php?process&design='+design+'&name='+name);
http.send(null);
}
</script>
<input id='name' type='text' />
<input id='design' type='hidden' value="<?=$UserID; ?>" />
Link
As you can see, we remove the html variable "process" and make it a part of the ajax call.
Basically what I'm trying to do is to DYNAMICALLY APPEND elements in a form and save their values to MySQL using PHP so far I've started adding but I don't know how to post their values.. Here is what I've done so far:
<html>
<head>
<script type="text/javascript" src="jquery-1.3.2.min.js" ></script>
<script>
$('document').ready(function(){
$('#save').live('click',function(e){
/*
this should be the function that gets and post all elements in php
*/
//maybe the use of $.post or .load() or ajax
});
$('#add').click(function(){
$("<br><input type='text' class='do'name='do'>").appendTo('form');
});
});
</script></head>
<body>
<form> //this was the form that im appending
<input type='text' class='do' name="do"><br>
<input type='text' class='do' name="do"><br>
<input type='text' class='do' name="do"><br>
</form>
<input type='button' id='add' value='[+]'>
<input type='button' id='save' value='go2'><br>
<div id='res'></div>
</body>
</html>
and i want to save all values in database using php so just for example here is the php
<?php
//
mysql_query("INSERT <blablabla HERE>");
?>
please help me with this stuff... thanks..
I think a simple snippet of code can answer this for you:
HTML:
<form method="post">
<input type="submit" id="btn_submit">
</form>
JQuery:
$("#btn_submit").mousedown(function() {
$("form").append("<input name='foo' value='bar'>");
});
PHP:
$myValue = $_POST["foo"];
mysql_query("insert into myTable (foo), ($myValue)");
On every add click, you need to make an ajax call to a page, where (the ajax-called page) the mysql-insert would be performed.
Consider using Jquery.ajax
Instead of trying to append the form while saving the information to a database, why not just pull the list from the database (Holding all significant values), then reload using AJAX after the 'appending', which will programmatically be just adding the value to the end of the database. Voila! Problem solved.
Please refer the jquery form plugin .http://jquery.malsup.com/form/
Here form submitted using ajax , so it will not refresh the form and also you will get all the values posted as what php does
change name of the form text boxes 'do' to do[]
$('#save').live('click',function(e){
/*
this should be the function that gets and post all elements in php
*/
//maybe the use of $.post or .load() or ajax
});
$('#add').click(function(){
$("<br><input type='text' class='do' name='do[]'>").appendTo('form');
});
<form> //this was the form that im appending
**<input type='text' class='do' name="do[]"><br>
<input type='text' class='do' name="do[]"><br>
<input type='text' class='do' name="do[]"><br>**
</form>
<input type='button' id='add' value='[+]'>
<input type='button' id='save' value='go2'><br>
I'm attempting to make it so that when you click a button, it'll add new fields to the page. I know that onclick can only take JS functions so I decided to try my luck making a JS script. At first I had tried to do
<html>
<head><title>multiple line test</title><head>
<body>
<script type="text/javascript">
var texters='';
var num=0;
function newInput(nomnom)
{
texters='';
num=nomnom+1;
for (var i=0; i<nomnom; i++)
{
texters+='<p>\
Please specify a file, or a set of files:<br>\
<input type="file" size="40">\
</p>\
<p>\
Caption : <br> \
<input type="text" size="30">\
</p>';\
}
document.write(texters+'<div>\
<input type="submit" value="Send">\
</div>\
</form>\
<br>' + '<input type="button" value="new entry" onclick="newInput(num)">' . '</body></html>');
}
newInput(num);
</script>
</body>
</html>
but that didn't work. So I tried instead to add a little bit of php being that I know it better. I tried this :
<html>
<head><title>multiple line test</title><head>
<body>
<script type="text/javascript">
var texters='';
var num=0;
function newInput(nomnom)
{
document.write(<?php
tex='';
for (var i=0; i<=(nomnom); i++)
{
tex.='<p>
Please specify a file, or a set of files:<br>
<input type="file" size="40">
</p>
<p>
Caption : <br>
<input type="text" size="30">
</p>';
}
echo tex . '<div>
<input type="submit" value="Send">
</div>
</form>
<br>' . '<input type="button" value="new entry" onclick="newInput(num)">' . '</body></html>';
?>);
}
newInput(num);
</script>
</body>
</html>
But I know that won't work because I can't get the variable I'm using for the number of fields to use out of the JS and into the PHP. Is there any way I could force JS to put that number in $_POST so I can retrieve it without having to make another form? Or is there a better way to do what I'm trying to do?
You can set a hidden input field on the form, and have the Javascript populate that.
You should try using jquery http://jquery.com/ or a simialr scripting library as it will make manipulating the DOM much easier. Using jquery you can create an onClick function that will do what you want in a few lines of code:
var onClickAddInput = function()
{
$('div#your_div_id').append('<input type="text" size="30" />');
}
If you need to add more input boxes you could loop the append statement and give the individual input boxes ids that equal the loop number.
As commented PHP is for serverside, javascript for clientside. html is the ui elements which either can create. If you need something done on the server, do it in PHP, if it needs to be done on the client, do it in javascript. Here is a sample of javascript for you.
function newInput(nomnom)
{
var tex='';
for (var i=0; i<=(nomnom); i++)
{
tex+='<p>Please specify a file, or a set of files:';
tex+='<br/><input type="file" size="40"></p>';
tex+='<p>Caption :';
tex+='<br/><input type="text" size="30"></p>';
}
document.getElementById('form_id').innerHTML += tex;
}
when this function is called, it will create a number of new inputs and add them to the form with the id "form_id".