Codeigniter insert id model using javascript - javascript

How can I get the value of the productid in the php code?
function getunitstock(){
var productid = document.getElementById('product').value
<?php $prodbyid = $this->pos_model->get_productbyid(+productid+)?>
document.getElementById('unitprice').value = <?php echo $prodbyid['price'];?>
}

You can't use javascript variable in php code. Php code run's on the serverside and javascript code runs in the client side. You can't ask the browser to run php code.
<?php $prodbyid = $this->pos_model->get_productbyid(+productid+)?>'
So, this line won't get value of productid.
The only way to do what you are looking to do is either:
do a redirect to a PHP script or do an AJAX call to a PHP script

Related

How to pass javaScript values to PHP

How to pass javaScript values to PHP, my code is following
<script>
function a{
var b = a;
}
</script>
<button onclick="a(2)">Values</button>
<?php
$select = mysql_query("select * from tabl1 where id='values'"); // values comes here
echo $select;
?>
There's a lot of thing you could do.
Principal things you have to know is that javaScript run on the client side (browser), while PHP is running on the server.
Then If you want to pass a variable from your JS to your PHP you have to make a server call.
There's various way you can use in order to send variable from client to server.
As I understand from your example, it looks like your php code and your javascript on the same file. so maybe call your file another time will be enough for you.
Let's say your file's name is index.php.
<script>
function yourJavascriptFunction(id) {
window.location.href = "index.php?id=" + id;
}
</script>
Then in change your PHP code to this:
<?php
$select = mysql_query("select * from tabl1 where id='".$_GET['id']."'"); // values comes here
echo $select;
?>
$_GET will get the variable you've sent in your Js function.
Doing like this will refresh the page.
May be you don't want to refresh the page? Then look at the ajax way.
I hope it helps you

Assign js variable in PHP variable

function show_selling_price(sp,co)
{
$r=sp;
var e="select * from tbl where id='$r'; ?>";
document.getElementById('selling_price_'+co).value=e;
}
//php assign value does not work why?
//Please correct above code with proper description
You can not set a PHP variable from JavaScript. For example this is not possible:
var js_var = 10;
$php_var = js_var;
However you can set a JS variable from PHP:
var js_var = <?php echo $php_var ?>;
PHP is executed on the server side and then the result shows up on the browser.
JavaScript is executed on the client side once the server returns the response.
If you really need to pass a JS var then use AJAX to submit vars to the server.
It is not possible to assign a javascript variable to a php variable like your example above.
Javascript is a client side language whilst PHP is a server side language.
What you can do is to echo out php variables to a javascript code before it is rendered at the visitors web browser like:
<script>
var text = "<?php echo $variable; ?>";
</script>
If you are setting a variable in Javascript first, and then want to pass it on to PHP, you either have to use ajax, or use a html-form to post the information to the server

Assigning php variable to javascript alerts PHP source code

I want to assign the php variable $stop to the javascript variable stopped
<?php
$stop = $_POST['stop'];
?>
<script>
var stopped = "<?php echo($stop) ?>";
alert(stopped); //Output: alertbox with the text: <?php echo($stop) ?>
</script>
How can I solve this?
Since the PHP source code is being alerted, the document is not being run through the PHP preprocessor before being delivered to the browser.
Make sure that:
You're using a web server and not loading from a local file
The webserver supports PHP
The file the PHP code is in is one the server expects to find PHP in (typically this will be a file with a .php extension, but it is configurable).

How to get PHP string value in javascript?

Hi have looking on various questions but none of them seem to help me. I have a php variable in my php code and I am trying to access that in my javascript when I do. . .
var thing = "<?php echo($phpvariable); ?>";
then when I do
alert(thing);
It comes out to be "<?php echo($phpvariable); ?>" in the alert statement
What am I doing wrong?
Your PHP is obviously not being parsed. Are you in a .php file? If you're in a .js file, you'll need the server to parse those (or, more safely, put the PHP part somewhere in the DOM that the JS can access)
However, you're doing it wrong:
var thing = <?php echo json_encode($phpvariable); ?>;
Note: no quotes. json_encode will take care of that for you.
If this code is in a function in javascirpt that executes on click or at a specific event, then:
You are writing PHP Syntax in javascript, there is no way that you load the page then you run the php code. PHP code runs on the server side, so before any other HTML Javascript code executes
Else if you want to dynamically set the variable thing in javascript when the page is first loaded, then most probably you meant to write in the php file:
var thing = <?php echo '"'.$phpvariable.'"'; ?>;

php and JavaScript zip code

What i'm trying to do is when a user enters a zip code in the input type i get the value of the input type store in origin_zipcode than add origin_zipcode inside the $url variable. From there it should run the xml and i should be able to display the city and state but it doesn't do that it im trying to make it work like
https://ship.onemorepallet.com/shipment/start?hsCtaTracking=296d76b9-a6fc-4140-83f9-de066c986716|9001d544-e760-4dae-ac72-36cc2922be55?utm_referrer=http%3A%2F%2Fwww.onemorepallet.com%2F
when you enter in the zip code
$("#origin_zipcode").keyup(function(){
var origin_zipcode = $("#origin_zipcode").val();
<?php
$url = "http://zipcodedistanceapi.redline13.com/rest/gNPseXsQyP3daTlKfRB3D0v0mPVC3jJj6zMlwec54fie8EZowAsaBXaXN5zQpjav/info.xml/origin_zipcode/degree";
$xml = simplexml_load_file($url);
$city = $xml->city;
$state = $xml->state;
?>
$(".labelzip").text(<?php echo $city,$state"; ?>);
});
You can't send a jQuery variable to a PHP script unless you refresh the page using one of a few possible methods. This is because while jQuery is client-side, php is server-side. After PHP has finished loading the page, it can't be used again without a page reload.
You can look at these questions for a possible solutions:
How to pass jQuery variables to PHP variable?
jquery 'variable' to php 'variable'
how to pass a jquery variable to php and then display it

Categories

Resources