1

Good day. I am still learning Javascript. And I have a function that is called by this button:

<input class='inputButton' type='button' onclick="submit_data('EDIT')">

The function of submit_data looks like this:

function submit_data(mode){
var txtName = $('#txtName').val();
}

Where txtName is the id of a textbox. But suppose I have a variable called $xDate how do I call it?

var xDate =  $('$xDate').val();

like this?

This is the whole submit_data:

function submit_data(mode){
var txtName = $('#txtName').val();
varUrl = "/includes/action.php";
varData = "MODE=" + mode + "&TXT_NAME=" + txtName;

     $.ajax({
          url: varUrl,
          type: 'POST',
          data: varData,
          success: function(result) {
              alert(result);
              if(result = 'success'){
                redirect_to_parent();
              } 
              else
              {
              }
          }
     });
}

And the action.php looks like this:

<?php
include_once("/includes/functions.php");

$_GETVARS = (($_GET <0) ? $_GET : $_POST);

$mode = $_GETVARS['MODE'];
switch ($mode){    
    case 'EDIT': 
break;

    default:
        print_r($_GETVARS);
        break;
}

?>

In short. I need my variable to be accessible in action php. Possibly be included in my $_GETVARS

Sorry for the confusion. $xDate is a variable. I cannot get it from textbox and exist only as a variable.

aozora
  • 423
  • 3
  • 13

3 Answers3

0

If the element your want to get an element to is linked to an id

$('#' + $xDate).val();

or, if it's a class

$('.' + $xDate).val();
Swaraj Giri
  • 4,007
  • 2
  • 27
  • 44
0

You can try something like this.

<input class='inputtext' type='text'id="$xDate"/>

<input class='inputButton' type='button' onclick="submit_data('$xDate')"/>

function submit_data(mode){
    var txtName = $('#'+mode).val();
}
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
-1

your supposed to pass javascript variable to jquery as

 $('#' + $xDate).val();
PavanAsTechie
  • 322
  • 3
  • 14