How to use php class using jquery call? - javascript

i have a problem with calling php data. Its shows me 'its works' and 'blalalala' only, without class function data. how to use class echo's calling from .load or some other way?
php file code:
echo 'its works';
echo '<br>';
echo 'blalalala';
Some_Data::getData();
script inside the html:
<script>
$(document).ready(function(){
function myShow(){$("#dd").load("/get_some_data.php")}
setInterval(function(){myShow()},2000)});
</script>
<div id="dd"></div>
my program shows me this into the browser:
its works
blalalala
So we can see that my class did not started.
And if i use my class without jquery its works perfect so problem not in my class. Its dont load data from my class. And even if i'm testing this case by using echo time(); as result i will see different time every 2 seconds but class do not starting.
php file:
class Some_Data
{
public static function getSomeData()
{
if(){ echo '<div class="my_messages">';
echo $row1['text'];
echo '</div>';
}else{
echo '<div class="their_messages">';
echo $row1['text'];
echo '</div>';
}
}}
Some_Data::getSomeData();
my class and my class calling located in the same php file so with require_once html shows me all what i need... there if no problems in class file... maby its some protection moments and i need to use .get and convert all variables into array to get them with json.parse

Try echoing the last line as you do with the previous three. If you don't echo (print_r, var_dump... etc) it won't send the information to the client.
echo 'its works';
echo '<br>';
echo 'blalalala';
echo Some_Data::getData();
If you are already doing an echo in your getData(); post your Some_Data class.

Related

How can I place a drop-down list made in php in a specific HTML form?

I have some php that connects to a database and creates a drop down list. I have a specific form in the HTML that I'd like to put the list in.
<body>
<form>
// some text inputs
// where i'd like the drop down to go
<?php makeList(parameter1, parameter2); ?>
// submit button
</form>
<?php
// connect to database
function makeList(arg1, arg2) {
echo '<select>';
while ($row = mysqli_fetch_array($result)){
echo "<option">;
echo $row[$column];
echo "</option>";
echo '</select>';
}
</body>
The only languages I'm allowed to use (apart from the sql) are php, html and javascript. As it is right now, makeList() returns an empty list. When I include opening and closing form tags in the function it returns a fully functional list, but then it acts as it's own form and I need to to be a part of the original form. Thanks.
EDIT: Sorry, forgot to mention the makeList function works fine when called within the php tags. It's when I call it in the HTML that it returns an empty list.
Firstly, you have some syntax issues with your script. It's not a valid HTML file, not a valid PHP file, and not a valid JS file.
If it were up to me, I'd define the PHP function at the stop of my script. Be careful to balance your opening and closing PHP tags. Something like this:
<?php
// connect to database
function makeList($arg1, $arg2) {
echo '<select>';
while ($row = mysqli_fetch_array($result)){
echo "<option">;
echo $row[$column];
echo "</option>";
echo '</select>';
}
?>
And only after that would I start to output my HTML.
Now there are a couple of important things to note about that script I just posted:
the database code is not in here...I don't see any connection or query getting run or anything
In your script, this function doesn't look valid. arg1 and arg2 need a $ in front of each to be a valid PHP function. If it's a JS function you want then well, you are very confused and probably need to go back and figure out why this is not a valid JS function.
Your function refers to a variable, $result, that you have not bothered to define. It is not mentioned anywhere else in your script. It is most certainly not mentioned anywhere inside your function. For $result to be defined inside your function, you either need to pass it in as an array or declare it as a global:
global $result
Your function doesn't return anything at all. It just echoes stuff. This doesn't mean you can't use it, but it does mean that the function has no return value. Echoing the result of makeList won't output anything at all
So after that script above, you might have something like this:
<body>
<form>
// some text inputs
<?php makeList($parameter1, $parameter2); ?>
// submit button
</form>
Depending on what your parameters ($parameter1 and $parameter2) are this should work.
<body>
<form>
// some text inputs
<?php echo makeList($parameter1, $parameter2); ?>
// submit button
</form>
<?php
// connect to database
function makeList($arg1, $arg2) {
echo '<select>';
while ($row = mysqli_fetch_array($result)){
echo "<option>";
echo $row[$column];
echo "</option>";
echo '</select>';
}
</body>

php in <script> tags executing without call why?

The php in method signout is executing without call . At first the username is echoed fine , but second echo below signout method shows that its null. It should be called when the singout link is clicked . Why is this happening ?
<?php echo $_SESSION['username']; ?> // this echo show real value
<script>
function singout()
{
<?php $_SESSION['username']='null'; ?>
window.alert("<?php echo $_SESSION['username'];?>");
}
<?php echo $_SESSION['username']; ?> // but this echo show null value
</script>
<a onclick="singout();" href="login.html" style="float: right">singout</a>
Your PHP is being called without the JS Function being called because on browser load each PHP line is procedurally called. Just because your PHP line is within a JS function doesn't mean it isn't called on browser load

display a div/span only after $_SERVER['HTTP_REFERER'] has been executed

I tried to approach it numerous ways, but somehow I'm not able to figure it out. Maybe you guys can help me?
I need to display a certain div/span or whatever container to display a text after page goes back via
header('Location: ' . $_SERVER['HTTP_REFERER']);
The reason why I need such a wierd approach to display something is that I'm using a href/GET combination to run a PHP function when my link gets klicked. (submit button is in use by another function/module so I can't use that )
HTML part
<p id="show_project">
<a id="add_to_project" name="add_to_project" href="?function">
Add to project
</a>
</p>
PHP function part
$this->getProductinfo();
if (isset($_GET['function'])){
$this->getSQLQuery($_GET['function']);
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
Any ideas ?
BR's
Why not add a message to your header() call
$this->getProductinfo();
if (isset($_GET['function'])){
$this->getSQLQuery($_GET['function']);
header('Location: ' . $_SERVER['HTTP_REFERER'].'?msg=We got a message');
}
Get the message
<?php
session_start();
if (!empty($_GET['msg'])){
$_SESSION['msg'] = $_GET['msg'];
header('Location: ' . $_SERVER['HTTP_REFERER']);
die;
}
else{
if (!empty($_SESSION['msg'])){
$msg = $_SESSION['msg'];
unset($_SESSION['msg']);
}
}
?>
<?php
/* Later in the document */
echo '<div>' . $msg . '</div>';
?>
You will need to sanitise and validate the $_GET variable
borrowed from here

Updating session variable using onclick in php

I am trying to update the $_SESSION['state'] whenever the user clicks on the anchor tag by firing the onclick event. The variable $state_names_array as all the names of the states of a specific country. But problem is that no matter whichever of the anchor tag I click on, the $_SESSION['state'] is always getting updated by the last element of the $state_names_array.
<?php
foreach($state_names_array as $value){
$temp = '<?php $_SESSION["state"]=$value;?>';
?>
<?php echo $value ?><br>
<?php
}
?>
I will not code whole code, I will just show you an example, how to do it:
<?php
foreach ($state_names_array as $value) {
echo ''.$value.'<br>';
}
?>
Then you will have javascript function change_value:
here you will send ajax call to some PHP file, where you will process $_SESSION["state"]=$value;
Also you didnt say, what you want after anchor is clicked, because you will be able to access new $_SESSION["state"] only after refreshing your site...
Also maybe you just want to redirect to index.php, after clicking on some anchor, and that will be enough for you, then just use value as $_GET parameter, f.e.:
<?php
foreach ($state_names_array as $value) {
echo ''.$value.'<br>';
}
?>
and add to you index.php file this lines:
if (isset($_GET['new_value'])) {
$_SESSION["state"] = $_GET['new_value'];
}

Put SQL array data append to javascript

$data = mysql_query("SELECT * FROM Table WHERE 1") or die(mysql_error());
while ($info = mysql_fetch_array($data)) {
echo ''.$info['value'].
'<br>';
}
Here the value of sql is showing, my question is how can i add div or other attribute ?
For example
echo '<img src = "'.$info['value'] .'" />'; // Convert into javascript append type
$("#messages").append("<img src='"+ value +"'/>");
I cannot use <?php echo ?> the required data is on other page and i'm using ajax to take into main page but i cannot understand how can append the array data
thanks
Why do you want to append using Javascript?
Why not echo the tags appropriately and include whatever attributes you want?
You can only make that if you use ajax in jQuery.
PHP is executed in server and javascript in client navigator so when jQuery see your page, it see only an html file because the
server send only html file.
You can create your jQuery with echo in PHP but it's a dirty solution.

Categories

Resources