• I have a textbox with some value in it. How will I transfer the value of textbox into a php variable. I have tried using the following code :
    $ipth = $_POST['textbox'].
    This way value is not being transferred.

    One more thing I would like to tell you is that value in the textbox is not entered by the user, instead the value is coming from a jquery. In this jquery when a user clicks a row in the table, the values of that rows is shown in different textboxes outside the table.

    Please guide ??

Viewing 15 replies - 1 through 15 (of 15 total)
  • How are the values being submitted to the server? is it AJAX, form submission (and POST or GET)?

    The first thing to do is some simple de-bugging. I’d add this in just below where you’re setting the value:

    echo "";
    var_export ($_POST);
    echo "";
    die ();

    That will show you every $_POST variable that was given, and will stop any further processing. That will at least show you what you have to work with. From there you can work backwards if the value isn’t being sent.

    And just as a note, the main times that I’ve seen this in my own cod,e it’s been from me getting the name of the textbox wrong, so I was trying to get a wrong value. As an example…

    <input type="text" name="my_input_area" value="My Value" />

    And PHP with the wrong reference:

    $my_value = $_POST ['my_imput_area'];

    It took me about 30 minutes to figure out that my spelling was wrong.

    Thread Starter Suneet

    (@suneetpant)

    hi, thanks for replying. Let me explain you the whole thing :
    Value in the textbox is coming using a jquery ( clicking a row of the table ) and then extracting values of that row in the respective textboxes outside the table. Let me give you the code :

    $("#tabstatusdisp tr").click(function(){
    	var td = $(selectedRow).children('td');
    	for (var i = 4; i < td.length; ++i) {
    		$('#txtqty').val(td[4].innerText);
    		$('#txtrate').val(td[5].innerText);
    		$('#oldcbval').val(td[7].innerText);
    		$('#imgpath').val(td[8].innerText);
            }
    });

    As you can see, I want to pick the value of “imgpath” and pass it into a php variable “inpvar” which will then be used within img tag.
    For your convenience code is shown below :

    <?php
      $ipth = inpvar;
      $fpth = "http://localhost/website/wp-content/themes/website/".$ipth;
    ?>

    Image tag :

    <div class="lbx1">
       <?php echo "<img src='$fpth' alt='' style='width:230px; height:200px' />"; ?>
    </div>
    <div class="c2r1">
      <input id="imgpath" name="imgpath"  type="text" value='' class="imgsty" />
    </div>

    Please help ??

    You need to actually send the value back to your PHP script, and you’re not doing that. You are only populating the textbox, but that’s where it stops.

    There’s two ways of sending the value. First, through a standard form that, and that’s the easiest way to do it. secondly, you can do it through a jQuery post using AJAX.

    I’d suggest using AJAX. It’s a bit harder to do, but it does tend ot work a lot better, and will most likely give a better experience to your visitors.

    Thread Starter Suneet

    (@suneetpant)

    sorry i forgot to mention here is the complete code :

    $(document).ready(function() {
       $("#tabstatusdisp tr").click(function(){
         var td = $(selectedRow).children('td');
         for (var i = 4; i < td.length; ++i) {
    	$('#txtqty').val(td[4].innerText);
    	$('#txtrate').val(td[5].innerText);
    	$('#oldcbval').val(td[7].innerText);
    	$('#imgpath').val(td[8].innerText);
    	var inpvar =  $('#imgpath').attr("value");
    	}
         });
    });

    You’re still not actually sending the value back to the server. You need to do that before you can save anything. go back and re-read what I posted above as it’s got all of the information that you need. 🙂

    Thread Starter Suneet

    (@suneetpant)

    I have gone through this but still not clear what needs to be done. If possible can you please provide me a small example that will help me understand how it is done.

    There’s examples on the page that I linked to. That shows how it works, and what you need to do…

    JavaScript code:

    jQuery(document).ready(function($) {
    
    var data = {
    	'action': 'my_action',
    	'whatever': 1234
    };
    
    // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
    jQuery.post(ajaxurl, data, function(response) {
    	alert('Got this from the server: ' + response);
    });
    });

    PHP code:

    add_action( 'wp_ajax_my_action', 'my_action_callback' );
    
    function my_action_callback() {
    	global $wpdb; // this is how you get access to the database
    
    	$whatever = intval( $_POST['whatever'] );
    
    	$whatever += 10;
    
            echo $whatever;
    
    	wp_die(); // this is required to terminate immediately and return a proper response
    }

    That will give you a working solution. Of course you will need to substitute yoru own values in there, so keep on reading that page, and look at the jQuery post() function page and you’ll figure it out.

    Thread Starter Suneet

    (@suneetpant)

    $(document).ready(function() {
       $("#tabstatusdisp tr").click(function(){             <-- Ajax Function
         var td = $(selectedRow).children('td');
         for (var i = 4; i < td.length; ++i) {
    	$('#txtqty').val(td[4].innerText);
    	$('#txtrate').val(td[5].innerText);
    	$('#oldcbval').val(td[7].innerText);
    	//$('#imgpath').val(td[8].innerText);
    	//var inpvar =  $('#imgpath').attr("value");
            $('#showpic').html(td[8].innerText);
    	}
         });
    });

    ok if I change the syntax to “$(‘#showpic’).html(td[8].innerText);”, then even I have to do the same thing ??. But here I am already using ajax

    Sorry but no. That’s not AJAX.

    All you are doing is setting the value on the page. You are not sending the value back to the server.

    Look at the examples that I’ve given with jQuery.post(). That’s where JAAX comes into it, and that is what you will need to do to send the value back to the server.

    Thread Starter Suneet

    (@suneetpant)

    yeah, Sorry you are right, that’s not ajax that jquery.

    Thread Starter Suneet

    (@suneetpant)

    is this the correct way :

    $("#tabstatusdisp tr").click(function(){
    	var td = $(selectedRow).children('td');
    	for (var i = 4; i < td.length; ++i) {
    		$('#txtqty').val(td[4].innerText);
    		$('#txtrate').val(td[5].innerText);
    		$('#oldcbval').val(td[7].innerText);
    		$('#imgpath').val(td[8].innerText);
    		var inpvar =  td[8].innerText;
    		//data = { imgpath:inpvar };
    	}
    	data = { imgpath:inpvar };
    	$.ajax({
    		type: 'POST',
    		url: 'ordMcheck.php',
    		data: data,
    		cache: false,
    		success: function(data) {
    			console.log(data)
    		}
    	});
    });

    Please guide ?

    That part looks OK. Then you just have to set up the PHP functions to read in the file.

    The only thing that you’d need to check is the URL that the post submits too. The WordPress AJAX URL is different to what you have there, so you’ll need to fix that before you can send the value to the right place.

    Thread Starter Suneet

    (@suneetpant)

    Here actually I don’t want other users to see what values or filename is being passed. so that’s why i have redirected to “ordMcheck.php” instead of “url”

    The URL that you pass it to doesn’t make any difference to the visibility of the data. It’s still sent as POST data, which is plain-text unless you’re woking with an SSL certifcate.

    Thread Starter Suneet

    (@suneetpant)

    showing this error :
    POST http://localhost/website/ordMcheck.php 404 (Not Found)

    code of ordMcheck.php :

    <?php var_dump($_POST); ?>
    <?php ini_set('display_errors', 1); ?>
    
    <?php session_Start(); ?>
    <?php
    	include ("dbcon.php");
    	include ("included_functions.php");
    
    	echo (imgpath);
    ?>

    Pls guide where I am wrong ??

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘how to pick value of a textbox in a php variable’ is closed to new replies.