php in a javascript function not working

The folowing code should show me the results of the selected ID :

<form id="<?= $blog_id ?>" method="post" action="show.php"> <input type="hidden" name="blog_id" value="<?= $blog_id ?>"> <input type="hidden" name="blog_title" value="<?= $blog_title ?>"> <input type="hidden" name="blog_date" value="<?= $blog_date ?>"> <input type="hidden" name="blog_content" value="<?= $blog_content ?>"> Click <a href="#" onclick="document.getElementById('<?= $blog_id ?>').submit();">here</a> to see (<?= count($blog_comments) ?>) comments. </form>

But when i click the link, it does nothing (Does not lead me to show.php).
However, if i change onclick="document.getElementById('<?= $blog_id ?>') To onclick="document.getElementById('test') (And also the other id).
It leads me to show.php, but shows me the highest ID.
Why do i need the id to be a variable? because it is in a loop, and when i click the link, i want to get the correct information from the selected id.

I looked at my console (inspect element), and it says:

Uncaught TypeError: document.getElementById(...).submit is not a function

However i don't know how to fix this.

-------------Problems Reply------------

IMO is not good idea to set ID of form to numer:

first standards:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

but, maybe in Your case is problem in duplicate ID in code. Maybe You have more that one element with this ID ?

try this:

<form id="form_<?= $blog_id ?>" method="post" action="show.php">
<input type="hidden" name="blog_id" value="<?= $blog_id ?>">
<input type="hidden" name="blog_title" value="<?= $blog_title ?>">
<input type="hidden" name="blog_date" value="<?= $blog_date ?>">
<input type="hidden" name="blog_content" value="<?= $blog_content ?>">

Click <a href="#" onclick="document.getElementById('form_<?= $blog_id ?>').submit();">here</a> to see (<?= count($blog_comments) ?>) comments.
</form>

I wrote a simple test and it this works

Maybe ryrysz is right.

test.php

<?php for($i=1; $i<11; $i++): ?>
<?php
$blog_id =$i;
$blog_title = 'abc'.$i;
$blog_date = '11-02-2012';
$blog_content = 'dsfdsfdfsdfsdfs'.$i;
$blog_comments[] = '';
?>
<form id="<?= $blog_id ?>" method="post" action="show.php">
<input type="hidden" name="blog_id" value="<?= $blog_id ?>">
<input type="hidden" name="blog_title" value="<?= $blog_title ?>">
<input type="hidden" name="blog_date" value="<?= $blog_date ?>">
<input type="hidden" name="blog_content" value="<?= $blog_content ?>">

Click <a href="#" onclick="document.getElementById('<?= $blog_id ?>').submit();">here</a> to see (<?= count($blog_comments) ?>) comments.
</form>
<?php endfor ?>

show.php

<?php
print_R($_POST);

Category:javascript Views:11 Time:2019-01-03

Related post

  • Phonegap iOS 4.3 javascript functions not working 2011-10-16

    Has anyone experienced an issue with javascript function not working on Phonegap/iOS 4.3? I've tested my javascript in my Safari browser and it works. But when I try it in on the iOS simulator or my iPod touch it doesn't work. Here's my code: in head

  • PHP 5.3 namespaces - functions not working as expected 2011-03-21

    There are some functions not working properly with namespaces <?php namespace MyApp; class Fruit {} class Apple extends Fruit {} $apple = new Apple(); $name = 'Apple'; var_dump (is_subclass_of($apple, 'Fruit')); var_dump (is_a($apple, 'Apple')); v

  • Javascript function not working on php page 2012-01-24

    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"> <!-- js start--> <script src="<?=base_url();?>js/jquery-1.5.1.min.js" type="text/javascript"></script> <script src="<?=base_url();?>js/jqu

  • Hide/Unhide Div JavaScript functions not working in IE, Safari, and Chrome 2010-08-24

    I am trying to cause a div (which contains a table) to hide based on a users selection from a dropdown. It works fine in FireFox and Opera, but IE, Safari, and Chrome do not work. Any suggestions? <style> .hidden { display: none; } .unhidden {

  • Javascript functions not working / giving errors in my iFrame based app on Facebook! 2011-04-04

    I have a iFrame based app on Facebook which does what its supposed to. Its PHP-based. However, as soon as I put some Javascript function, it stops working at all! What I am initially trying to do is to see if I have authorization for the user. If I d

  • javascript functions not working properly under document.ready() 2011-11-08

    When ever I try adding the second function, the JavaScript ceases to work on the page. Can you guys help with possible error it is? Thanks. Note: If I comment out the second function, it works fine and the PHP code is an element from CaKePHP. <scr

  • javascript function not working after page load 2012-02-08

    why my code is not working? I have called a javascript function after page load PHP script.like below: <script> setTimeout("fb_login()",100); </script> fb_login() function is on same page function fb_login() { alert("ok"); } Tried with se

  • HTML within Javascript Function Not Working Properly 2009-06-25

    I am using the following JavaScript function: function Projects() { var PrjTb1 = "<input type=text id=PrjNme size=100/>" var PrjTb2 = "<textarea rows=5 col=200 wrap=hard></textarea>" var Info = "1. Project or activity name:<br

  • Php in MYSQL aggrate function not working 2010-01-22

    $result = mysql_query("SELECT avg(r.rate) FROM rate r where ImgName='1'"); this php is not working. Originally my code is <?php $con = mysql_connect("localhost","root","sql"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select

  • javascript function not working in IE 7 2010-04-09

    Can anyone please tell me how do I make this script run in IE 7? When I run this , nothing happens. <html> <head> <script language="JavaScript"> function moveSelectedOption() { // Fetch references to the <select> elements. var

  • javascript functions not working when in put in different files 2010-10-21

    I'm new to this whole thing, and am making a web app that relies heavily on dynamically loaded html pages into a div on the main page. The main page links to various js files including jquery and some of my own. As well as this, each of the loaded ht

  • Javascript function not working properly in Chrome 2011-05-24

    i do i function with js to change the div color depend on inputs click or select its work perfect on firefox but the radio input not see the onblure property so its never return div to its static color when click any where here the function <scrip

  • Javascript function not working based on the version of jquery being included. 2011-09-29

    My code of my Script is given below. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> <script type="text/javascript" src="scripts/prototype.lite.js"></script> <s

  • add options by javascript function not working 2011-11-28

    I have a javascript function. This function is supposed to populate a html select-box by adding options. Argument passed is, for example, [['val1','txt1'],['val2','txt2']] I expected that it would generate the html as under: <select id='element_id

  • Javascript function not working in IE and chrome 2012-01-12

    I have a VS2010,C# ASP.NET web app, I've written a JavaScript (in aspx page) that works perfect in firefox but almost does nothing in chrome or IE, it is my function: <script type="text/javascript" > function onRadioChange(rowIndex, value, myHr

  • Javascript functions not working in chrome 2012-01-19

    For some reason javascript functions are not working right now. function alert(){ alert('Close') } When I trigger the alert using: <span onclick="alert()">Hi</span> I get the following error: Uncaught RangeError: Maximum call stack size e

  • Javascript function not working after postback? 2012-04-25

    Here i am trying to increase the height of the iframe after postback but it is not working and the alert itself is not popping up.Here is my code function increaseiframesize() { alert("aaaaaa"); $('#MainContent_IFTrendAnalysis').height('523'); } and

  • javascript function not working in firefox 2013-10-02

    I am having the issue with the below JavaScript function to be run on a form submit it works fine on ie. and Google groan but Firefox not running the second JavaScript function ItemAddMultiple: function (data, loadBasketPage, callBack) { var actionPa

  • c# Multiple calls to a javascript function not working 2011-11-09

    On a click event I call the same JavaScript function 3 times but the javascript function itself only execute once. BLL.Common.executeJSFunction("DoSomething();"); BLL.Common.executeJSFunction("DoSomething();"); BLL.Common.executeJSFunction("DoSomethi

  • struts2 jquery autocomplete onselect call a javascript function not working properly 2012-03-12

    Am working with a java web application that was using struts2 features. Am having one struts2 jquery autocomplete in one UI. And when loading this, am having a listKey and listValue for this autocomplete field. What i need is when onselect work on th

  • javascript function not working correctly 2012-01-17

    I have the following simple javascript function to slide an object up and down. When I first click it should slide down which it should do and alerts with true which it should do. However on the second click I want it to slideup however it detects fi

  • Javascript function not working with an array 2011-01-07

    i have this function which loops through an array of check boxes checking if the boxes value is equal to something in a text box, i dont know whats wrong. function checkValue(contain) { var boxes = document.getElementsByTagName("input"); for (var i =

  • Javascript function not working correct in IE and Chrome, image not change if image in inside a accordion 2012-01-11

    I have ASP.NET site. At the master page I have javascript code. $(document).ready(function () { var img = $("img"); img.each(function (index, element) { if (img[index].complete == true) { Pixastic.process(img[index], "desaturate", {}); } else { img.l

  • html onclick run javascript function not working 2012-01-27

    I'm trying to run a javascript menthod on an input="image" in html. My code in my html looks like this: onclick="manageHandlers('Unassign'); document.getElementsByName('manageHandlers')[0].submit(); " and in my javascript the method is like this: fun

  • basic javascript function not working 2012-02-04

    I would like my script to check the textarea box when a user clicks on it and if it has the value "Add additional information here!" to make the box blank and change the text color to #000. it starts out as grey. javascript function changeMessage() {

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.098 (s). 11 q(s)