8 Replies - 2305 Views - Last Post: 21 May 2012 - 08:31 PM

#1 smaboud  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 22-April 12

passing a variable from index.php to get_order.php using javascript

Posted 21 May 2012 - 01:49 AM

Hi I have a form a text box in it + a button to submit the entered value for a search query using an external PHP page,

I want to use javascript read the entered textbox.value and pass it to my php file, but it seems nothing is being passed.

when i run the php it self and store a number into the $q variable the php code works but when i pass the value nothing will be passed, please help.

pages are in admin/index.php for the form and admin/get_order.php for the search query.

Note the script is in the index.php <head>

here is the code

<script type="text/javascript">
 var xmlHttp;

function showOrder()
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  } 

xmlHttp=new XMLHttpRequest();
var str=documnent.list.order_IDsearch.value();
var url="admin/get_order.php";
url=url+"?v="+str;

xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send();
}

</script>




get_order.php

<?php
$q=$_POST["q"];
//$_POST['order_IDsearch'];
$con = mysql_connect('localhost', 'root', '');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("userinfo", $con);
         

$sql = "UPDATE order_detail SET freelancer_st = 'h' where order_id='".$q."'  ";
//WHERE order_id = '".$a."'";
//"SELECT * FROM order_detail WHERE order_id = '".$q."'";
$result = mysql_query($sql);


?>



Is This A Good Question/Topic? 0
  • +

Replies To: passing a variable from index.php to get_order.php using javascript

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3554
  • View blog
  • Posts: 10,332
  • Joined: 08-June 10

Re: passing a variable from index.php to get_order.php using javascript

Posted 21 May 2012 - 02:07 AM

there are 2 errors on line #28.

on line #32, the function stateChanged is not defined.

This post has been edited by Dormilich: 21 May 2012 - 02:08 AM

Was This Post Helpful? 0
  • +
  • -

#3 smaboud  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 22-April 12

Re: passing a variable from index.php to get_order.php using javascript

Posted 21 May 2012 - 02:18 AM

View PostDormilich, on 21 May 2012 - 02:07 AM, said:

there are 2 errors on line #28.

on line #32, the function stateChanged is not defined.

What is the right way , the correct syntax I'm new in Javascript, appreciated. ;)
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3554
  • View blog
  • Posts: 10,332
  • Joined: 08-June 10

Re: passing a variable from index.php to get_order.php using javascript

Posted 21 May 2012 - 02:20 AM

what kind of variable is documnent?
Was This Post Helpful? 0
  • +
  • -

#5 smaboud  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 22-April 12

Re: passing a variable from index.php to get_order.php using javascript

Posted 21 May 2012 - 02:49 AM

View PostDormilich, on 21 May 2012 - 02:20 AM, said:

what kind of variable is documnent?

It's a 4 digits number enter by user into the textbox
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3554
  • View blog
  • Posts: 10,332
  • Joined: 08-June 10

Re: passing a variable from index.php to get_order.php using javascript

Posted 21 May 2012 - 02:53 AM

er, no. documnent is a typo that is supposed to read document.

you should read about how Javascript interacts with the website (W3C wiki*) otherwise you’ll run into trouble more often than not.




* - once they fix the error

This post has been edited by Dormilich: 21 May 2012 - 07:08 AM

Was This Post Helpful? 0
  • +
  • -

#7 smaboud  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 22-April 12

Re: passing a variable from index.php to get_order.php using javascript

Posted 21 May 2012 - 07:06 AM

Hi want to store what user input into the textbox into my javascript var to be passed to my external PHP page,

I can pass the variable if i just define a value like mySite= 22 but not from what user enters into the text box.

Please help me to get access to the texbox.value

enter code here

    <form method="post" action="" onsubmit="submitFun(this)">
    <input type="text" name="order_IDsearch" id="order_IDsearch"         onblur="javascript:setmysite(this);">
       
     <input type="submit" />

      </form> 




and here is the Javascript


enter code here


    <script type="text/javascript">


    var mySite = '';
    function setmysite(v1) {

    var parent = document.getElementById('list');
    var element = parent.GetElementsByTagName('order_IDsearch')[0];

    mySite = element;


    }

    function submitFun(f1) {

    t = './get_order.php?s=' + mySite;
    t = encodeURI (t);
    f1.action = t;
    f1.submit();
    return true;
     }

This post has been edited by Dormilich: 21 May 2012 - 07:09 AM
Reason for edit:: please use [code] [/code] tags when posting code

Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3554
  • View blog
  • Posts: 10,332
  • Joined: 08-June 10

Re: passing a variable from index.php to get_order.php using javascript

Posted 21 May 2012 - 07:16 AM

that seems like shooting in your foot ...
<form method="get" action="get_order.php">
  <input type="text" name="s" id="order_IDsearch">
  <input type="submit">
</form>

gives you intentionally the same result. (despite <order_IDsearch> not being a valid HTML element)

besides that, there are some typos in your code and a Javascript Input element object prints as "[object HTMLInputElement]"
Was This Post Helpful? 0
  • +
  • -

#9 smaboud  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 119
  • Joined: 22-April 12

Re: passing a variable from index.php to get_order.php using javascript

Posted 21 May 2012 - 08:31 PM

View PostDormilich, on 21 May 2012 - 07:16 AM, said:

that seems like shooting in your foot ...
<form method="get" action="get_order.php">
  <input type="text" name="s" id="order_IDsearch">
  <input type="submit">
</form>

gives you intentionally the same result. (despite <order_IDsearch> not being a valid HTML element)

besides that, there are some typos in your code and a Javascript Input element object prints as "[object HTMLInputElement]"

Yes you are right I have changed it already, I also put the var inside the 2nd function. It works now. Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1