javascript redirect won't work? - php

I have a little bit of javascript code that is getting some parameters from php. the php variable $rid is an integer with a value within 1-100. what i am trying to do is change the value of the 'rid' variable in the querystring by a specified amount (offset). when i remove the last str($rid) characters from window.location.href, i am left with http://www.qwerty.asdf/uiop?rid=. When I add <?php echo $rid;?>+offsetto it I get http://www.qwerty.asdf/uiop?rid=2 for example. when I try the code, nothing happens, but I've debugged and str is the desired value by the time I say window.location.href=str;
why doesnt the page redirect? I have tried window.location as opposed to window.location.href, but it doesn't work.
p.s. there is no other javascript on the page
function moveRid(offset) {
str = window.location.href.substring(0,window.location.href.length-<?php echo strlen($rid);?>);
rid = <?php echo $rid;?>+offset;
str += rid;
window.location.href=str;
}

You need to actually set the window.location
function moveRid(offset) {
str = window.location.href.substring(0,window.location.href.length-<?php echo strlen($rid);?>);
rid = <?php echo $rid;?>+offset;
str += rid;
//window.location.href=str;
window.location = str;
}

Related

Decode a search string in the url

I have a search text input. When i submit the searching, the text will get into the $_GET, and i will land on a search.php site.
function search_k()
{
return""!=$.trim($("#country_id").val())&&
(document.location="/kereses?k="+$("#country_id").val())
}
If i type in this: some test text
The url will be like this: kereses?k=some%20test%20text
How can i replace the %20 with a + mark? My problem is, that when i search for a product, that name has a + mark, i didnt get any result.
If i echo $_GET['k'] with php, that replaces the + mark to empty whitespaces. I have a real_escape_string function on the $_GET['k'].
UPDATE:
It still dont work, no changes. I always get the %20 in the GET. If i alert the word var, i get the right text.
function search_k()
{
if($.trim($('#country_id').val()) != "" )
{
var word = encodeURIComponent($('#country_id').val());
document.location = '/kereses?k='+word;
}
return false;
}
You need encodeURIComponent(); javascript function
var x= $("#country_id").val(); //get field value
x = encodeURIComponent(x); // encode it before passing
(document.location="/kereses?k="+x); // pass your field value
that's it

Pass array into GET URL using jQuery

I am trying to pass an array into a GET URL BUT it is coming as add_cart?ids=1,2,3,4,5 as opposed to sending it properly.
This is my jquery code where it adds the array to the URL and directs the user to the next page:
$(document).on("click", 'button.btn_checkout', function(){
var cart = <?php echo json_encode($carts); ?>;
window.location.href = "addcart.php?cart=" + cart;
});
And then on the addcart.php page I am unable to get these values.
Ideally on this page, I want the values in the form 1,2,3,4,5
This is the code for that page:
<?php
session_start();
$cart = isset($_GET['cart']) ? $_GET['cart'] : "";
$cart = explode(",", $_GET['cart']);
for($i = 0; $i<$cart.size; $i++){
echo $cart[$i];
}
?>
Where am I going wrong?
You are using the jQuery GET request a little wrongly. You will use
window.location.href
when you are trying to change the location of your current webpage.
Try this instead:
$(document).on("click", 'button.btn_checkout', function(){
var result = <?php echo json_encode($carts); ?>;
$.get( "addcart.php", { cart: result } );
});
I'm assuming by ARRAY you mean to include the braces {}?
If so, your problem is actually the php part. json_encode is creating a proper json object. Which then is being added onto the url AS the object itself, and NOT a string. You actually want it to be a string.
this line: var cart = <?php echo json_encode($carts); ?>; is the main issue.
convert it to something like: var cart = "<?php echo json_encode($carts); ?>";
Use $.param() function to convert params to get query string.
You are directly initialising Json to param but not converting to query string.
Above function will convert Json to query string
Try to use the javascript function JSON.stringify() to convert to json.
Note: Don't sent long data over a URL. There is a limit for sending data via url and it will end up in a corrupted data if exceeded the limit. For large data, use POST method.

Php variable to JavaScript

How to add the value of php variabele to jquery variable and adds them,i have a form that will take a value and post to second page where second page contains a div panel where the form checkboxes values are adding and subtracting,basically i want to add the submitted value from page1 to page2.Here is the code.I have 3 form values which will be redirected one by one.Whenever user submits the respective button
if($_POST['submit'])
{
$beg=$_POST['basic'];
}
function refreshPrices() {
var currentTotalValue = 0;
var beg=?????
$("#results div").each(function() {
if (!isNaN(parseInt($(this).find("span").text().substring(1)))) {
currentTotalValue += parseInt($(this).find("span").text().substring(1));
}
});
$("#totalValue").text("$" + currentTotalValue)
}
var beg=<?php echo $beg; ?>
Try this:
var beg = '<?php echo $beg ;?>';
if you want to add to the total value you can do this:
currentTotalValue = currentTotalValue + parseInt(beg);
its better to do it this way :
var js_var = '<?=json_encode($php_var);?>';
This way you will be able to transfer any type of data to js (arrays, objects, strings, etc.) and access it easily like this:
js_var['element']
var beg = '<?php echo $beg ;?>';
But this can be only one once during the load of the page. After that we cant assign a server side script to js.
Retrieve value by input class or id like dat
var beg = $("#basic").val();

Send Get with jQuery with spaces

I have a problem with a word count using the jQuery. The method is working as soon as I click space it will stop.
HTML:
<textarea id="essay_content_area" name="essay_content" onkeydown="words();"></textarea>
<td>Number of words: <div id="othman"></div></td>
jQuery:
function words(content)
{
var f = $("#essay_content_area").val()
$('#othman').load('wordcount.php?content='+f);
}
PHP file:
if(isset($_GET['content']))
{
echo $_GET['content']; // if it works I will send this variable to a function to calculate the words
}
the script shows the content until I click space. any suggestions ?
You need to url-encode the value before sending it over to your PHP script as the value of a GET parameter. Consider this:
function words(content)
{
var f = $("#essay_content_area").val()
$('#othman').load('wordcount.php?content=' + encodeURIComponent(f));
}
You don't need php to count the words you can use JS, something like this:
function words(content)
{
// Get number of words.
var words = content.split(" ").length;
}
You need to url encode your variable before you send it (space is not a valid url character):
function words(content)
{
var f = encodeURIComponent($("#essay_content_area").val());
$('#othman').load('wordcount.php?content='+f);
}

PHP jQuery value problem

I have problem to send value from php to jQuery script.
PHP looks like that:
echo "<a id='klik' value='".$row['id']."' onclick='help()' href='http://www.something.xx/tag/".$row['link']."'>".$row['name']."</a><br>";
and script jQuery:
function help(){
var j = jQuery.noConflict();
var zmienna = j('#klik').val();
alert(zmienna);
j.post('licznik.php',{id:zmienna}, function(data) {
alert(data);
});
}
licznik.php
$p=$_POST;
$id=$p['id'];
echo $id;
$wtf = "UPDATE tag_content SET wyswietlenia=wyswietlenia+1 WHERE id='$id'";
$result = mysql_query($wtf);
And as I tested, it has problem at the begining (alert(zmienna); doesn't work, shows nothing). How to fix it?
Thx for help and if u want more informations (like more code etc.) let me know.
{id:zmienna} is not JSON, {'id':'zmienna'} is. Fix that.
The a tag can't have a value. What you can do is pass the id as a parameter:
echo '<a id="klik" onclick="help(\''.$row['id'].'\')">...</a>';
and in Javascript:
function help(zmienna) {
alert(zmienna);
}
That's probably because the anchor tag has no value attribute (http://www.w3.org/TR/html4/struct/links.html). Try this instead:
var zmienna = j('#klik').attr('value');
I would also advice against using value. If I need additional data, I use a tag like data.

Categories