Echoing out variables in a function - php

I am trying to returning a string like this.
$col="<td><input ".$name." type='".$field['1']."' ".$id." ".$class." value='".eval( '?> '.$row[$field["2"]] .'<?php ')."'".$size." ".$maxlength." ".$disabled." ".$readonly." /></td>";
I need
value='".eval( '?>'.$row[$field["2"]] .'<?php ')."'
to be evaluated after the string is returned into the page so i can use my sql call
$row = $core->getRowById("email_users", $user->userid,"userid");
I include the row here.
<input type="text" readonly="readonly" disabled="disabled" size="55" value="<?= $row['username'];?>" class="inputbox" name="username">
It would then evaluate this
$row['username'];
How can I go about doing this? Do I use eval?

If I get your point:
You can't do that, PHP doesn't work in a multipass way. Your eval would be a plain string in output. You have to use either POST or GET back to this page, check whether $_POST or $_GET exists, and act accordingly.

Related

PHP Retrieve Values from Dynamically Named Form Input Fields

I've setup a form with a table which includes rows from a database. I've used a variable to represent the database record ID as the name for one of the rows like this:
<td><input type="text" name= "<?php echo $recordID; ?>" class="form-control" placeholder="User Notes"></td>
which is working fine and ends up appearing like this in the browser:
<td><input type="text" name= "TSL1406" class="form-control" placeholder="User Notes"></td>
I'm having trouble retrieving the value when the form is submitted. I would normally use something like this:
$input = $_POST['userNotes']
which I've updated to handle the dynamic naming of the inputs like this:
$input = $_POST['.$recordID.'];
but this is returning an empty variable and I can't work out the correct syntax to retrieve the input from the $_POST array with a dynamically named input field?
single quote " ' " means literal, it won't display your variable name just literally .$recordID.. Try using double quotation instead.
$input = $_POST[".$recordID."];
Only $input = $_POST[$recordID]; without quotes?
or you use arrays like:
<input type="text" name= "form[<?php echo $recordID; ?>]" class="form-control" placeholder="User Notes"></td>
and save everything at once
$input = $_POST['form'];
Why don't you just simply use $_POST[$recordID]? However there is a better way to get POST variable using filter_input(INPUT_POST, $recordID)

Multi-word $GET variable problemin PHP

I am currently writing some search engine, where this page is retrieving some _GET variables from a previous page. This is working as intended.
Now I am using those variables as default value in a POST form. However, for some reason, only the first word for each of them is showing up. The form code is as follows:
<form action = "insert.php" method = 'POST'>
<Place name <input type="text" name="name" size = "30" value= <?php echo $_GET['name']; ?> />
Note that when echoing $_GET['name'] anywhere else in the page, everything is fine. Multiple words show up as expected, but when I use it as a text box default value, only the first word shows up on the textbox.
At first, I thought it had something to do with the way those $_GET variables are sent in the URL so I tried this:
$fullname = array();
$fullname = explode("%20", $_GET['name']);
$aaa = implode (' ',$fullname);
...
Place name <input type="text" name="name" size = "30" value= <?php echo $aaa; ?> />
but the result is still the same. If I echo it anywhere else in the page I get the full string, but if it's inside the form only the first word shows up.
What am I missing here?
The value attribute of the input tag needs to be in quotes:
<input type="text" name="name" size = "30" value="<?php echo $_GET['name']; ?>" />"
Otherwise, if $_GET['name'] contains spaces you'll end up with something like: value=John Smith. That will be understood as value=John with an invalid Smith attribute floating around.
Also, consider sanitizing $_GET['name'] with htmlspecialchars. Consider what would happen if $_GET['name'] was "/><script>alert(0)</script><. You'd end up embedding user-controlled code on your website, resulting in a reflected XSS.

How $_POST variable/operation is managed in PHP?

When I do something like:
foreach($_POST as $post_key => $post_value){
/* Any code here*/
}
So, something like:
$varSomething = $_POST['anything'];
$varSomethingElse = $_POST['somethingElse'];
Is it possible? When I catch a $_POST[' '], isn't that variable already consumed?
The main reason why I would do this is because after a form submission, I want to check wether some items of some type got certain value or not.
Is there aything else more appropiate?
Firstly the html code don't use variable types, for example, if you have
<input id="check" type="checkbox" />
without a established value, after that you have echo $_POST['chek'], you could think that the result would be a boolean value (false or true), but the correct result will be "on" or "off", you can coding this case. Also, if you want to know the type of your data, you can use regular expression on server side, for example:
<input type="text" id="number" value="1350" />
.....
PHP code
$data = $_POST['number'];
$regularExpression = "/^\d{1,10}$/";
if (preg_match($regularExpression, $data)) {
echo "Is numeric";
}
Good lucky.
if you don't know what is the name of element which is sending the data. the first method is ohk . but if know the name like password or username you can use second one
in html
<input type="password" name ="password" />
in php
$pass_recvd=$_POST['password'];
there is no way to check the type i.e. text/password/checkbox/select etc. you have to do it on client side BEST WAY IS USING Jquery
if you wanna check whether a variable is set or not simple check by using isset method
if( isset($_POST['someVariableName'])) {}else{}

Passing HTML input data to Javascript call

I am currently using this code:
<input type=button value='Call' class="call" onClick='voipCall("<?php echo $number_1;?>")'>
<input type=button id="callendbutton" class="hangup" value='Hangup' onClick='voipHangup()'>
which uses the variable $number_1 and passes it into a javascript function to call that number. Now I need another section which lets the user input their own number to call, but I'm not sure how to pass the information from the text input box into the function call.
Something like this:
<input type="text" tip="Enter alternate phone number" name="phonenumber" id="phonenumber" size="40" value=""/>
<input type=button value='Call' class="call" onClick='voipCall("#phonenumber")'>
<input type=button id="callendbutton4" class="hangup" value='Hangup' onClick='voipHangup()'>
Is there an easy way to do this?
Thanks for any help
You can use the id "phonenumber" to fetch the text-input node, and then get the current value from it:
var number = document.getElementById('phonenumber').value;
You can use javascript to get the text from your phonenumber input text field.
jquery:
voipCall($("#phonenumber").val())
Call that voipCall() in the voiphangup().
Use 'title' instead of 'tip'.
Use document.getElementById('phonenumber') to access the input field and get its value.
You can set a variable with default set by PHP if you want, getting the input field value if non empty, and use this variable in your JavaScript call.

How to connect php and jquery

Is it possible to use php to compute something and use jquery to fetch the computed
value and display it back to the webpage without the need to submit the form.
I'm really having trouble using client-side to compute something that is using arrays. So I'm thinking of letting php do the computation.
<input type="hidden" name=ids[] value="<?php $id; ?>">
<input type="hidden" name=qoh[] value="<?php $qtyhand; ?>">
<input type="hidden" name=dprice[] value="<?php $dsprice; ?>">
<input type="hidden" name=sprice[] value="<?php $ssprice; ?>">
<td>$qtyhand</td>";
<td><input type="text" name=qbuys[] value="<?php echo $ssprice; ?>"></td>
<td><?php $ssprice; ?></td>";
<td><input type="text" name=disc[] value=""></td>
<td><input type="text" name=subtot[] value=""></td>
And here is the php file that is supposed to be used for computing.
<?php
$ctbill=0;
foreach ($_GET['ids'] as $k => $v) {
$id = (int)$v;
$qtyhnd = (int)$_GET['qoh'][$k];
$qtytbuy = (int)$_GET['qbuys'][$k];
$left = $qtyhnd - $qtytbuy;
$sellprice=(double)$_GET['sprice'][$k];
$dealerprice=(double)$_GET['dprice'][$k];
$finalvalue=.01;
$dis=(double)$_GET['disc'][$k];
$stotal=(double)$qtytbuy * $sellprice;
$cdizval=(double)$stotal * $dis * $finalvalue;
$cdstotal=$stotal-$cdizval;
$ctbill=(double)$ctbill + $cdstotal;
$dizval=$dis * $finalvalue;
$preprof=(($sellprice * $qtytbuy)-($dealerprice*$qtytbuy)) * $dizval;
$profit=(($sellprice * $qtytbuy)-($dealerprice*$qtytbuy)) - $preprof;
?>
Can you give me an idea on how to do it.
The loading itself is often as simple as
$("#output").load("compute.php?ids[]=1111&ids[]=2222");
The complexity in your case is assembling the various URL parameters from the form fields. Usually you can pass a simple Javascript array/hash as data parameter. But JS arrays and the default jQuery .load handler don't work well with repetetive PHP-style var[] field names that you have.
Your best bet is to build a loop and do that manually I think. (But there might be plugins for that, so try Google and jQuery plugin repository.)
Yes. Look at the jquery manual for examples of how to make ajax requests and show their results:
http://api.jquery.com/jQuery.ajax/
Use ajax and set those values in the callback.
jQuery's ajax functions include $.get, $.post, $.load, $.ajax . You should probably pass your parameters with POST bc it seems you want to pass some arrays and GET is not well suited for that. That PHP file should echo out the stuff you need, and depending on its complexity you may want it to be JSON used JSONencode instead of just a plain string.
$.post('/url.php', {data1: 'literal', data2: variable, ....}, function($x){--display==});
url.php is the PHP file that handles it
second argument is encoming data
third argument is a callback function, with $x being whatever url.php echos
http://api.jquery.com/category/ajax/
http://www.visualjquery.com

Categories