PROMPT/CONFIRM values in PHP

Something where i’m struggling with some time is to ask for a value with a JS prompt and use this result to run a query.

So I want the user to enter a value before closing a form or when losing a focus. I suppose i have to use a JS prompt and store this value in a var.

But how can I use this value to run a query and update a database table? Can someone help me with some example code?

It’s easy to use a PHP value in a JS environment using this format mixed with JS code: <?php echo $val; <?

The opposite way is not working!
<script>
var tmp ‘test’
<?php $val= <? tmp;
</script>

Somenone?

You don’t describe your requirement very well, and I suspect you’re confused about how this stuff works. That said, I think you want to use a form application. I would.

REMEMBER: 1) PHP executes on the server. 2) JavaScript (and HTML) executes on the browser. Your browser (and JavaScript) can only “see” what PHP outputs to it, and the server can only “see” what your browser GETs or PUTs in an HTML request.

Yes, a form application is overkill to get a value from the user, but it will get the job done on the correct end.

Question is simple.

How do i ask for a value to the user using JS prompt and write this value in a database using updateSQL macro?

The problem i have is that the JS value is not passing to the PHP.

Some code examples would be nice :)))

Yes, your question is simple, the answer is not. You want an example of functionality that doesn’t exist because you have no idea how any of this works. But maybe you can get someone else to do this project for you, eh? Did you try mom?

Or get a book. O’Reilly’s HTTP in a Nutshell is a great start and it’s free. The basics are also covered on www.w3schools.com for free. Take a course if you’re the low-energy type. Hire someone who already read a book or took a course if you’re low energy and under a time constraint.

@nonkelmike

the only solution I’m aware of is to send the variable name\value from js to the php server via ajax.

To do that, I’ve created a SC app only for this purpose and then I use this line in the JS code in any app where I need to send JS variables back to PHP:

$.post('../shr_b_set_php_sess_var_via_ajax/index.php', { variable_name: 'variable_value' });

The SC app ‘shr_b_set_php_sess_var_via_ajax’ is a blank app with just this code in its onExecute event:

foreach($_POST as $key => $value){

$_SESSION[$key] = $value;

}

Note that in the SC app where you’ll read the variable in PHP you need to access it as a session variable.

I haven’t used it too much yet, so I’m not sure it’s 100% reliable as ajax is async and sync ajax calls are deprecated.
In my scenario, the app setting the var in JS then used sc_redir to call another app, where the var was read in PHP. So, maybe due to the time needed to execute the second app, it didn’t seem to have any async issues.

To send vars from js to php, I do this in the SC app where the JS code is running:

$.post('../shr_b_set_php_sess_var_via_ajax/index.php', { var_name: 'var_value' });

“shr_b_set_php_sess_var_via_ajax” is a blank SC app with only this code in its onExecute event:

foreach($_POST as $key => $value)
{
    $_SESSION[$key] = $value;
}

Then in the SC app where you need to read the var name\value in PHP, you access it as a SC session var as [var_name] or using straight PHP as $_SESSION[“var_name”]

Note that I’m not sure this is 100% reliable, as ajax is async, so will the PHP session var be created by the time the PHP code is accessing its value?
It seems to work in my scenario: the SC app setting the var via js/ajax is a different one from the SC app reading the var in PHP. So maybe there are no async issues because the time it takes to load (sc_redir) the second app is always enough to prevent them.

i will give it a try
THX!

hi…

have you tried? did it work?