Get variable from Ajax to Php - php

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.

Related

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

Assign jquery value to php variable

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

make a javascript variable into php variable without refreshing page

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.

Get value of input box, without a form?

Does anyone know how I can get the value of an input box, without having a form? I want a submit button, but instead of submitting a form, I want it to change data in a MySQL database. Something like this maybe?
$img1="WHAT DO I PUT HERE?"
$idx=1
$sql="INSERT INTO games SET img1='$img1' WHERE id=$idx";
$result=mysql_query($sql);
Could I use that code on a "onclick" event? The input box's name and id is "img1".
If you don't want to submit a form, the only two other ways of accomplishing this are to click a link with the data as query parameters in the url, or use AJAX to send the data to the server in the background.
update:
Javascript, as usual. You'd put a link or button somewhere on the page with "Send to Server" or whatever for the text. The script would pull your information from the input fields, and then send it on to the server via an AJAX call. Something along these lines (note that I'm using Mootools for all this, as it makes life much easier than having to do the remote calls yourself):
function clickHandler() {
var img1 = $$("input[name='img1']")[0].value;
var r = new Request.JSON({
'url: 'http://yourserver.example.com/script.php',
'method': 'post',
'onComplete': function(success) { alert('AJAX call status: ' + (success ? 'succeeded': 'failed!'); },
'onFailure': function() { alert('Could not contact server'); },
'data': 'img1=' + img1
}).send();
}
and on the server you'd have something like:
<?php
$img1 = mysql_real_escape_string($_POST['img1']);
$idx=1;
$sql="INSERT INTO games SET img1='$img1' WHERE id=$idx";
$result=mysql_query($sql);
echo (($result !== FALSE) ? 1 : 0);
You'd probably want something more complicated than this, but this is the basis of an AJAX application. Some client-side javascript that makes requests, and a script on the server that handles them and returns any data/errors as needed.
Don't know if your completely against using a form or just might not know how to keep it hidden.
You can create a hidden form that submits the info with the click of a button.
<form name="hidden-form" action="youraction.php" method="post">
<input type="hidden" name="submitme" value="I get submitted">
<input type="hidden" name="submitmetoo" value="I get submitted">
<input type="hidden" name="submitmeaswell" value="I get submitted">
<input type="hidden" name="dontleavemeout" value="I get submitted">
<input name="submit" type="submit" value="SUBMIT" />
</form>
However anyone that looks at your html will be able to see this
I guess this is what you looking for.
this will only work if your html and php are on the same file...
<html>
<head>
</head>
<body>
<input type='text' id='user' placeholder='user'>
</body>
</html>
<?php
$val = "
<script>
document.write(document.querySelector('#user').value);
</script>
";
echo $val;
?>

strange javascript form behaviour

I am generating the following html form with this php:
echo "<form name=\"userForm\">
Username:
<input type=\"text\" name=\"username\" />
<br />
First name:
<input type=\"text\" name=\"firstname\" />
<br />
Last name:
<input type=\"text\" name=\"lastname\" />
<br />
<input name=\"Submit\" type=\"submit\" value=\"Update\" onclick=\"submitUserInfo();return false\"/>
</form>";
Which is handled by submitUserInfo, which is here:
function submitUserInfo() {
url = "edit_user.php?cmd=submitinfo&username="+document.userForm.username.value+"&firstname="+document.userForm.firstname.value+"&lastname="+document.userForm.lastname.value;
var xmlHttp=GetXmlHttpObject();
if(xmlHttp.responseText == 'true') {
alert(url);
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
//updateByUser(username);
}
}
I clearly have url defined as beginning with
edit_user.php?cmd=submitinfo&username
however when pressing the submit button, it tries to send the url as
edit_user.php?username=
and I cannot figure out why. I have used the above technique succusfully with other forms on my site, and can not find any reason that cmd=submitinfo& is being excluded.
Use a tool like Firebug to determine if the browser is sending it without cmd=submitinfo& or if it's being lost on the server side.
Change:
url = "edit_user.php?cmd=submitinfo&username="+document.userForm.username.value+"&firstname="+document.userForm.firstname.value+"&lastname="+document.userForm.lastname.value;
to:
var url = "edit_user.php?cmd=submitinfo&username="+document.userForm.username.value+"&firstname="+document.userForm.firstname.value+"&lastname="+document.userForm.lastname.value;
You must declare your url variable before you try to use it.
Also, avoid echoing straight HTML to the page. The way you've done it is going to be an absolute pain to debug, update, and stylize.
Frameworks like jQuery is not really needed for such simple tasks, although I would suggest you taking a look if you plan to implement more JavaScript on your site.
Your code, using jQuery, would look like this:
$("#<id of form goes here>").submit(function ()
{
$.ajax({
data: {
"username": $("#<id of username-input goes here>").val(),
"firstname": $("#<id of firstname-input goes here>").val(),
"lastname": $("#<id of lastname-input goes here>").val()
},
url: "edit_user.php"
});
return false;
});
Looks a bit nicer, I think :)
Also; using frameworks - not jQuery in particular - takes care of browser work-arounds.. so you won't have to.. take care of 'em, that is :)
I think you need to change the type of the button from "submit" to "button":
<input name=\"Submit\" type=\"button\" value=\"Update\" onclick=\"submitUserInfo();return false\"/>

Categories