Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
i have javascript function and it returns a sum variable. But i couldnt reach it from php code.
my function is :
function add()
{
var sum = 0; // sum initially equals to zero.
var newNumber = 0; // Since textfields are initially text format, I convert them into integer and equalled to newNumber variable.
if(document.RodeoForm.checkbox.checked == true) // If checkbox changed to true,
{
for(var i=0;i<<?php echo $_SESSION['us']; ?>;i++) // Since we have 3 numbers, loop will work 3 times.
{
newNumber = parseInt(document.getElementById("fiyat"+i).value); // Take the number from field and convert it into an integer.
sum += newNumber; // Add the numbers into each other.
}
}
document.RodeoForm.tf.value = sum; // Print the sum onto the screen.
}
JavaScript runs on client side, whereas PHP runs on server side. They can't communicate directly with each other.
Once the page is rendered (and sent to the user), all PHP code was executed and is no longer visible in the source. The JavaScript however is sent along with the HTML and will be executed in the browser of the client. If you want PHP to be aware of a value generated by JavaScript, you have to manually send the data using AJAX.
You can only reach PHP from JavaScript using AJAX. Google how to do AJAX calls from Javascript.
Make sure you have session_start(); to the beginning of that php file, or in the php file which includes that code.
You can add PHP into Javascript but not the other way around because PHP is server side, so it can echo information into client slide.
Depending on what you're using this for , you can use AJAX to send the information to a PHP page.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
So the problem is I'm having is looping to check the file size, for example a file A.txt is 10 bytes and with other php scripts A.txt changes to 20bytes so when the A.txt file is changed I want the iteration to stop.
<?php
$i = 1;
$a = filesize("A.txt");
do {
$c = filesize("A.txt");
if ($a >= $c) {
} else {
echo file_get_contents('chat.log');
$i++;
}
} while ($i <= 2);
?>
What is my mistake?
It seems like you want to implement long-polling for a chat application.
This approach could lead you to lose some chat messages being delivered. And this is not very scalable.
I would suggest you to receive the previously known file size via a GET parameter, and check if the file size is larger than that.
Then I would suggest you to use fopen to open the file and read from the previous pointer (GET parameter) to the end of file, than reading the whole file every time.
you may also want to clearstatcache before each filesize, and add a usleep(100); inside the while loop.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
If I have a query string such as: http://mywebsite.co.uk/charge.php?val=5768
how can I validate this query string to throw an error with php if the user changes the value of the 'val' in the query string. Could I write some code on the charge.php page to do this?
It seems to me you're barking up the wrong tree. You cannot detect if someone "changes" something. All you get is a request for a URL, and that's it. You typically have no idea where that request came from or who provided the URL; you need to evaluate the request on its own merits.
I'm guessing you have some confidential action that's taking place when someone visits that URL. And the value changes some important part of that action. Then you need to create server-side checks and bounds that confirm whether the user is currently allowed to do whatever he requests to do there. You need to have enough information stored on the server to be able to confirm whether the action the user is about to make is allowed or not. You cannot simply trust the information in the URL, because anyone can tamper with it.
How to do this specifically in your case is not clear, since I have no concrete idea what's supposed to be happening there.
You can check if the val is an integer and if the val exists in the database. I use a function like following to check integer:
// CHECK IF VALID INTEGER
function valid_integer($str)
{
if (strlen($str) == strlen(preg_replace('#[^0-9]#i', '', $str) + 0) AND $str > 0 AND $str < 9999999999999)
{
return true;
}
}
so then I can check the val like following
if (valid_integer($_GET['val']))
{
echo 'valid int';
}
else
{
echo 'not valid int';
}
Then you have to check if the val exist in the database. If you use a db.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I'm using PHP to retrieve a document and find some data within the HTML.
I've used Tidy clean and repair as the document contains lots of bad html.
Anyway,
In the html document there is a tag like:
Link 12345
I want to get the value of the attribute (www.google.com) if the text content (Link 12345) matches a certain string.
$h2 = $doc->getElementsByTagName('a');
for ($i2; $i2 < $h2->length; $i2++) {
$attr2 = $h2->item($i2)->getAttribute('href');
if ($h2->item($i2)->textContent == "Link 12345")
print "FOUND";
}
which doesn't seem to work. I know that the for loop returns 'Link 12345' at some point (when ->textContent is called). But the comparison always fails even though Link 12345 appears if it is printed out. I suspect there is some issue with the encoding but I can't get it fixed.
Thanks.
You can use PHP's DOMXPath to execute an XPath query against your DOM object.
I believe that for yours it'll be
//a[text()="Link 12345"]
Will return all the who's text is "Link 12345".
A simple bug: you are testing "$h2->item($i2)->textContent" instead of "$h2->textContent"
Isn't it?
This question already has answers here:
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
(12 answers)
Closed 9 years ago.
I want to echo exact url : http://domain.com/mystuff.html#123456
but it only print http://domain.com/mystuff.html how to include the #123456 ? so become http://domain.com/mystuff.html#123456
my code so far
$urladdress = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']."?".$_SERVER['QUERY_STRING'];
You cannot. The fragment identifier is handled entirely client side so the browser will not send it to the server.
(The nearest you could come would be to (after the page has loaded) use JavaScript to sent it to the server in another request.)
you cannot get it using php, you can get it done by javascritp. use this test script to check your result.
<script>
alert(document.URL);
var urlDetail = document.URL;//you can use this variable to print in your web page.
</script>
Found the answer:
text
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Pass a PHP string to a Javascript variable (including escaping newlines)
Access a JavaScript variable from PHP
I have a Javascript function that takes in one variable. The function has some php code inside it. I want to use this variable inside the php section of the function. I couldn't get it to work. How is it done?
The issue is that your PHP code is being rendered on the server before being served to the client. I would recommend either converting the PHP code into Javascript code or creating an AJAX call to the PHP function.
Start reading about AJAX! You will likely need to rewrite some of the code you have written but what you are attempting to accomplish is not really possible otherwise.
try with it
<script>
var whatever = "<?php echo $phpVar ?>";
</script>